Perl CGI

Perl's CGI module
use strict;   # defends primarily against typographical errors use warnings; # defends primarily against logic and programming errors
 * Start the script
 * 1) !/usr/bin/env -S perl -T

use CGI; use CGI::Carp;
 * Include CGI module

$CGI::POST_MAX = 1024 * 1024; # 1 MB posts $CGI::DISABLE_UPLOADS = 0;   # Do not allow uploads
 * Restrict some CGI settings

delete @ENV{qw(IFS CDPATH ENV BASH_ENV PERL5SHELL)}; $ENV{PATH} = q{/usr/local/bin:/usr/bin/:/bin};
 * Clean environment variables

my $cgi = CGI->new;
 * New CGI object

print $cgi->header, $cgi->start_html(-title  => 'Page Title',                        -bgcolor => '#FFFFFF'), $cgi->h2('A Form:');
 * Start an HTML page

use File::Copy qw(copy); use File::Temp qw(tempfile); my $file_in = $cgi->upload('filefield_name'); if( $file_in ) { # Create a temporary file my ($fh_save, $new_filename) = tempfile('file_name_XXXXX',                                            DIR    => '/tmp/',                                             SUFFIX => '.txt',                                             UNLINK => 1); # Prevent newline translations by Perl binmode($file_in); binmode($fh_save); copy($file_in, $fh_save) or die "Failed to copy $fh_save file: $!\n"; chmod(0600, $new_filename) or die "Failed to modify $fh_save file permissions: $!\n"; print "File $new_filename uploaded.\n"; }
 * Read an uploaded File
 * 1) If we have uploaded a file

print $cgi->Dump;
 * Print all the user supplied values for debug

print $cgi->h2('Environment Variables:'); foreach my $key (sort(keys(%ENV))) { print "$key = $ENV{$key}\n"; }
 * Print all Environment Variables

my @all_form_parameters = $cgi->param; my @checkbox_group_parameters = $cgi->param('checkbox_group_parameter_name'); my $parameter_name = $cgi->param('parameter_name') || '';
 * Read cgi form parameters

my ( $safe_parameter_name ) = ( $parameter_name =~ m/^([A-Za-z0-9_.-]+)$/ );
 * Clean input data

$cgi->delete( 'password', 'repeat_password' );
 * Delete passwords when we no longer need them

$cgi->delete_all;
 * Delete all parameters

print $cgi->end_html;
 * End an HTML page

Writing HTML elements with the CGI module
print $cgi->start_form;
 * Open a element

print $cgi->hidden(-name    => 'hidden_name',                    -value    => 'hidden_value',                    -override => 1);
 * Hidden field

print $cgi->textfield(-name     => 'text_name',                       -value     => 'text_value',                       -size      => 20,                       -maxlength => 20);
 * Text field

print $cgi->password_field(-name     => 'password_name',                            -size      => 20,                            -maxlength => 20);
 * Password field

print $cgi->textarea(-name     => 'textarea_name',                      -value     => 'textarea_value',                      -rows      => 2,                      -columns   => 20);
 * Text area

print $cgi->checkbox(-name   => 'checkbox_name',                      -value   => 'checkbox_value',                      -label   => 'checkbox_label',                      -checked => 1);
 * Checkbox

print $cgi->checkbox_group(-name      => 'checkbox_group_name',                            -values     => [ qw( checkbox_group_value1 checkbox_group_value2 checkbox_group_value3 ) ],                            -labels     => { checkbox_group_value1 => 'checkbox_group_label1',                                             checkbox_group_value2 => 'checkbox_group_label2',                                             checkbox_group_value3 => 'checkbox_group_label3' },                            -attributes => {},                            -default    => [ qw( checkbox_group_value2 ) ],                            -disabled   => [ qw( checkbox_group_value3 ) ],                            -linebreak  => 1);
 * Checkbox group

print $cgi->radio_group(-name      => 'radio_group_name',                         -values     => [ qw( radio_group_value1 radio_group_value2 ) ],                         -labels     => { radio_group_value1 => 'radio_group_label1',                                          radio_group_value2 => 'radio_group_label2' },                         -attributes => {},                         -default    => 'radio_group_value2',                         -linebreak  => 1);
 * Radio button group

print $cgi->popup_menu(-name      => 'popup_menu_name',                        -values     => [ qw( popup_menu_value1 popup_menu_value2 ) ],                        -labels     => {},                        -attributes => {},                        -default    => 'popup_menu_value2');
 * Popup menu

print $cgi->scrolling_list(-name      => 'scrolling_list_name',                            -values     => [ qw( scrolling_list_value1 scrolling_list_value2 ) ],                            -labels     => { scrolling_list_value1 => 'scrolling_list_label1',                                             scrolling_list_value2 => 'scrolling_list_label2' },                            -attributes => {},                            -default    => [ qw( scrolling_list_value2 ) ],                            -multiple   => 1,                            -size       => 2);
 * Scrolling list

print $cgi->defaults('defaults'), $cgi->submit(-name => 'submit_name',                    -value => 'submit_value');
 * Defaults and Submit button

print $cgi->end_form;
 * Close a element

print $cgi->start_multipart_form; print $cgi->filefield(-name     => 'filefield_name',                       -value     => 'Please enter a file name',                       -size      => 30,                       -maxlength => 60); print $cgi->br; print $cgi->defaults('defaults'), $cgi->submit(-name => 'upload_submit_name',                    -value => 'upload_submit_value'); print $cgi->end_form;
 * File field

print $cgi->hr; print $cgi->a( {-href => ' http://perltraining.com.au/notes.html '},               'Print an anchor element.');
 * Print an anchor element

print $cgi->start_ul; foreach my $li_name ( qw/li_name1 li_name2/ ) {    print $cgi->li($li_name); } print $cgi->end_ul;
 * Print a list element

print $cgi->start_table( {-border => 1, -width => '50%', -align => 'LEFT'} ), $cgi->caption( $cgi->strong('Relational and Equality Operators') ), $cgi->Tr( {-align => 'LEFT', -valign => 'CENTER'},                  [                       $cgi->th( [ qw(Test Numbers Strings) ] ),                       $cgi->td( [ '$x is equal to $y', '$x == $y', '$x eq $y' ]),                       $cgi->td( [ '$x is not equal to $y', '$x != $y', '$x ne $y' ]),                       $cgi->td( [ '$x is greater than $y', '$x > $y', '$x gt $y' ]),                       $cgi->td( [ '$x is greater than or equal to $y', '$x >= $y', '$x ge $y' ]),                       $cgi->td( [ '$x is less than $y', '$x < $y', '$x lt $y' ]),                       $cgi->td( [ '$x is less than or equal to $y', '$x <= $y', '$x le $y' ])                   ]             ), $cgi->end_table;
 * Print a table element

print $cgi->table( {-border => 1, -width => '50%', -align => 'LEFT'},            $cgi->caption( $cgi->strong('Logical Operators') ),             $cgi->Tr( {-align => 'LEFT', -valign => 'CENTER'}, [                      $cgi->th( [ qw(Operator Example Explanation) ] ), $cgi->td( [ '&&', 'condition1 && condition2', 'True if condition1 and condition2 are both true' ] ), $cgi->td( [ '||', 'condition1 || condition2', 'True if either condition1 or condition2 is true' ] ), $cgi->td( [ 'and', 'condition1 and condition2','Same as && but lower precedence' ] ), $cgi->td( [ 'or', 'condition1 or condition2','Same as || but lower precedence' ] ) ]            )       );
 * Print a second table element