Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

ferreira (5993)

  (email not shown publicly)

Just another Brazilian Perl hacker.

Journal of ferreira (5993)

Wednesday October 03, 2007
05:27 PM

A Test::WWW::Mechanize trap: getting contents in a file

[ #34604 ]

Sometimes I wonder at how good I am to use code in the wrong way. This time I did it with Test::WWW::Mechanize. I started a test script with:

    use Test::WWW::Mechanize;

    get_ok( $url );

and then I decided to take a look at the HTML I was getting. Using the LWP options seemed natural:

    get_ok( $url, { ':content_file' => 'p.html' } );

But that gave me some binary content rather than the HTML I was waiting for. The problem was that WWW::Mechanize (the father of most of the browser functionality of the testing module) determines that 'gzip' can be used as the content encoding (if Compress::Zlib is available) and transparently decodes the response.

Unfortunately, the LWP options in get_ok are the same as in LWP::UserAgent::get and knows nothing about this helpful trick. So I got the gzipped content and not the HTML page. The content is uncompressed later with other WWW::Mechanize methods.

In turn,

    use File::Slurp qw( write_file );
    write_file( 'p.html', $mech->content );

worked as I needed it.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.