CGI.pm isn't really a standalone tool; it's a collection of stuff you can use to make CGI programming in Perl quite a bit easier. In general, Perl modules, which by convention end in .pm, allow you to load functions that you can then call from your own script. (Modules are also the mechanism typically used for creating classes if you're doing object-oriented programming, which you may or may not care about at this point.) If you put a line like this in your script:
use CGI;
Perl will look for a module called CGI.pm and run its contents through the interpreter before continuing. If you installed the module in a fairly standard location, Perl will probably find it without any further effort; if not, there are a few ways to control the directories that Perl looks in. One easy way is to add this earlier in your program:
use lib '/path/to/modules';

To answer the latter part of your question #1: No, you don't need to cut and paste at all. The use CGI line does the importing for you. But some modules don't export stuff by default, and you may need to specify exactly what you want when you use the module. Usually you can get the details from the module's documentation:
perldoc CGI

Debugging CGI scripts can be kind of tricky, so I'd also suggest getting your hands on the CGI::Carp module and adding this line to your scripts as well:
use CGI::Carp qw(fatalsToBrowser);
This will cause many run-time errors to send error messages to your browser rather than leaving you with an uninformative 500 Internal Server Error page and forcing you to search through the server log for details.

To help you get started, here's a sort of skeletal CGI script that you can fill in with actual logic:

#!/usr/bin/perl -w -T # Change the above line to the location of your Perl executable use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); my $query = new CGI; print $query->header(); print "Hello world!"; exit;

If you save this in Apache's /cgi-bin/ directory (or whatever you've configured to hold your executable scripts) as hello.pl, you should then be able to use your Web browser to visit http://your.server.address/cgi-bin/hello.pl and see "Hello world" appear in your browser.

Also, you asked about cgi-lib. I haven't used it personally, but it's my understanding that cgi-lib is basically an archiac way to get much of the functionality that's available in CGI.pm.

For general information about modules, try:
perldoc -q module
at your command prompt.

Best of luck.


In reply to Re: How do I use CGI.pm? by seattlejohn
in thread How do I use CGI.pm? by garyj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.