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

Hi all, As you can see I am in the very early stages of learning to use the Perl CGI module. I have this little script (learning about post and get)

use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); my $cgi = CGI->new(); print $cgi->header(), $cgi->start_html(), $cgi->start_form({ -action => "http://example.com/favourites.html", -method => "get", }); print $cgi->p('What is your favourite colour? <input type="text" name="colour" /><br/> What is your favourite food? <input type="text" name="food" /><br/> <input type="submit" '), $cgi->end_form, $cgi->end_html();
which will work fine when fatalsToBrowser is not enabled, but when it is I get this error

Can't locate object method "new" via package "CGI" at...

I do know about perldoc and the perl faq, have had a look at perltoot but am still not really sure... hoping someone will be able to easily explain this error to me. Thanks ; )

Replies are listed 'Best First'.
Re: CGI Error
by stonecolddevin (Parson) on Mar 01, 2007 at 20:30 UTC

    You forgot to use CGI.

    use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); use CGI; # <-- need this to use CGI ...

    meh.
      oh dear, thanks very much... I'm just going by the examples given in the tutorial I'm working through at the moment and they've done it the way I posted - I assumed that the one mention of CGI covered all

      ta again

        Not a problem at all, once you start working more with CGI and modules in general, you'll get a better knack for what modules are needed for what :-)

        meh.
Re: CGI Error
by scorpio17 (Canon) on Mar 02, 2007 at 14:56 UTC
    Not to quibble - but since you're new to CGI.pm, note that you could write it this way:
    use strict; use CGI qw/:standard/; print header, start_html, start_form, "What's your favorite color?", textfield('color'), p, "What's your favorite food?", textfield('food'), p, submit, end_form, hr, "\n"; if (param) { print "Favorite color: ", param('color'),p, "Favorite food: ", param('food'), "\n"; } print end_html;