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

What is idiomatic use of WWW::Mechanize->new re: default headers?

by CoVAX (Beadle)
on Feb 09, 2015 at 01:36 UTC ( [id://1115984]=perlquestion: print w/replies, xml ) Need Help??

CoVAX has asked for the wisdom of the Perl Monks concerning the following question:

Here's what I've currently doing (snippet):

$mech = WWW::Mechanize->new( autocheck => 1 ); $mech->cookie_jar($cookie_jar); $mech->default_header( 'User-Agent' => 'Mozilla/5.0 <snip>' ); $mech->default_header( 'Referer' => 'https://www.<snip>' ); $mech->default_header( 'Accept' => 'application/<snip>' ); $mech->default_header( 'Accept-Language' => 'en-US,en;q=0.5' ); $mech->default_header( 'Accept-Encoding' => 'gzip, deflate' ); $mech->default_header( 'Connection' => 'keep-alive' ); $mech->show_progress(1);

Since TIMTOWTDI I'm wondering if I should use a hash instead, and pass a reference to it to WWW::Mechanize->new() (how?), or should I call default_header once, passing it a list of key/value pairs?

Your thoughts?

Replies are listed 'Best First'.
Re: What is idiomatic use of WWW::Mechanize->new re: default headers?
by ikegami (Patriarch) on Feb 09, 2015 at 02:34 UTC
    WWW::Mechanize->new( autocheck => 1, cookie_jar => $cookie_jar, default_headers => HTTP::Headers->new( User_Agent => 'Mozilla/5.0 <snip>', Referer => 'https://www.<snip>', Accept => 'application/<snip>', Accept_Language => 'en-US,en;q=0.5', Accept_Encoding => 'gzip, deflate', Connection => 'keep-alive', ), show_progress => 1, );

    Both the original version and this version look fine to me.

    (User_Agent => ... and 'User-Agent' => ... can be used interchangeably in both snippets.)

      I like your version much better than mine.

      Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1115984]
Approved by Athanasius
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: (3)
As of 2025-12-17 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (98 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.