Perl Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
Running Perl Scripts on Linux Systems
This section provides a tutorial example on how to run Perl scripts on Linux systems. To make a Perl script file executable, you need to add '#!/usr/bin/perl' to the beginning of the script.
There are many ways to run Perl scripts on Linux:
1. Run the "perl" command with the Perl script included in the command line. For example, enter the following command line in a shell window:
/home/herong$ perl -e "print 'Hello world!';" Hello world!
Note that about command will not work in some shell windows. For example, in a "bash" shell window, you will get the following error, because the "!" is a reserved character to access an event in the command history.
/home/herong$ perl -e "print 'Hello world!';" -bash: !': event not found
To avoid the problem, you can run the above command a "sh" command window:
/home/herong$ sh $ perl -e "print 'Hello world!';" Hello world!$
Or you can protect "!" using escape sequence:
/home/herong$ perl -e "print \"Hello world\!\n\";" Hello world!
Or you can use single quotes ('...') to protect the entire Perl script from the shell:
/home/herong$ perl -e 'print "Hello world!\n";' Hello world!
Here is another example of running a Perl script in a single command line:
/home/herong$ perl -e "for (\$i=0; \$i<10; \$i++) {print \"\$i\n\";}" 0 1 2 3 4 5 6 7 8 9
Note that dollar sign ($) used for Perl scalar variables are also shell reserved characters, we need to use escape sequences to protect them.
Also note that double quote (") is used to put the entire script code as one command line parameter. Any double quote inside the program needs to be protected as (\").
Or you can use single quotes ('...') to protect the entire Perl script from the shell:
/home/herong$ perl -e 'for ($i=0; $i<10; $i++) {print "$i\n";}' 0 1 2 3 4 5 6 7 8 9
Including Perl script in the command line is quick and easy. But you can only run scripts that are small enough to fit into one command line.
2. Run the "perl" command with the Perl script supplied from the standard input stream. For example, enter "perl" in a command window. Then enter the script code followed by Control-D, which is the End Of File (EOF) indicator:
/home/herong$ perl $s=0; for ($i=0; $i<10; $i++) { $s+=$i; } print "$s\n"; ^D 45
Obviously, you can enter a much longer script in this way. But the program is not save permanently.
3. Run the "perl" command with the Perl script supplied in a file. For example, enter the following Perl script in a file called hello.prg:
print "Hello world!\n";
Then enter the following command in a command window:
/home/herong$ perl hello.prg Hello world!
4. Run Perl script files as commands. You can do this, only if you insert a special line at the beginning of your script file: #!/usr/bin/perl, and assign execution permission to the script file. This special line represents the Perl installation location on the file system.
For example, enter the following script in a file called hello.pl:
#!/usr/bin/perl print "Hello world!\n";
Then assign execution permission and enter the script file name to run it:
/home/herong$ chmod a+x hello.pl /home/herong$ ./hello.pl Hello world!
It works! And this is the best way to run Perl scripts on Linux systems.
Table of Contents
Perl Installation on Linux Systems
►Running Perl Scripts on Linux Systems
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
Hard References - Addresses of Memory Objects
Objects (or References) and Classes (or Packages)
Typeglob and Importing Identifiers from Other Packages
String Built-in Functions and Performance
File Handles and Data Input/Output
Open Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
SOAP::Lite - SOAP Server-Client Communication Module
Perl Programs as IIS Server CGI Scripts
CGI (Common Gateway Interface)
XML-RPC - Remote Procedure Call with XML and HTTP
RPC::XML - Perl Implementation of XML-RPC
Integrating Perl with Apache Web Server
CGI.pm Module for Building Web Pages
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary