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!