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

I'm using a script for parsing errors, forms, cookies, etc all at once. Due to the forms, I'm using:
use CGI;
for the errors, I'm using:
use CGI::Carp qw/fatalsToBrowser/;
Now, the cookie documentation says I need to use:
use CGI qw/:standard/; use CGI::Cookie;
Now, is use CGI qw/:standard/ the same as use CGI? I mean, if I use the qw/:standard/, will it do the same for forms as would the regular use CGI; or would I have to put this:
use CGI qw/:standard/; use CGI::Cookie; use CGI; $query = new CGI;

Replies are listed 'Best First'.
Re: CGI.pm questions...
by eg (Friar) on Feb 09, 2001 at 23:56 UTC

    You don't need to use both. Using qw/:standard/ just imports certain non-object methods into your script's namespace. If you're using the objects (as it looks like you are), there's no need to use qw/:standard/.

    In fact, using CGI is sufficient to read and set cookies. You don't need to explicitly use CGI::Cookie.

      If the user wants to import all CGI methods (and doesn't mind the slight overhead) he can use CGI qw/:all/;
Re: CGI.pm questions...
by tadman (Prior) on Feb 10, 2001 at 02:26 UTC
    As with any module, the parameters after the use MODULE_NAME statement are forwarded to the import method of the requested module. Exporter normally handles the exporting of functions from the module into the calling package namespace so that you can just use a function without a package name:
    use XYZ; print XYZ::SpecialFunction(),"\n"; # versus: use XYZ qw ( SpecialFunction ); print SpecialFunction(),"\n"; # SpecialFunction imported
    Exporter also allows you to group together a bunch of exports with a "tag" to simplify usage. ':standard' is such a tag, and it will import a whole bunch of useful functions without you having to name them.

    As eg pointed out, since you aren't calling these functions directly, you won't have to worry about importing them. Using the object-oriented method means that you will always specify "$query->function()" so that Perl will know where to fetch that function from.

    So, if you want to use the object-oriented method, like you have with the 'new CGI' call, then don't import anything since you don't need it. However, if you aren't using that, and you want to slam out code quickly using the CGI methods, then import what you need (i.e. ':standard' for example) and then go wild:           print hr(),table(tr(td('My Table Content'))),"\n";
Re: CGI.pm questions...
by extremely (Priest) on Feb 10, 2001 at 10:29 UTC
    A little more on this than others posted might be handy. Do you tend to use the OOP style when calling the class methods in CGI. By that, I mean, do you call them like @p=$query->param() or do you tend to use the imported commands like @p=param()? If you tend to call only the methods by class then you may want to do a use CGI (); to tell perl not to import anything into your space. Sending it an empty list like that is a nice trick if you play the honest OOP game.

    OTOH if you like the handy-dandy HTML helpers and like them imported or if you want the code methods imported as well you can get CGI to give you just the ones you want.

    Commonly used requests that I've seen are:

    ## remember, only one. use CGI; # let it do it's own thing. use CGI (); # import NOTHING, thank you. use CGI qw(param redirect); # just param() and redirect() use CGI qw(:cgi); # just cgi stuff use CGI ':html'; # all the HTML[23] and netscape stuff use CGI qw/:html :form/; # all html + form html widgets use CGI ':standard'; # all cgi and HTML[23] and forms use CGI ':all'; # kitchen sink

    I tend not to use the qw operator on a single item, but you may get into the habit of doing so if you tend to add things one at a time while building code. Not a bad habit but I personally think it is ugly =)

    HTH

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: CGI.pm and cookies
by flocto (Pilgrim) on Feb 10, 2001 at 09:31 UTC
    There is no need to import CGI::cookie. All neccesary routines are imported by ':standard'.. The use is really simple:
    my $cookie = cookie(-NAME => 'name', -VALUE => 'some text', -EXPIRES = +> 'now'); print header(-COOKIE => $cookie, -TYPE => 'text/html');
    Regards,
    octopus
    --
    GED/CC d-- s:- a--- C++(+++) UL+++ P++++$ L++>++++ E--- W+++@ N o? K? w-- O- M-(+) V? !PS !PE !Y PGP+(++) t-- 5 X+ R+(+++) tv+(++) b++@ DI+() D+ G++ e->+++ h!++ r+(++) y+