<!DOCTYPE html>
<html class="client-js"><head>
  <meta charset="UTF-8">
  <title>CGI.pm</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script>
    function importScript() { return 1 } // this is to avoid the error from site.js
  </script>
  <script>let articleId = 'CGI.pm'</script>
  <link rel="canonical" href="https://en.wikipedia.org/wiki/CGI.pm">
  <link href="../-/mw/ext.cite.ux-enhancements.css" rel="stylesheet" type="text/css">
<link href="../-/mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="../-/mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="../-/mw/skins.minerva.base.reset|skins.minerva.content.styles|ext.cite.style|site.styles|mobile.app.pagestyles.android|mediawiki.page.gallery.styles|mediawiki.skinning.content.parsoid.css" rel="stylesheet" type="text/css">
  <link href="../-/style.css" rel="stylesheet" type="text/css"><link href="../-/content.parsoid.css" rel="stylesheet" type="text/css"><link href="../-/inserted_style.css" rel="stylesheet" type="text/css">
  <script src="../-/script.js"></script><script src="../-/masonry.min.js"></script><script src="../-/article_list_home.js"></script><script src="../-/images_loaded.min.js"></script><script src="../-/node_module/details-element-polyfill/dist/details-element-polyfill.js"></script>
</head>

<body class="mediawiki mw-hide-empty-elt ns-0 ns-subject stable skin-minerva action-view animations">
  <div id="mw-mf-viewport" class="feature-header-v2">
    <div id="mw-mf-page-center">
      <div id="content" class="mw-body">
        <a id="top"></a>
        <div id="bodyContent" class="content mw-parser-output">
          
          <div id="mw-content-text" style="direction: ltr;"><h1 class="section-heading" tabindex="0" aria-haspopup="true" data-section-id="0">
  <span class="mw-headline" id="title_0">CGI.pm</span>
</h1>
<div id="mf-section-0" class="mf-section-0" aria-pressed="true" aria-expanded="true">
  <p><b>CGI.pm</b> is a large and once widely used <a href="Perl_module" title="Perl module">Perl module</a> for <a href="Computer_programming" title="Computer programming">programming</a> <a href="Common_Gateway_Interface" title="Common Gateway Interface">Common Gateway Interface</a> (CGI) <a href="WWW" title="WWW" class="mw-redirect">web</a> applications, providing a consistent <a href="Application_programming_interface" title="Application programming interface" class="mw-redirect">API</a> for receiving and processing user input. There are also functions for producing <a href="HTML" title="HTML">HTML</a> or <a href="XHTML" title="XHTML">XHTML</a> output, but these are now unmaintained and are to be avoided.<span class="mw-ref reference" id="cite_ref-1"><a href="#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></span> CGI.pm was a core Perl module but has been removed as of v5.22 of Perl.<span class="mw-ref reference" id="cite_ref-2"><a href="#cite_note-2" style="counter-reset: mw-Ref 2;"><span class="mw-reflink-text">[2]</span></a></span> The module was written by <a href="Lincoln_Stein" title="Lincoln Stein">Lincoln Stein</a> and is now maintained by Lee Johnson.</p>


<table class="infobox vevent" style="width:22em"><caption class="summary">CGI.pm</caption><tbody><tr><th scope="row" style="white-space: nowrap;"><a href="Software_developer" title="Software developer" class="mw-redirect">Original author(s)</a></th><td><a href="Lincoln_Stein" title="Lincoln Stein">Lincoln Stein</a></td></tr><tr><th scope="row" style="white-space: nowrap;"><a href="Software_developer" title="Software developer" class="mw-redirect">Developer(s)</a></th><td>Lee Johnson</td></tr><tr style="display: none;"><td colspan="2" style="text-align:center"></td></tr><tr><th scope="row" style="white-space: nowrap;"><a href="Software_release_life_cycle" title="Software release life cycle">Stable release</a></th><td><div style="margin:0px;">4.21
   / 2015-06-22</div></td></tr><tr style="display:none"><td colspan="2">
</td></tr><tr><th scope="row" style="white-space: nowrap;"><a href="Computing_platform" title="Computing platform">Platform</a></th><td><a href="Perl" title="Perl">Perl</a></td></tr><tr><th scope="row" style="white-space: nowrap;"><a href="Software_categories#Categorization_approaches" title="Software categories">Type</a></th><td><a href="Perl_module" title="Perl module">Perl module</a> for <a href="Common_Gateway_Interface" title="Common Gateway Interface">CGI</a></td></tr><tr><th scope="row" style="white-space: nowrap;">Website</th><td><span class="url"><a href="https://metacpan.org/release/CGI" class="external text external">metacpan<wbr>.org<wbr>/release<wbr>/CGI</a></span></td></tr></tbody></table>


</div><details data-level="2" open="">
    <summary class="section-heading"><h2 id="Examples">Examples</h2></summary>
    
<p>Here is a simple CGI page, written in Perl using CGI.pm (in <a href="Object-oriented_programming" title="Object-oriented programming">object-oriented</a> style):</p>

<div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span class="ch">#!/usr/bin/env perl</span>

<span class="k">use</span> <span class="nn">strict</span><span class="p">;</span>
<span class="k">use</span> <span class="nn">warnings</span><span class="p">;</span>

<span class="k">use</span> <span class="nn">CGI</span><span class="p">;</span>

<span class="k">my</span> <span class="nv">$cgi</span> <span class="o">=</span> <span class="nn">CGI</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">;</span>

<span class="k">print</span> <span class="nv">$cgi</span><span class="o">-&gt;</span><span class="n">header</span><span class="p">(</span><span class="s">'text/html'</span><span class="p">);</span>

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

