Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Others have offered explanations and if you'd like another example (perhaps something a little less textbook-like) I just grepped this from my scratch directory. It's hideous, yes, but demonstrates the point at hand.

Newlines added for "clarity":

print LWP::UserAgent->new()-> request( HTTP::Request->new(GET => 'http://geeksalad.org') )->content;
Of course, there's no error checking in there (I didn't want it at the time). That'd make it look a bit more like:
if (($_=LWP::UserAgent->new()-> request( HTTP::Request->new(GET=>'http://geeksalad.org') ))->is_success) { print $_->content; }
I'm assuming, of course, that HTTP::Request isn't going to fail (unlikely anyway). And this doesn't allow me the opportunity to do anything useful with the UserAgent object other than what I could have accomplished with LWP::Simple. I have no idea why this was there, other than to serve as an example of some kind. Whether this was to demonstrate something good or bad, I can no longer remember. :)

This mess is more commonly written as something like (edited from the LWP manpage):

# Create a user agent object use LWP::UserAgent; $ua = new LWP::UserAgent; my $req = new HTTP::Request POST => 'http://geeksalad.org'; my $res = $ua->request($req); if ($res->is_success) { print $res->content; } else { print "Bad luck this time\n"; }
So what my code snippet above did was exactly what's being done below, except that I don't put the request object into a variable ($req), and I don't put the user agent object ($ua) in one either. The only thing I stash away is the result ($res/$_) object because I have to call two methods (content, is_success) on it. Neither of these returns the original object, so I can't chain these together with ->'s.

In reply to Re: Getting Confused with the '->' operator by clintp
in thread Getting Confused with the '->' operator by the_Don

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found