Skip to main content

Posts

Showing posts with the label Perl

Python Convert

I shall officially look to Python for all my scripting needs from now on. Just when I thought scripting could not get any easier than Perl, Python has come along and proved me wrong. Everything I have heard about Python's simplicity and elegance is true. My Python education started a while back when I attended at least two tutorials in grad school by Python enthusiasts who were trying to pull some of us Perl enthusiasts to their side. The whole idea of learning a whole new scripting language did not appeal to me and thus I didn't give Python much thought. Lately I have had some downtime and there seemed to be no more productive way to spend it than learning Python and that's what I did. My first set of tutorial was a quick and basic intro by Steven Thurlow . Aside from what is contained in those s ets of lectures, I needed to learn two sets of subjects, namely string/text manipulation and system calls. David Mertz provides a nice introduction to text processing usi...

Parsing Commandline Options in Perl

Anyone who has tried to parse command-line options in Perl knows how messy the process gets with increasing number of options. Luckily, there are some modules that the whole process more manageable. The tutorials I am learning from are http://systhread.net/texts/200704optparse.php#one and http://aplawrence.com/Unix/perlgetopts.html . Without using a special Perl module, options can be parsed as follows (credit to http://systhread.net/texts/200704optparse.php#one ) while ( my $arg = shift @ARGV ) { if ( $arg eq '-F' ) { $F_FLAG = 1; } elsif ( $arg eq '-f' ) { $FILE_ARGUMENT = shift @ARGV; } elsif ( $arg eq '-u' ) { usage(); } else { usage(); exit 1; } } Ultimately, using the getopt module should be done if it is available, why reinvent the wheel? Here is an example of using the Getopt module: use Getopt::Std; ... getopt ('f:uF'); die "Usage: $0 [ -f filename -u ]\n" unless ( $opt_f or $opt_u ); if ($opt_f) {...

Intro to OOPerl

Excellent set of basic notes can be found as Part 1 and Part 2 The main reason I am interested in learning it is to enable me to use some important Perl modules/packages effectively. In particular, I want to use the PerlMol package to clean up some of my scripts.

Checking Perl Library Path and Modifying it

It is contained in an array @INC, so checking it would be as easy as printing that array. One can add or subtract items to this array to include of exclude some libraries using array manipulating tools like shift/unshift/chop/pop/push/splice/ ... . Here is a script demonstrating that. !#/usr/bin/perl -w #Prints Perl library path and modifies it: print "Default Perl library path:\n@INC\n"; push(@INC, "/home/user/lib/perl"); print "New Perl library path:\n@INC\n"; And the output was: Default Perl library path: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl . New Perl library path: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl . /home/user/lib/perl BTW: I brushed up on Perl basics from this great resource: http://www.pa...