Perl CGI examples



  1. Introduction
  2. To run these scripts
  3. Basics
  4. Scalars
  5. Conditionals
  6. Form data
  7. File Handling
  8. Arrays
  9. Hashes
  10. Printing
  11. Regular Expressions
  12. Remember
  13. Security
  14. Subroutines
  15. Debug
  16. Modules

See Also

  1. Examples



This page contains a listing of CGI-BIN scripts written in Perl.


To run these scripts

  1. Drop the scripts into to the cgi-bin directory of your web server.

  2. ssh to your server and cd to your cgi-bin directory

  3. For each *.cgi file run:
    chmod 755 scriptname.cgi

  4. Determine the directory path to your Perl interpreter, run:

    which perl

    Compare 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.

  5. Edit each *.html file and change any directory paths that are incorrect.



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



Run Code
Concatenate two scalars concat.cgi
Multiply numbers repeat.cgi
Divide numbers taxdeduc.cgi
Increment a number increment.cgi
Raise a number to the nth power exponents.cgi
Get remainder modulus.cgi
Calculate with parenthesis preference.cgi



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

Run Code
Read from a file readfromlog.cgi
Get comments, then write to file chomp.cgi
Print key values printkeyvalues.cgi
Get exclusive access to a file flock.cgi
Send e-mail formmail.cgi
Make a directory mkdir.cgi
View contents of a directory opendir.cgi
Rename a file rename.cgi
Write to a file writetolog.cgi
chmod a file chmod.cgi



Run Code
Display array.cgi
Display selected elements getday.cgi
Process data from forms split.cgi
Slice getmultipledays.cgi
Transpose gettoday.cgi
Get the number of elements length.cgi
Peform math functions modifyall.cgi
Sort sort.cgi
Reverse sort reverse.cgi
Add an element push.cgi
Add multiple elements combine.cgi
Add an element unshift.cgi
Remove the first element shift.cgi
Remove the last element pop.cgi
Replace an element replaceone.cgi
Replace multiple elements replacemore.cgi



Run Code
Remove a key-value pair delete.cgi
Get each key and value in a hash each.cgi
Check to see if a key exists exists.cgi
Get several values using keys getmore.cgi
Get a value using a key getone.cgi
Print all keys and values hash.cgi
Get all of a hash's keys keys.cgi
Get all values using keys keysvalues.cgi
Get all of a hash's values values.cgi



Run Code
Base base.cgi
EOF eof.cgi
Format format.cgi
Format HTML formathtml.cgi
Format Proper formatproper.cgi
Format w/Subs formatwsubs.cgi
Get Time gettime.cgi
HTML Formatting htmlformatting.cgi
List list.cgi


Regular Expressions

Run Code
alternation alternation.cgi
Anchor anchor.cgi
Asterisk asterisk.html
Class class.cgi
Class Short classshort.cgi
Mand mandn.cgi
Match match.cgi
Match One matchone.cgi
Neg Class negclass.cgi
Optional optional.cgi
Plus plus.cgi
Sequence sequence.cgi
Splitr splitr.cgi
Substitute substitute.cgi
CGI Text use2.cgi
Using html using.cgi



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



Run Code
badsystem system.cgi
goodreferer referer.cgi
referer referer.cgi
system system.cgi



Run Code
arguments arguments.cgi
manretBAD manretBAD.cgi
manreturn3 manreturn3.cgi
return return.cgi
simple simple.cgi



Run Code
Handle an open file error errortest.cgi
Print browser info extra_printing.cgi



Check out the CPAN module list for an extensive library of pre-written functions.

You can use CPAN to install modules.

  1. Log in as root

  2. Using browser, retrieve yourmodule.

  3. cd /usr/lib/perl5/5.6.0

  4. Decompress and unpack file

    gzip -dc modulename.gz | tar -xof -

  5. Go into the newly-created directory and type:

    perl Makefile.PL
    make test
    make install