Running Perl Scripts


The quickest way to run a command in Perl is on the command line. The -e switch is used to run one statement at a time. The statement must be enclosed in single quotes:

 $ perl -e 'print "Hello, world\n";' Hello, world

Although there are many useful perl one-liners, this is not going to get you far with the language.

A more common way to use Perl is to create a script by entering lines of Perl code in a file. You can then use the perl command to run the script:

 $ cat hello print "Hello, world\n"; $ perl hello Hello, world

As you can see, the Perl function print sends a line of text to standard output. The “\n” adds a newline at the end.

You can also create scripts that automatically use perl when they run. To do this, add the line #!/usr/bin/perl (or whatever the path for perl is on your system-use which perl to find out) to the top of your file. This instructs the shell to use /usr/bin/perl as the interpreter for the script. You will also need to make sure that you have execute permission for the file. You can then run the script by typing its name:

 $ cat hello.pl #!/usr/bin/perl print "Hello, world\n"; $ chmod u+x hello.pl $ ./hello.pl Hello, world

If the directory containing the script is not in your PATH, you will need to enter the pathname in order to run the script. In the preceding example, ./hello.pl was used to run the script in the current directory

The extension .pl is commonly given to Perl scripts. Although it is not required, using .pl when you name your Perl scripts can help you organize your files.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net