Tuesday, March 11, 2008

Running external commands in Perl

Perl enables you to call external command line utilities.
Whenever possible, avoid calling external commands
* Perl supports a large number of built in functions
* External commands are generally not portable
* Often more time consuming (process setup/teardown overhead)

There are several methods to execute external commands

* The open() function
* The system() function
* Back-quotes
* The fork() & exec() functions

All of these methods have different behaviour, so you should choose which one to use depending of your particular need. In brief, these are the recommendations:

system() : You want to execute a command and don't want to capture its output
exec : You don't want to return to the calling perl script
backticks : You want to capture the output of the command
open : You want to pipe the command (as input or output) to your script

The native shell is used to execute the command line.

Using open()

Use open() when you want to:

- capture the data of a command (syntax: open("command |"))

- feed an external command with data generated from the Perl script (syntax: open("| command"))

Examples :

* Read the output from one or more commands

open( README, "ls -l |" );
$line = ;

* Write to the input of one or more commands

open( WRITEME, "| Mail -s 'test' joe@foo.com" );
print WRITEME "Dear John,\n";

#-- list the processes running on your system
open(PS,"ps -e -o pid,stime,args |") || die "Failed: $!\n";
while ( )
{
#-- do something here
}

#-- send an email to user@localhost
open(MAIL, "| /bin/mailx -s test user\@localhost ") || die "mailx failed: $!\n";
print MAIL "This is a test message";

Using system()

system() executes the command specified. It doesn't capture the output of the command.

system() accepts as argument either a scalar or an array. If the argument is a scalar, system() uses a shell to execute the command ("/bin/sh -c command"); if the argument is an array it executes the command directly, considering the first element of the array as the command name and the remaining array elements as arguments to the command to be executed.

For that reason, it's highly recommended for efficiency and safety reasons (specially if you're running a cgi script) that you use an array to pass arguments to system().

Examples :

#-- calling 'command' with arguments
system("command arg1 arg2 arg3");

#-- better way of calling the same command
system("command", "arg1", "arg2", "arg3");

The return value is set in $?; this value is the exit status of the command as returned by the 'wait' call; to get the real exit status of the command you have to shift right by 8 the value of $? ($? >> 8).

If the value of $? is -1, then the command failed to execute, in that case you may check the value of $! for the reason of the failure.

system("command", "arg1");
if ( $? == -1 )
{
print "command failed: $!\n";
}
else
{
printf "command exited with value %d", $? >> 8;
}

# The return value is the integer value returned by the shell

$err = system( "ls -l | more" );

# Here the more command can be used becuase the new shell inherits STDIN, STDOUT, and STDERR.

Using backticks (``)

In this case the command to be executed is surrounded by backticks. The command is executed and the output of the command is returned to the calling script.

In scalar context it returns a single (possibly multiline) string, in list context it returns a list of lines or an empty list if the command failed.

The exit status of the executed command is stored in $? (see system() above for details).

Examples :

#-- scalar context
$result = `command arg1 arg2`;

#-- the same command in list context
@result = `command arg2 arg2`;

Using exec()

The exec() function executes the command specified and never returns to the calling program, except in the case of failure because the specified command does not exist AND the exec argument is an array.

Like in system(), is recommended to pass the arguments of the functions as an array.

PATH Environment Variable
All methods for executing external commands use the $ENV{PATH} environment value to locate "unqualified" commands.Unqualified commands have no explicit full path specification.The $ENV{PATH} environment value is initialized from your user environment when the Perl interpreter starts.The structure of the $ENV{PATH} environment value is a colon-separated list of search paths.

Sunday, February 3, 2008

Programming Methodology & Algorithms

Programming from Specifications presents a rigorous treatment of most elementary program-development constructs, including iteration, recursion, procedures, parameters, modules and data refinement.

The below link provides more details about the programming methodology :
http://web.comlab.ox.ac.uk/oucl/publications/books/PfS/

