Rectangle 27 0

perl What is the preferred method of accessing WWW::Mechanize responses?


Btw, for either method, you should check $response->is_success or $mech->success before accessing the content, as the request may have failed.

From the perldoc: standalone WWW::Mechanize instances have autocheck turned on. However, if WWW::Mechanize is subclassed, it's off.

I added autocheck because of how staggeringly common it was for people to come into the #perl IRC channel and complain that $mech->content was empty, because they'd not bothered to check $mech->success. Now, the most common case is the default.

Oh wow, the last time I used Mechanize, that option didn't exist. That was in 2008... now I feel old :(

They are both acceptable. The second one seems cleaner to me because it returns a proper HTTP::Response object which you can query and call methods on, and also means that if you make another Mechanize request, you'll still have access to the old HTTP response. With your first approach, each time you make a request, the content method will change to something new, which sounds error-prone.

Note
Rectangle 27 0

perl What is the preferred method of accessing WWW::Mechanize responses?


Btw, for either method, you should check $response->is_success or $mech->success before accessing the content, as the request may have failed.

From the perldoc: standalone WWW::Mechanize instances have autocheck turned on. However, if WWW::Mechanize is subclassed, it's off.

I added autocheck because of how staggeringly common it was for people to come into the #perl IRC channel and complain that $mech->content was empty, because they'd not bothered to check $mech->success. Now, the most common case is the default.

Oh wow, the last time I used Mechanize, that option didn't exist. That was in 2008... now I feel old :(

They are both acceptable. The second one seems cleaner to me because it returns a proper HTTP::Response object which you can query and call methods on, and also means that if you make another Mechanize request, you'll still have access to the old HTTP response. With your first approach, each time you make a request, the content method will change to something new, which sounds error-prone.

Note