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

Is my understating correct for use "CGI qw(:standard)" vs. "use CGI"?

I'm using the function "redirect('http://www.example.com')" in my script. When I use "use CGI" instead of "use CGI qw(:standard);" I get the following error:
Software error: Undefined subroutine &main::redirect called at upload.pl line 109, <fh +00001C%3A\Program Files\Java\j2re1.4.2_06\javaws\javalogo52x88.gif> l +ine 20. For help, please send mail to the webmaster (webmaster@example.com), g +iving this error message and the time and date of the error.

When I use use "use CGI qw(:standard);" the function works.

My understanding is that "use CGI" is for Object Oriented programming. And "use CGI qw(:standard);" is for Function Oriented programming.

Using "use CGI" I'd have to do:
use CGI; my $q = new CGI; $q->redirect('http://www.example.com')

Using "use CGI qw(:standard); " I'm importing the function for Function Oriented programming.

use CGI qw(:standard); redirect('http://www.example.com');
Is my understating correct for use "CGI qw(:standard)" vs. "use CGI"?

Replies are listed 'Best First'.
Re: use CGI qw(:standard) vs. use CGI
by Zaxo (Archbishop) on Jun 01, 2005 at 07:20 UTC

    Yes, you're correct.

    A third form of usage is: use CGI(); With that you have the OO conventions, but no attempt is made to obtain form data.

    In the OO form it's advisable to call new with my $q = CGI->new();, rather than the indirect object call you show. That is a general technique to prevent some hard-to-find bugs where namespaces are flying about.

    After Compline,
    Zaxo

      Hello Zaxo,

      I have heard this before, but I am unclear as to exactly why this is better
      or what types of problems might be encountered if it was done the other way.

      Why is it better to call an objects new method like this...

      $q = CGI->new();
      rather than...
      $q = new CGI;

      Any help is much appreciated.

      Best Regards,
      Wonko

        A long time ago i read some material here at the Monastery that disuaded using "indirect object syntax" (thanks Corion). After reading your question, which is a good one ... i did a Google Search Gone Wrong (notice the first hit) and found the result so funny that i just had to share it in the CB. This led to both bart and Corion correcting my search parameters, and bart went searching for material. From one of runrig's journal entries, bart posted this link in the CB. I invite you to copy that code and run it on your own.

        Done so yet? Good. :) The only difference in the two versions is that the second declares package X::Y before it is used in package X. Had the code not used indirect object syntax, it would not have mattered which package was declared first. That is, new X::Y (@_); should have been X::Y->new(@_);. Using direct object syntax, in essence, is just a Good Habit™ to get into. For your example, it doesn't really matter. But the day may come when you, like Simon Cozens, will get bitten in the butt from using indirect object syntax (or notation).

        To add some more insight to your original question (and yes, your understanding is correct) i only instantiate a CGI object (via my $cgi = CGI->new;) if i have to reuse that CGI object. Otherwise i just import the functions into my namespace for ease of typing. Check this out:

        perl -MCGI=foo -le'print foo{bar=>baz}=>qux'

        Thanks to bart (who should get all the credit for this node), Corion, and of course, yourself and Zaxo. :)

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: use CGI qw(:standard) vs. use CGI
by radiantmatrix (Parson) on Jun 01, 2005 at 14:15 UTC

    You're almost right. One nitpick: when you use CGI qw':standard', you're actually importing the set of functions and variable tagged as 'standard' in the CGI module. So you aren't importing one function, but a collection of them.

    The piece of a use statement you express as qw(:standard) is the list of items to import into your namespace. Items in the list that begin with a colon (':') are "tags" and represent groups of items to import.

    Yoda would agree with Perl design: there is no try{}