Perl CGI examples
Content
- Introduction
- To run these scripts
- Basics
- Scalars
- Conditionals
- Form data
- File Handling
- Arrays
- Hashes
- Printing
- Regular Expressions
- Remember
- Security
- Subroutines
- Debug
- Modules
See Also
Introduction
This page contains a listing of CGI-BIN scripts written in Perl.
To run these scripts
- Drop the scripts into to the cgi-bin directory of your web server.
- ssh to your server and cd to your cgi-bin directory
- For each *.cgi file run:
chmod 755 scriptname.cgi
- Determine the directory path to your Perl interpreter, run:
which perlCompare the output to the first line of each *.cgi file. If the paths are different, change the first line of each offending script to conform to the command output.
- Edit each *.html file and change any directory paths that are incorrect.
Basics
Run Code Scalar Assignment inputself.cgi Array assignment days.cgi Loop through an array chores.cgi Print browser info printbrowser.cgi Parse Form Fields parseform.cgi
Scalars
Conditionals
Run Code do while dowhile else else elsif elsif for for foreach foreach if if nest nest unless unless until until while while
Form data
Run Code Radio Buttons cityinfo.cgi Parse form with foreach namevalue.cgi Get Time gettime.cgi Get Local Time getlocaltime.cgi
File Handling
Arrays
Hashes
Printing
Regular Expressions
Remember
Run Code domain domain.cgi expires expires.cgi hidden1 hidden1.cgi hidden2 hidden2.cgi path path.cgi readcookie readcookie.cgi secure secure.cgi sendcookie sendcookie.cgi
Security
Run Code badsystem system.cgi goodreferer referer.cgi referer referer.cgi system system.cgi
Subroutines
Run Code arguments arguments.cgi manretBAD manretBAD.cgi manreturn3 manreturn3.cgi return return.cgi simple simple.cgi
Debug
Run Code Handle an open file error errortest.cgi Print browser info extra_printing.cgi
Modules
Check out the CPAN module list for an extensive library of pre-written functions.
You can use CPAN to install modules.
- Log in as root
- Using browser, retrieve yourmodule.
- cd /usr/lib/perl5/5.6.0
- Decompress and unpack file
gzip -dc modulename.gz | tar -xof -- Go into the newly-created directory and type:
perl Makefile.PL
make
make test
make install