Friday, May 15, 2015

Executing a perl script

Perl is interpreted and like c, c++ and java, it doesn't get compiled to any native object code or byte code. A Perl script is just a text file and it is directly taken and executed by perl interpreter. 

On Unix, the Perl interpreter is called "perl". Syntax of running perl script on unix is following:  

$ perl my_first_script.pl
 

Multi-pass functioning of perl interpreter:

The interpreter does whole operation in multiple passes. In first pass it analyze the file for any syntax errors.  In second pass it runs the Perl code.  

No 'main' function:

There is no "main" function -- the interpreter just executes the statements in the file starting at the top.

Making a perl script executable:

Following the Unix convention, the very first line in a Perl file usually looks like this...
#!/usr/bin/perl -w
 

This special line is a hint to Unix to use the Perl interpreter to execute the code in this file. The "-w" switch turns on warnings which is generally a good idea. In unix, use "chmod" to set the execute bit on a Perl file so it can be run right from the prompt...
> chmod u+x foo.pl  ## set the "execute" bit for the file once
>
> foo.pl     ## automatically uses the perl interpreter to "run" this file

The second line in a Perl file is usually a "require" declaration that specifies what version of Perl the program expects...
#!/usr/bin/perl -w
require 5.004;
 

Perl is available for every operating system imaginable, including of course Windows and MacOS, and it's part of the default install in Mac OSX. See the "ports" section of http://www.cpan.org/ to get Perl for a particular system.

No comments:

Post a Comment