Jump to content

CGI.pm: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Added short description
Tags: Mobile edit Mobile app edit iOS app edit App description add
 
(25 intermediate revisions by 22 users not shown)
Line 1: Line 1:
{{Short description|Perl module for web applications}}
{{primary sources|date=September 2011}}
{{primary sources|date=September 2011}}
{{one source |date=April 2024}}
{{Infobox software
{{Infobox software
| name = CGI.pm
| name = CGI.pm
| logo =
| logo =
| screenshot =
| screenshot =
| caption =
| caption =
| author = [[Lincoln Stein]]
| author = [[Lincoln Stein]]
| developer = Mark Stosberg
| developer = Lee Johnson
| released =
| released =
| latest release version = 3.49
| latest release version = 4.21
| latest release date = 2010-02-05
| latest release date = 2015-06-22
| latest preview version =
| latest preview version =
| latest preview date =
| latest preview date =
Line 15: Line 17:
| platform = [[Perl]]
| platform = [[Perl]]
| language =
| language =
| status =
| genre = [[Perl module]] for [[Common Gateway Interface|CGI]]
| genre = [[Perl module]] for [[Common Gateway Interface|CGI]]
| license =
| license =
| website = {{URL|https://metacpan.org/release/CGI}}
| website = {{URL|https://metacpan.org/release/CGI}}
}}
}}
'''CGI.pm''' is a large and widely used [[Perl module]] for [[Computer programming|programming]] [[Common Gateway Interface]] (CGI) [[WWW|web]] applications, providing a consistent [[Application programming interface|API]] for receiving user input and producing [[HTML]] or [[XHTML]] output.


'''CGI.pm''' is a large and once widely used [[Perl module]] for [[Computer programming|programming]] [[Common Gateway Interface]] (CGI) [[WWW|web]] applications, providing a consistent [[Application programming interface|API]] for receiving and processing user input. There are also functions for producing [[HTML]] or [[XHTML]] output, but these are now unmaintained and are to be avoided.<ref name="auto">{{Cite web|url=https://metacpan.org/dist/CGI/view/lib/CGI.pod|title=CGI - Handle Common Gateway Interface requests and responses - metacpan.org|website=metacpan.org}}</ref> CGI.pm was a core Perl module but has been removed as of v5.22 of Perl.<ref name="auto"/> The module was written by [[Lincoln Stein]] and is now maintained by Lee Johnson.
The module was written by [[Lincoln Stein]] and is now maintained by Mark Stosberg.


== Examples ==
== Examples ==
Here is a simple CGI page, written in Perl using CGI.pm (in [[Object-oriented programming|object-oriented]] style):
Here is a simple CGI page, written in Perl using CGI.pm (in [[Object-oriented programming|object-oriented]] style):


<source lang="perl">
<syntaxhighlight lang="perl">
#!/usr/bin/env perl

use strict;
use warnings;

use CGI;
use CGI;


my $cgi = CGI->new();
my $cgi = CGI->new;


print $cgi->header('text/html');
print
$cgi->header('text/html'),
$cgi->start_html('A Simple CGI Page'),
$cgi->h1('A Simple CGI Page'),
$cgi->start_form,
'Name: ',
$cgi->textfield('name'), $cgi->br,
'Age: ',
$cgi->textfield('age'), $cgi->p,
$cgi->submit('Submit!'),
$cgi->end_form, $cgi->p,
$cgi->hr;


print << "EndOfHTML";
if ( $cgi->param('name') ) {
<!DOCTYPE html>
print 'Your name is ', $cgi->param('name'), $cgi->br;
<html>
<head>
<title>A Simple CGI Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>A Simple CGI Page</h1>
<form method="post" enctype="multipart/form-data">
Name: <input type="text" name="name" /><br />
Age: <input type="text" name="age" /><p>
<input type="submit" name="Submit!" value="Submit!" />
</form>
<hr />
EndOfHTML

if ( my $name = $cgi->param('name') ) {
print "Your name is $name.<br />";
}
}


if ( $cgi->param('age') ) {
if ( my $age = $cgi->param('age') ) {
print 'You are ', $cgi->param('age'), ' years old.';
print "You are $age years old.";
}
}


print $cgi->end_html;
print '</body></html>';
</syntaxhighlight>
</source>


This would print a very simple webform, asking for your name and age, and after having been submitted, redisplaying the form with the name and age displayed below it. This sample makes use of CGI.pm's object-oriented abilities; it can also be done by calling functions directly, without the <tt>$cgi-></tt>.
This would print a very simple webform, asking for your name and age, and after having been submitted, redisplaying the form with the name and age displayed below it. This sample makes use of CGI.pm's object-oriented abilities; it can also be done by calling functions directly, without the {{mono|$cgi->}}, however the necessary functions must be imported into the namespace of the script that requires access to those functions:


<syntaxhighlight lang="perl">
Note: in many examples $q, short for query, is used to store a CGI object. As the above example illustrates, this might be very misleading.
#!perl


use strict;
Here is another script that produces the same output using CGI.pm's [[Procedural programming|procedural]] interface:
use warnings;
use CGI qw/ :standard /;


print header('text/html');
<source lang="perl">
use CGI ':standard';


# ... HTML output same as above example
print header,
start_html('A Simple CGI Page'),
h1('A Simple CGI Page'),
start_form,
'Name: ',
textfield('name'), br,
'Age: ',
textfield('age'), p,
submit('Submit!'),
end_form, p,
hr;


print 'Your name is ', param('name'), br if param 'name';
if ( my $name = param('name') ) {
print "Your name is $name.<br />";
print 'You are ', param('age'), ' years old.' if param 'age';
}


if ( my $age = param('age') ) {
print end_html;
print "You are $age years old.";
</source>
}


print '</body></html>';
An astuce to write CSS perls' variables in chevrons (<<)
</syntaxhighlight>
<source lang="perl">
my($CSS_OK)='';
my($txt_Title) = 'INPUT_TITLE';
my($txt_content) = 'INPUT_CONTENT';
my($default_textSize) = '2.1';


Note: in many examples {{var|$q}}, short for query, is used to store a CGI object.
$CSS_OK = <<CSSOK;

/* 1 - the way to interdict perl to interpret a CSS variable. */
\@keyframes mymove2 {
50% {transform:rotate(5deg);}
}

/* 2 - to replace le value of $txt_titre HTML ID for CSS ID
- This one gives : #INPUT_TITLE {
---------------------------------------
*/ \#$txt_Title { /*mean: #INPUT_TITLE. !!! note the space between "Title" and "{" */
font-size:20pt;
}

/* 3 - Idem to interpret a perl value into CSS Class variable.
- This one gives : .INPUT_CONTENT {
---------------------------------------
*/ \.$txt_content {
font-size: $default_textSize\pt;
/* hear perl take the content of $default_textSize, you must cut from "pt" with anti-slash */
}
CSSOK

the CSS variable can be include as it:
-------------------------------------
print $query->header(-type => 'text/html',
-expires=>'+3d',
-style=>{'code'=>$CSS_OK};
);
<ref>ME</ref>


==See also==
==See also==
*[[Mod perl|mod_perl]]
*[[mod_perl]]

==References==
{{Reflist}}


==External links==
==External links==
{{Wikibooks|Perl Programming|CGI}}
*[http://stein.cshl.org/WWW/software/CGI/ Official homepage]
*[https://metacpan.org/module/CGI CGI.pm] &ndash; at the [[CPAN]]
*[https://metacpan.org/module/CGI CGI.pm] &ndash; at the [[CPAN]]


[[Category:Perl modules]]
{{Wikibooks|Perl Programming|CGI}}


{{unix-stub}}


{{unix-stub}}
[[Category:Perl modules]]

Latest revision as of 02:53, 17 October 2024

CGI.pm
Original author(s)Lincoln Stein
Developer(s)Lee Johnson
Stable release
4.21 / 2015-06-22
PlatformPerl
TypePerl module for CGI
Websitemetacpan.org/release/CGI

CGI.pm is a large and once widely used Perl module for programming Common Gateway Interface (CGI) web applications, providing a consistent API for receiving and processing user input. There are also functions for producing HTML or XHTML output, but these are now unmaintained and are to be avoided.[1] CGI.pm was a core Perl module but has been removed as of v5.22 of Perl.[1] The module was written by Lincoln Stein and is now maintained by Lee Johnson.

Examples

[edit]

Here is a simple CGI page, written in Perl using CGI.pm (in object-oriented style):

#!/usr/bin/env perl

use strict;
use warnings;

use CGI;

my $cgi = CGI->new;

print $cgi->header('text/html');

print << "EndOfHTML";
<!DOCTYPE html>
<html>
    <head>
        <title>A Simple CGI Page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
        <h1>A Simple CGI Page</h1>
        <form method="post" enctype="multipart/form-data">
            Name: <input type="text" name="name"  /><br />
            Age: <input type="text" name="age"  /><p>
            <input type="submit" name="Submit!" value="Submit!" />
        </form>
        <hr />
EndOfHTML

if ( my $name = $cgi->param('name') ) {
    print "Your name is $name.<br />";
}

if ( my $age = $cgi->param('age') ) {
    print "You are $age years old.";
}

print '</body></html>';

This would print a very simple webform, asking for your name and age, and after having been submitted, redisplaying the form with the name and age displayed below it. This sample makes use of CGI.pm's object-oriented abilities; it can also be done by calling functions directly, without the $cgi->, however the necessary functions must be imported into the namespace of the script that requires access to those functions:

#!perl

use strict;
use warnings;
use CGI qw/ :standard /;

print header('text/html');

# ... HTML output same as above example

if ( my $name = param('name') ) {
    print "Your name is $name.<br />";
}

if ( my $age = param('age') ) {
    print "You are $age years old.";
}

print '</body></html>';

Note: in many examples $q, short for query, is used to store a CGI object.

See also

[edit]

References

[edit]
  1. ^ a b "CGI - Handle Common Gateway Interface requests and responses - metacpan.org". metacpan.org.
[edit]