Some good algorithm related sites :
http://www2.toki.or.id/book/AlgDesignManual/BOOK/BOOK/BOOK.HTM
http://www.cs.sunysb.edu/~algorith/
http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/

Thursday, January 31, 2008

Standard C library

Both Unix and C were created at AT&T's Bell Laboratories in the late 1960s and early 1970s.The C programming language, before it was standardized , did not provide built-in functionalities such as I/O operations . By the beginning of the 1980s compatibility problems between the various C implementations became apparent.

In 1983 the American National Standards Institute (ANSI) formed a committee to establish a standard specification of C known as "ANSI C" . Over time, user communities of C,shared ideas and implementations of what is now called C standard libraries to provide that functionality.

Since the standardisation of the C library , applications written strictly within the bounds mentioned in the standard can vouch to be portable across different platform implementations.

There are lot of online resources which can act as a reference for the standard C libarary :
http://www-ccs.ucsd.edu/c/
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
http://www.freshsources.com/1995002A.HTM

Books on C :
http://publications.gbdirect.co.uk/c_book/

If you would like to learn and understand the C libarary , you should grab yourself a copy of the The Standard C Library by P. J. Plauger . Dr. Plauger has impeccable qualifications for writing this book - he was secretary of the ANSI C committee.

Wednesday, January 30, 2008

Jargon File

Jargon Defination
Language used in a certain profession or by a particular group of people. Jargon is usually technical or abbreviated and difficult for people not in the profession to understand.

But it seems,within the software industry , the jargon's generation rate is far too higher to keep pace with it.People use Jargons to impress upon their peers/managers.Its better to learn , than to be left out or be embarresed to be asking someone for their meanings.I found one good link which provides a good vocabulary of computer jargons and might prove helpful in the long term.

http://catb.org/~esr/jargon/html/index.html

Free E-Book resources

Around 38 percent of Indian Internet users (14 million) spent an average of 8 hours per week online ,as found in the last CNET survey . Due to the same reason,we see a proliferation of sites providing books in Electronic form (E-Book) .
I have used the below sites and feel they have a decent collection of books on varied subjects and might interest people from varied backgrounds ( though it might appeal more to people from computer science background ).

Some of the links are :
http://en.wikibooks.org/wiki/Main_Page
http://freecomputerbooks.com/
http://homepage.mac.com/kaotech/Free_Books.html
http://www.freetechbooks.com/
http://www.gutenberg.org/wiki/Main_Page

There may be more and better sites . If anyone comes across , I would be happy if you too let me know about them.

Happy Reading

Tuesday, January 29, 2008

Open Source Education : Knowledge is there to be shared

The most practical thing to go open source according to me has been education.It started through the MIT's OpenCourseWare initiative , which plans to put all of its educational materials for its under-graduate and graduate level courses freely available on the net for anyone.

I think this provides an advantage to interested students/professionals to make most of the missed out opportunity of getting into a course at MIT.Many top universities seem to be following the MIT way and are providing their courses online.The world won't be divided now based on the quality of education with these initiatives.

Top 10 universities providing online courses
http://www.jimmyr.com/blog/1_Top_10_Universities_With_Free_Courses_Online.php

MIT Open CourseWare
ocw.mit.edu

Stanford Open CourseWare
http://stanfordocw.org/

CMU Open Learning Initiative
http://www.cmu.edu/oli/

Also,to make it more convinient to search and find courses,this site would be handy :
http://ocwfinder.com/

Other helpful sites
http://www.ocwconsortium.org/
http://www.opencourse.info/
http://education.jimmyr.com/
http://www.jimmyr.com/blog/Online_Education_Free_201_2006.php

Monday, January 28, 2008

Tool Tips : Code search

Its always a good way to learn a language by reading good code written by expert programmers.Rather than searching the entire web for code snippets , we have some popular code search engines that give instant and relevent details .

Some of the admired code search tools are :

www.csourcesearch.net

http://www.google.com/codesearch

http://www.koders.com/