Monday, 28 October 2013

PS_01_Perl - Basics

Script names
While generally speaking you can name your script/program anything you want, there are a number of conventional extensions applied to portions of the Perl bestiary:
.pm - Perl modules
.pl - Perl libraries (and scripts on UNIX)
.plx - Perl scripts

Language properties
· Perl is an interpreted language – program code is interpreted at run time. Perl is unique among interpreted languages, though. Code is compiled by the interpreter before it is actually executed.
· Many Perl idioms read like English
· Free format language – whitespace between tokens is optional
· Comments are single-line, beginning with #
· Statements end with a semicolon (;)
· Only subroutines and functions need to be explicitly declared
· Blocks of statements are enclosed in curly braces {}
· A script has no “main()

Invocation
On platforms such as UNIX, the first line of a Perl program should begin with
#!/usr/bin/perl
and the file should have executable permissions. Then typing the name of the script will cause it to be executed.
Unfortunately, Windows does not have a real equivalent of the UNIX “shebang” line. On Windows 95/98, you will have to call the Perl interpreter with the script as an argument:
> perl myscript.plx
On Windows NT, you can associate the .plx extension with the Perl interpreter:
> assoc .plx=Perl
> ftype Perl=c:\myperl\bin\perl.exe %1% %*
> set PATHEXT=%PATHEXT%;.plx
After taking these steps, you can execute your script from the command line as follows:
> myscript
The ActivePerl distribution includes a pl2bat utility for converting Perl scripts into batch files. You can also run the interpreter by itself from the command line. This is often useful to execute short snippets of code:
perl –e ‘code’
Alternatively, you can run the interpreter in “debugging” mode to obtain a shell-like environment for testing code scraps:
perl –de 1

No comments :

Post a Comment