ru en
Help
Print version

Perl and CGI programs — specifics of usage

Contents

  1. Use of ready-made scripts
  2. Composing a simple CGI script
  3. Viewing installed PERL modules
  4. Installation of additional PERL modules
  5. Possible errors

1. Use of ready-made scripts

If you already possess composed CGI scripts, before copying them to server you should undertake the following actions:

  • Ensure that the correct path to interpreter is indicated in scripts:
    • Perl: /usr/bin/perl
    • Python: /usr/local/bin/python
  • If your script requires access to the MySQL database, it is required to indicate access settings:
  • In hosting control panel "Web server" — "Modules management" enable CGI module

Now you may copy your scripts to the server.

Files should be loaded to directory your_domain/cgi. Files from this directory will be accessible at http://your_domain/cgi-bin/file_name.

To enable launching of CGI scripts from the website root directory your_domain/docs, you should create .htaccess file with the following contents:

AddHandler cgi-script .cgi .pl .py
Options +ExecCGI 

Scripts shall be assigned with execution attribute (access rights 755 or -rwxr-xr-x).

Attributes can be changed through the file manager of the control panel.

2. Composing simple CGI script

Let's consider composing simple CGI script on Perl.

If you use Windows OS, working with scripts code requires a specialized text editor, for example, Notepad++. Windows standard Notepad program is a poor alternative. To demonstrate operation of CGI script it is required to create two files. The first file is an html document with text entry form:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> <title>Example of work with Perl</title> </head>
<body> <form method='post' action='/cgi-bin/hello.pl'> Enter your name:<input type='text' name='name'> <input type='submit'> </form> </body>
</html>

The second file is a CGI script

#!/usr/bin/perl 
 use CGI; 
 print "Content-type: text/html\n\n"; 
 $my_cgi = new CGI; 
 $your_name = $my_cgi->param('name'); 
print "Hello $your_name!!!"; 

You may place the first file in directory your_domain/docs. The second file must be placed in directory your_domain/cgi. Make sure you check the rights to your CGI script. They shall be set to -rwxr-xr-x or 755 value.

3. Viewing installed PERL modules

To check installed PERL modules it is required to perform the following actions in a consistent manner:

  • Using ssh protocol, log in to web server and run command:
    vim modules.pl

    This way file modules.pl will be created.

  • Now it should be edited, therefore click Insert and write the following:
    #!/usr/bin/perl -w
    
    use ExtUtils::Installed;
    
    $installed = ExtUtils::Installed->new();
    foreach $module ($installed->modules()){ 
       printf "Module: %s\t\tVersion: %s\n", $module, $installed->version($module);
    } 

    To exit editor with the saved text, click on Esc and then :wq

  • The installed modules may be viewed:
    perl ./modules.pl

4. Installation of additional PERL modules

You may install to hosting additional PERL modules from the source codes. Before starting installation, connect the latest version of PHP in section "Web server" — "Modules management" of the hosting control panel. In this case the updated software toolkit that may include the desired Perl module will be connected.

Before the installation you should set the environment variables to make the installed modules available for the interpreter.

  • For this, connect to hosting via SSH and create in home directory file .bashrc using command:
    cat >> ~/.bashrc << "EOF"
    PERL5LIB=$HOME/PERL/lib:$HOME/PERL/lib/perl5
    export PERL5LIB
    
    MANPATH=$HOME/PERL/share/man
    export MANPATH
    EOF
  • In order for the changes to take effect, run command:
    source ~/.bashrc

To install the selected module for PERL, the following steps are required:

  • on page http://search.cpan.org/ find the required module, for example, Net::SMPP and copy link for loading archive with the source codes of the module,
  • connect to hosting via SSH and go to directory for temporary files
    cd ~/tmp
  • load archive to hosting by the found link using wget
    wget http://search.cpan.org/CPAN/authors/id/S/SA/SAMPO/Net-SMPP-1.12.tar.gz
  • unpack archive and go to directory with the source codes of the module
    tar -xf Net-SMPP-1.12.tar.gz
    cd Net-SMPP-1.12

Installation should be made to a separate directory, for example, /home/login/PERL, where login — hosting identifier by specifying variable INSTALL_BASE.

  • To install module using file Makefile.PL use the following commands:
    perl Makefile.PL INSTALL_BASE=$HOME/PERL
    make
     
    make install
  • To install module using file Build.PL use the following commands:
    perl Build.PL
    ./Build --install_base $HOME/PERL
    ./Build install --install_base $HOME/PERL

To use the installed modules in perl script you should connect them by adding the following lines in script file:

use lib "/home/login/PERL/lib"; 
use lib "/home/login/PERL/lib/perl5";
use Net::SMPP;

To allow operation of Apache web server with the additional modules, you should in hosting control panel enable module env_module and add the following lines into file .htaccess in the website root directory or in directory with CGI scripts:

SetEnv PERL5LIB  /home/login/PERL/lib:/home/login/PERL/lib/perl5

where login is hosting identifier.

5. Possible errors

Error 403

If you see 403 error when addressing the script, it means incorrect access rights have been assigned to script. CGI scripts shall be assigned with execution attribute (access rights755 or -rwxr-xr-x). Attributes can be changed through the file manager of the control panel.

Error 500

If you see 500 error when running your script, it means the script has an error, by which Perl translator is unable to complete its operation. Error may be both syntactic (for example, you forgot to close a quote or bracket), and logical, for example some of your actions caused a zero division operation. To understand the nature of the error, you should view web server log files stored in directory your_domain/logs.

Copyright © 2000-2024 Registrar R01
Information: info@r01.ru
Support: support@r01.ru
Office: 1 Bolshoy Gnezdnikovsky Lane, building 2, Moscow (Tverskaya metro station, entrance No. 9, Voznesensky business center)