Tuesday, August 18, 2009

Perl : Finding Installed Modules in Perl

Context:
I was working on a solution to upload data from an excel file - that contains macros - using Perl. Perl is a module based implementation of parsing langugage and has a different module used for Windows / Linux based implementation for parsing / creating excel files (Win32::OLE->GetActiveObject('Excel.Application') viz. Spreadsheet::ParseExcel )

Perl file would work on Windows environment but not on Linux. I will not get into the details what the error message I got and what does it means for now.

Commands to list Perl modules:
After a while of googling - I figured out following one line script that will list all the Perl modules installed on Linux environment.

perl -MFile::Find=find -MFile::Spec::Functions -lwe 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'

here is the same command to for Windows:
perl -MFile::Find=find -MFile::Spec::Functions -lwe find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC
Nothing much has changed other than double quotes highlighted in red.

An Alternate Way:
One can also try executing following command to fetch the details. If there is an error while execuing command, it means the module is not installed.

perl -MSample::Module -e 'print "\nModule is Installed\n\n"'

sample output - installed module:
bash-3.2$ perl -MExtUtils::Installed -e 'print "\nModule is Installed\n\n"'
Module is Installed
bash-3.2$


Sample output - module that is not installed:
bash-3.2$ perl -MSpreadsheet::ParseExcel -e 'print "\nModule is Installed\n\n"'
Can't locate Spreadsheet/ParseExcel.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi ... /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .).
BEGIN failed--compilation aborted.
bash-3.2$

Hope this helps . . .

Thursday, August 13, 2009

Blogger : Code formatting support and Blogger

It would be difficult for someone like me - being a software developer - to eschew from writing technical blog. It is a best practice to share the code and give example wherever possible. I started blogging recently and as it appears, lack of ability to format the code is becoming a major hold up for me to document every day learning if not sharing.

I am more of switching from Wiki to Blogging - and hence this problem, but there seems to be a scope for development if one can do. I have been a big fan of wiki. I absolutely love how smart macros does it offer and do all the hard I hate it when it comes to writing some code and 'Ctrl + Shft + F' short cut doesn't work. How dependent I have become on the editors is a topic - not for today's blog.

Syntax highlight is a very basic feature - I would use it day in and day out. I don't get it on blogger! I have to go to some sites to covert the code to HTML first and then replace all the HTML tags to comply with the standards.

One more thing which I miss is excel macros in WiKi. It just doesn't get limited there. Wiki seems to offer a pluggable framework. I have seen people add their own macros for various applications.

Reference Material:
Some sites I have found while I re-searched this subject online are:
  1. Java 2 Html tool converts java code into HTML and is available as online applet http://www.java2html.de/
  2. Built on Java2Html but offers a way to covert esp. flex action script code in HTML with syntax highlight. http://keg.cs.uvic.ca/flexdevtips/java2html/
  3. explains how to get formatting up and running on your blogger account http://codeblog.kello.se/2009/01/07/getting-code-formatting-with-syntax-highlighting-to-work-on-blogger/
  4. If you want an HTML based tool - here is one for you. However, the flip side is that it cannot highlight the code - http://codeformatter.blogspot.com/2009/06/about-code-formatter.html
  5. Something that I have read about but not tried - http://www.manoli.net/csharpformat/
Conclusion:
Blogger may not be an obvious choice of a technology geek! There are some basic features, I would like to see before I find it addictive!

Tuesday, August 11, 2009

Java : Garbage Collection

Context:
I got a query - how does garbage collection work in Java? This was the time I started gathering details by means of googling. The details compiled here are based on my research online. Please note that the question I was asked about is very generic - and the details below touch upon the topic at very high level.


What is a Garbage collection?
Garbage collection is a mechanism in java which works on mark and sweep basis and relives programmers from spending time in writing code to claim back the memory allocated to the objects.

The mechanism also is part of Java's security strategy. It will prevent current program to crash JVM instance by means of memory hazards.

