garyj has asked for the wisdom of the Perl Monks concerning the following question:

I'm a Perl newbie running IndigoPerl on an Apache server on my Windows system. I also now have CGI.pm running, but despite viewing Stein's webpage, I don't know how to use it. I think it's because I don't understand some basic stuff:

1) Is CGI.pm a scripting tool (akin to an HTML text editor, only for Perl), or just a collection of script modules that I'm to insert as needed in my scripts? If it's the former, how do I start up this tool; if it's the latter, am I to simply cut & paste, or is there an importing function or some such?

2) Is CGI.pm just another (albeit 'best in it's class') collection of scripts, or is it the 'standard' for Perl programming, replacing Berkeley's cgi-lib? I've searched 'CGI library' on search engines, but haven't found much of substance.

3) What exactly is a 'module' and how is it different from any other pre-written code I might add into my script?

Thanks.

Replies are listed 'Best First'.
Re: How do I use CGI.pm?
by talexb (Chancellor) on Jan 04, 2002 at 01:32 UTC
    CGI.pm is a Perl Module that is a tool box of useful routines that work together to simplify various CGI programming functions.

    I'd recommend reading the POD inside the module. It should help a lot. I can also recommend O'Reilly's Writing Apache Modules as informative reading.

    --t. alex

    "Excellent. Release the hounds." -- Monty Burns.

      Hi all,

      Since our fellow monk garyj sounds like he might be rather new to the whole Perl thing let me clarify a bit about this POD thing. (A year ago, I would have had not a clue on how to "read the POD inside the module".)

      On most Unix systems you can type from the command line perldoc CGI.pm and read the POD (Plain Old Documentation) for the module. I believe man CGI will have the same result on many systems. If you are on a system that has ActiveState's ActivePerl distribution - it comes with some excellent 'html-ized' POD documentation. I've gone so far to copy their documentation from a Win2k box to my Linux machine just to have it available.

      This site is itself a splended resource - by going to Super Search and looking for "CGI" or "use CGI" you are bound to find plentiful examples. Additionally by just going to the top of this screen and entering use CGI you'll find countless more examples.

      One last note is that by looking at http://search.cpan.org/ you can perform searches to find every module that has anything to do with CGI.

      Good luck,
      {NULE}
      --
      http://www.nule.org

Re: How do I use CGI.pm?
by seattlejohn (Deacon) on Jan 04, 2002 at 10:46 UTC
    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.

Re: How do I use CGI.pm?
by garyj (Novice) on Jan 04, 2002 at 01:23 UTC
    One more thing I forgot to ask: is there a simple way to search the CGI.pm library for the type of script you need? Stein's site just has a general table of contents.
Re: How do I use CGI.pm?
by moodster (Hermit) on Jan 04, 2002 at 02:48 UTC
    CGI.pm is more or less the standard way of handling cgi in perl, at least if you're writing cgi scripts. I know some people would argue that it's a slow, horrible, piece of code, but for most tasks it's exactly what you need and it takes care of all the things that WILL go wrong if you try to roll your own solution. So, in reply to question 2, yes. No one ever got fired for using CGI.pm.

    Cheers,
    -- moodster

(cLive ;-) Re: How do I use CGI.pm?
by cLive ;-) (Prior) on Jan 04, 2002 at 09:22 UTC
    The docs can be viewed here.

    cLive ;-)