Toolserver:Perl

From mediawiki.org

This page was moved from the Toolserver wiki.
Toolserver has been replaced by Toolforge. As such, the instructions here may no longer work, but may still be of historical interest.
Please help by updating examples, links, template links, etc. If a page is still relevant, move it to a normal title and leave a redirect.

Installing modules locally[edit]

As per river on toolserver-l, it is best to just fill in a bugreport at JIRA project TS to have modules installed server-wide for everyone.

Otherwise, if there is some good reason you can install them for yourself under your home directory with cpanminus. Note that if you install Perl modules this way, they will not be automatically upgraded by system administrators. This means, for example, if you locally install a Perl module that uses XS, then the system Perl itself is upgraded to a new major version (5.14 coming up in 2011), Perl will crash out when attempting to load the now incompatible module.

Step 1: Add these lines to your ~/.bash_profile:

 export PATH=$PATH:~/bin
 export PERL5LIB=$PERL5LIB:~/lib/perl5
 export MANPATH=$MANPATH:~/man

and change the following two values in your ~/.cpan/CPAN/MyConfig.pm

 'make' => q[make],
 'make_install_make_command' => q[make],

Step 2: Save, then logout, then log back in, so the new environment variables are set as intended.

These steps need to be done only once.

From then on, install the module(s) you want by running the following command on a Solaris host:

 $ cpanm -l ~ Chocolate::Belgian Foo::Bar

If you use CGI scripts in perl you have to add the library path in your home directory to @INC because CGI scripts do not parse your ~/.bash_profile:

 use lib '/home/username/lib/perl5';

FastCGI[edit]

The following code shows how to create Perl scripts that use FastCGI via the CGI::Fast module.

#!/usr/bin/perl
use Data::Dumper;
use CGI::Fast;
my $cgi = CGI::Fast->new();
print CGI::header(-type=>'text/html', -charset=>'utf-8');
my $Opts = $cgi->Vars();
print Dumper($Opts);

Note that although CGI::Fast is installed on the web server, it may not be installed on the toolserver login servers. It is available from CPAN.

The code above ignores the main benefit of FastCGI, which is that a single instance of the script can serve multiple web requests. The following code demonstrates how to achieve this.

#!/usr/bin/perl
use Data::Dumper;
use CGI::Fast;
my $cgi;
my $counter = 0;
while ( $cgi = CGI::Fast->new() ) {
  $counter++;
  print CGI::header(-type=>'text/html', -charset=>'utf-8');
  my $Opts = $cgi->Vars();
  print "This PID ($$) has served $counter requests<br/>\n";
}

Miscellany[edit]

  • Why do I get an error 500 if I try to run my Perl CGI script?
Use the short checklist or the long checklist to find the answer.

Category:Programming languages