Perl is an interpreted programming language that is
similar in syntax to C and includes a number of popular UNIX facilities such as
SED, awk, and tr. Perl has become the preferred scripting language for most of
the CGIs currently in use on the Web.
The VPS v2 Virtual Server has the Perl 5 standard libraries installed.
The first line in the env.cgi file is #!/usr/local/bin/perl, so the Perl5 binary is
used for the script. Perl can also take command line options, which can be
useful in debugging scripts. They can also be included on the first line of
your script. For example, the following causes Perl to check the syntax of the
script:
#!/usr/local/bin/perl -c
The following forces Perl to look in the
/usr/local/lib/perl5 directory for include files:
#!/usr/local/bin/perl -I /usr/local/lib/perl5
The following forces Perl to print warnings about unsafe
or poor programming practices. Always run with the –w flag:. It is considered good form in the Perl
community. Also, “use strict” will help you avoid bizarre errors caused by
typos or forgetfulness.
#!/usr/local/bin/perl –w
#use strict
Note: When a script does
not work properly, use the -w and -c options to help debug by generating warnings
and check for syntax errors. Also, check your Web server error log files for
errors.
Checking Your Server's Error Log Files
To check the server log files, type:
% cd /www/logs
% tail –f error_log
Common Scripting Problems and Solutions
Some common problems that can occur with Perl scripts are
described, followed by their solutions.
Failure to Upload Your Perl Script in ASCII Mode
Perl scripts, unlike compiled executables, are plain text
files. Failure to transfer your Perl scripts to your VPS v2 Virtual Server in ASCII mode may
result in 500 Server Errors.
Solution
Plain text files should be transferred from your local
computer to your VPS v2 Virtual Server using ASCII mode (not binary mode).
Editing Your CGI Script
This action runs your Perl program with the Perl
interpreter rather than perl4, located in /usr/bin/perl.
-
Go to /www/cgi-bin
% cd www/cgi-bin
-
Edit the-cgi file. This example invokes pico.
% pico my-cgi.cgi
-
Change the first line of the script from:
#!/usr/bin/perl
to:
#!/usr/local/bin/perl
Improper Path Specification of Perl Interpreter
The first line of a Perl script indicates the path name of
the Perl interpreter. In the VPS v2 Virtual Server environment, the correct specification of
your Perl5 interpreter is /usr/local/bin/perl.
If you downloaded a Perl script from a third party source, the Perl interpreter
is most often defined based on the author's host environment, which may be
different from the VPS v2 Virtual Server environment. In addition, if you have uploaded a Perl
script to your VPS v2 Virtual Server, ensure that the script includes the proper path definition
to the Perl5 interpreter. The location of the Perl4 interpreter is specified as
/usr/local/bin/perl4, whereas the Perl5
interpreter location should be specified as /usr/local/bin/perl.
A Sample Problem with a Perl Script Module
A module is not found in the Perl script, which is
probably because of a path issue (use or require path not to the correct Perl
module) or the module is not included in the current Perl installation.
Solutions
Any of the following solutions can solve the problem:
-
Put the module in the same directory in which the Perl script is running
and do not path to it (just call it by name with the use or require or other
such syntax).
-
Put the module in the directory where your other modules are stored,
normally /usr/local/lib/perl5/.
-
Add the path to modules you have created or desire to use into the @INC
array. To use this solution, Enetrics Communications suggests the O'Reilly books on Perl.
CPAN
CPAN is the Comprehensive Perl Archive Network, a large
collection of Perl software and documentation. CPAN is also the name of a Perl
module, CPAN.pm, which is used to download and install Perl software from the
CPAN archive. If you require a Perl module that is not included in the Perl
standard libraries, you can use the CPAN module to install it.
% perl -MCPAN –e shell
launches CPAN into interactive mode.
To access online help from interactive mode, type:
% cpan> h
For more information, go to:
See also the CPAN man page.
|