Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Transaction time for LWP::UserAgent GET

by sauoq (Abbot)
on Jul 24, 2003 at 02:20 UTC ( [id://277405]=note: print w/replies, xml ) Need Help??


in reply to Transaction time for LWP::UserAgent GET

Your question has been answered but I have a couple comments about style. First, "indirect object" syntax for method calls is best avoided. Consider writing these alternatives for your current syntax:

my $agent = LWP::UserAgent->new( timeout => 30 ); # ... my $response = HTTP::Request->new( GET => $site );
Second, it doesn't really make sense to call your new HTTP::Request object $response. It is, afterall, a request object and not a response object. Why not call it $request? I have a feeling that you did it just so my $page = $agent->request($response) would read like English but I don't think that's worth the potential confusion. It might read like English, but it doesn't read like code. It's even more confusing once you realize that the request() method returns an HTTP::Response object. I'd write that like this:
my $get = HTTP::Request->new( GET => $site ); # ... my $response = $agent->request( $get );

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Transaction time for LWP::UserAgent GET
by William G. Davis (Friar) on Jul 24, 2003 at 07:59 UTC

    First, "indirect object" syntax for method calls is best avoided.

    Really? If you're not doing anything too complicated, then what's wrong with the indirect object syntax, and why should it be avoided? It works, and personally, I think it looks much, much better. When used for constructors, the syntax almost reads like English:

    my $ua = new LWP::UserAgent;

    "Create a new LWP User Agent."

      If you're not doing anything to [sic] complicated, then what's wrong with the indirect object syntax, and why should it be avoided?

      The biggest problem with indirect object notation is that perl has to jump through hoops to decide whether method Class; should be interpreted as Class->method() or method("Class") and small changes to the order of those hoops can make perl change its mind. That can result in the appearance of nasty hard-to-find bugs in previously working code after a small seemingly innocuous change.

      When used in instance method calls, the "object" suffers from the same brittle parsing that the filehandle argument to print() does. That is, however, a relatively uncommon practice in comparison to the habit of using it for class methods, particularly constructors.

      You are right that problems rarely arise in simple use. But, they do exist whether or not you are doing something complicated. That means you could be bitten when your simple code goes through several iterations and becomes more complicated or if you continue to use the syntax out of habit even when you take on a more complex project.

      Look at it this way... If you get tripped up by a subtle bug caused by the finickiness of indirect object syntax, you'll likely spend an hour or more pulling out your hair trying to debug a problem that you are sure you shouldn't be seeing... And when it's all over and you've determined the cause, you'll swear an oath never to use indirect object notation again anyway.

      Or, you might never run into the problem in which case you'll keep your hair as well as the bad habit and eventually you'll be hired as a PHB and stop coding altogether. But then you'll be forever furtively glancing over your shoulder in fear of the inevitable attack by a bald maintenance programmer with a strong homicidal urge brought on by debugging your code...

      Why not save yourself the trouble by breaking the bad habit now? ;-)

      -sauoq
      "My two cents aren't worth a dime.";
      

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found