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

I can't get CGI.pm to work. I keep getting these "Undefined subroutine main::param called" errors. Here's my code:
#!/usr/bin/perl use CGI; print "Content-type: text/html\n\n"; print "<html><head><title>CGI test</title></head><body>"; $foo = param('foo'); print "foo is $foo.</body></html>";
Can anyone help me with this?

Replies are listed 'Best First'.
Re: CGI.pm question
by Fastolfe (Vicar) on Apr 13, 2001 at 23:09 UTC
    Try:
    use CGI ':standard';
    per the first section of the CGI.pm documentation.

    Additionally, if you're using the CGI module, you can have it do your CGI headers (and even your HTML) for you:

    use CGI ':standard'; print header, start_html(-title => 'CGI test'); print p('foo is ', param('foo'), '.'); print end_html;
Re: CGI.pm question
by thabenksta (Pilgrim) on Apr 13, 2001 at 23:12 UTC

    Or you could assign it to a handle.

    my $q = CGI::new(); my $foo = $q->param{'foo'};
    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
Re: CGI.pm question
by MrNobo1024 (Hermit) on Apr 13, 2001 at 23:10 UTC
    You need to replace use CGI; with use CGI qw(:standard);. That will tell CGI.pm to export standard functions like param() and header().
Re: CGI.pm question
by virtualsue (Vicar) on Apr 14, 2001 at 01:47 UTC
    I would like to suggest the following modifications:

       #!/usr/bin/perl -wT    #set warn and taint flags
       use strict;   #strict syntax checking

       use CGI qw/:standard :html3/;
       print "Content-type: text/html\n\n";
       etc.

    You don't need the ":html3" for this little snippet, but you soon will if you start using the html shortcuts (recommended) and want to do anything more than simple pages. The standard feature set for CGI.pm (loaded by ":standard") only supports HTML 2.0 tags.

    With "use strict;", you will need to declare $foo as "my $foo =..."

    See the perl documentation for examples of the html shortcuts, which IMO make perl CGI progs which generate HTML far more readable, and also for more info on perl warnings, taint mode, and strict.

      The standard feature set for CGI.pm (loaded by ":standard") only supports HTML 2.0 tags.

      I don't know what version of CGI.pm you're using, but mine (2.56) has this to say on the topic of :standard:

      :standard Import "standard" features, 'html2', 'html3', 'form' and 'cgi'.
      Excellent advice though.
        Silly me, I went back in time (& nearly 20 minor revision levels). Thanks for waking me up.
      ofcourse you could drop print "Content-type: text/html\n\n"; and let CGI.pm do the header handling... like gryphon did above

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
        Yes, I could; and in what passes for my real life I do. I merely copied the original poster's code and (for better or worse) made the points that I wanted to make.   Others, as you note, had already given examples of the use of HTML shortcuts, so I didn't want to plow the same furrow.
Re: CGI.pm question
by gryphon (Abbot) on Apr 13, 2001 at 23:58 UTC

    Greetings Anonymous,

    Try the following...

    use strict; use CGI qw(param header); print header; my $foo = param('foo'); print "<HTML><HEAD><TITLE>CGI Test</TITLE></HEAD>\n"; print "<BODY>$foo</BODY></HTML>\n";

    -gryphon.