This service of course doesn't come free, it causes a small overhead since the process has to be continuously running in the background. However, keep in mind that scale of the application will determine the magnitude of overhead caused by this process.

How it works?

Garbage collector will iterate over all the available objects. The ones which are left out are marked for garbage collection. In the sweep stage, Garbage Collector deletes the objects for which the heap space was allocated and make it available to executing program.

Garbage collector will determine eligibility of an object by measuring its "reachability" from the root node. Any objects which are referred to by a root is a live object. Subsequently any objects referred to by a live object is also considered live object. Objects which are not reachable are dead and will be removed from heap space.

There are algorithms used by Garbage collectors to mark "reachable" objects abut it would be a subject in itself to learn more about them.

Garbage Collection Strategies

The garbage collectors might use Compacting and Copying strategies to deal with heap fragmentation.

What are generations?

Generations refer to the life span of the variables. Local variables for example are the most short lived ones and will be the first ones to be identified and claimed back. They are called young generations. Old generations are the objects which survive across multiple collections. Old and Young generations symbolize the memory pool available to store such objects.

When young generations are filled up, it causes minor collection and when tenured generation pool will cause Major Collection to occur. Note that later is much a slow process considering the fact that it involves all live objects

Java program is allocated heap space. Every thread is allocated a separate stack of functions and stacks reside in Heaps. Local variables are part of stacks.

Conclusion:

Although JVM encapsulates memory management under the hood - knowing how it works will make the developers write code more consciously keeping scalability in mind.

More (Technical) resources:

  1. Command line parameters which come for JMV's performance improvement are described by Sun here: http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
  2. The details on how to performance tune Garbage Collection in java 1.5 is given here - http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html

Flex : Making flex application full screen

Context:
While performing unit testing of a new module I was transition a couple months back, I figured out a problem. The button that read "Toggle Full Screen" did not work how it should have i.e. it didn't switch between browser and full screen mode.

Why full screen doesn't work?
RTFM still holds true. None really read the manual to make it work. Merely copy pasting the code from Adobe example / sample code doesn't work!!

One needs to make changes in the java script / html templates as they are the ones which will initialize flash player and allow it to run or not run in full screen mode.

How to make it work?
You may want to walk thru following steps to verify all the elements to make flex application full screen are in place

  1. You must have version 9,0,28,0 or later of Flash Player installed to use full-screen mode and 9,0,115,0 or later for hardware-scaled full-screen mode (You can check it here - http://kb2.adobe.com/cps/155/tn_15507.html)
  2. Check if index.template.html contains reference to AC_FL_RunContent function.
  3. If yes, update the function to also include following pair of parameters
    AC_FL_RunContent(
    "src", "${swf}",
    "width", "${width}",
    "height", "${height}",
    "align", "middle",
    "id", "${application}",
    "quality", "high",
    "bgcolor", "${bgcolor}",
    "name", "${application}",
    "allowScriptAccess","sameDomain",
    "type", "application/x-shockwave-flash",
    "pluginspage", href="http://www.adobe.com/go/getflashplayer">http://www.adobe.com/go/getflashplayer, "allowFullScreen", "true"
  4. Update <object> tag in index.template.html to include parameter allowFullScreen and set its value to "true" <param name="allowFullScreen" value="true" >
  5. If you are using <embed> tag - you need to update it to set value of allowFullScreen attribute to true e.g. <embed
    src="${swf}.swf" quality="high" bgcolor="${bgcolor}"
    width="${width}" height="${height}" name="${application}" align="middle"
    play="true"
    loop="false"
    quality="high"
    allowScriptAccess="sameDomain"
    type="application/x-shockwave-flash"
    pluginspage="http://www.adobe.com/go/getflashplayer"
    allowFullScreen="true" />
  6. The flex code is in place to toggle the mode from normal to fullscreen and vice versa

Conclusion:

Its easy to miss on trivial things and reading the manual and doing implementation will save clients from going baffled! :-)