<span class="k">if</span> <span class="p">(</span> <span class="k">my</span> <span class="nv">$name</span> <span class="o">=</span> <span class="nv">$cgi</span><span class="o">-&gt;</span><span class="n">param</span><span class="p">(</span><span class="s">'name'</span><span class="p">)</span> <span class="p">)</span> <span class="p">{</span>
    <span class="k">print</span> <span class="s">"Your name is $name.&lt;br /&gt;"</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">if</span> <span class="p">(</span> <span class="k">my</span> <span class="nv">$age</span> <span class="o">=</span> <span class="nv">$cgi</span><span class="o">-&gt;</span><span class="n">param</span><span class="p">(</span><span class="s">'age'</span><span class="p">)</span> <span class="p">)</span> <span class="p">{</span>
    <span class="k">print</span> <span class="s">"You are $age years old."</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">print</span> <span class="s">'&lt;/body&gt;&lt;/html&gt;'</span><span class="p">;</span>
</pre></div>

<p>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 <style data-mw-deduplicate="TemplateStyles:r886049734">.mw-parser-output .monospaced{font-family:monospace,monospace}</style><span class="monospaced">$cgi-&gt;</span>, however the necessary functions must be imported into the namespace of the script that requires access to those functions:</p>

<div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span class="ch">#!perl</span>

<span class="k">use</span> <span class="nn">strict</span><span class="p">;</span>
<span class="k">use</span> <span class="nn">warnings</span><span class="p">;</span>
<span class="k">use</span> <span class="nn">CGI</span> <span class="sx">qw/ :standard /</span><span class="p">;</span>

<span class="k">print</span> <span class="n">header</span><span class="p">(</span><span class="s">'text/html'</span><span class="p">);</span>

<span class="c1"># ... HTML output same as above example</span>

<span class="k">if</span> <span class="p">(</span> <span class="k">my</span> <span class="nv">$name</span> <span class="o">=</span> <span class="n">param</span><span class="p">(</span><span class="s">'name'</span><span class="p">)</span> <span class="p">)</span> <span class="p">{</span>
    <span class="k">print</span> <span class="s">"Your name is $name.&lt;br /&gt;"</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">if</span> <span class="p">(</span> <span class="k">my</span> <span class="nv">$age</span> <span class="o">=</span> <span class="n">param</span><span class="p">(</span><span class="s">'age'</span><span class="p">)</span> <span class="p">)</span> <span class="p">{</span>
    <span class="k">print</span> <span class="s">"You are $age years old."</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">print</span> <span class="s">'&lt;/body&gt;&lt;/html&gt;'</span><span class="p">;</span>
</pre></div>

<p>Note: in many examples <var style="padding-right: 1px;">$q</var>, short for query, is used to store a CGI object.</p>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="See_also">See also</h2></summary>
    
<ul><li><a href="Mod_perl" title="Mod perl">mod_perl</a></li></ul>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="References">References</h2></summary>
    
<div class="reflist " style=" list-style-type: decimal;">
<div class="mw-references-wrap"><ol class="mw-references references"><li id="cite_note-1"> <span id="mw-reference-text-cite_note-1" class="mw-reference-text"><a href="https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#HTML-Generation-functions-should-no-longer-be-used" class="external free external">https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#HTML-Generation-functions-should-no-longer-be-used</a></span></li><li id="cite_note-2"> <span id="mw-reference-text-cite_note-2" class="mw-reference-text"><a href="https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#CGI.pm-HAS-BEEN-REMOVED-FROM-THE-PERL-CORE" class="external free external">https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#CGI.pm-HAS-BEEN-REMOVED-FROM-THE-PERL-CORE</a></span></li></ol></div></div>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="External_links">External links</h2></summary>
    
<ul><li><a href="https://metacpan.org/module/CGI" class="external text external">CGI.pm</a> <span>–</span> at the <a href="CPAN" title="CPAN">CPAN</a></li></ul>

<table role="presentation" class="mbox-small  sistersitebox" style="background-color:#f9f9f9;border:1px solid #aaa;color:#000">
<tbody><tr>
<td class="mbox-image"><span class="noviewer"><span><img src="../I/Wikibooks-logo-en-noslogan.svg.png.webp" data-file-width="400" data-file-height="400" data-file-type="drawing" height="40" width="40" loading="lazy"></span></span></td>
<td class="mbox-text plainlist">The Wikibook <i><a href="https://en.wikibooks.org/wiki/Perl%20Programming" title="wikibooks:Perl Programming" class="external">Perl Programming</a></i> has a page on the topic of: <i><b><a href="https://en.wikibooks.org/wiki/Perl%20Programming/CGI" title="wikibooks:Perl Programming/CGI" class="external">CGI</a></b></i></td></tr>
</tbody></table>


<p>
<br></p>

    
</details><!--htdig_noindex--><div><div style="clear:both; background-image:linear-gradient(180deg, #E8E8E8, white); border-top: dashed 2px #AAAAAA; padding: 0.5em 0.5em 0.5em 0.5em; margin-top: 1em; direction: ltr;">
    This article is issued from <a class="external text" title="Last edited on 2020-08-10" href="https://en.wikipedia.org/wiki/?title=CGI.pm&amp;oldid=972189903">Wikipedia</a>. The text is licensed under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons - Attribution - Sharealike</a>. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
        </div>
      </div>
    </div>
  </div>
  <script src="../-/mw/jsConfigVars.js"></script>
  <script src="../-/mw/startup.js"></script>
<script src="../-/mw/jquery.js"></script>
<script src="../-/mw/mediawiki.js"></script>
<script src="../-/mw/site.js"></script>
<script src="../-/mw/ext.cite.ux-enhancements.js"></script>



</body></html>