Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by Wonko the sane (Deacon)
on Jun 01, 2005 at 11:57 UTC ( [id://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,
Wonko

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

    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)
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://462396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found