Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: use CGI qw(:standard) vs. use CGI

by Wonko the sane (Deacon)
on Jun 01, 2005 at 11:57 UTC ( #462396=note: print w/replies, xml ) Need Help??

in reply to Re: use CGI qw(:standard) vs. use CGI
in thread use CGI qw(:standard) vs. use CGI

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,

Replies are listed 'Best First'.
Re^3: use CGI qw(:standard) vs. use CGI
by jeffa (Bishop) on Jun 01, 2005 at 13:53 UTC

    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. :)


    (the triplet paradiddle with high-hat)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://462396]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-06-22 00:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (120 votes). Check out past polls.