I have some questions about the following code.
1. How reliable is it to use the -s file test operator so soon after calling $mech->get considering the file contents may still be in a RAM buffer? (Especially for a small file (e.g. this one is 2,575 bytes).)
2. So I thought, "Can I use the size from the response header?" I read the docs about Mech's get, and HTTP::Response, but I can't figure out how to do so.
3. Should I even consider using the content-length for an accurate (reliable) measure of the file size considering the affects from gzip'ping and etc.? I.e., is -s the "only" way to get an accurate/reliable file size?
#! /usr/bin/perl -w use strict; use Data::Dump qw(pp); use WWW::Mechanize; my $url = 'http://perlmonks.org/images/vroom_sm.gif'; my $mech = WWW::Mechanize->new(); my $http_resp_obj = $mech->get( $url, ':content_file' => 'vroom_sm.gif +' ); if ( $mech->success ) { # What if file buffer(s) haven't been flushed yet? print "File size is ", -s 'vroom_sm.gif', "\n"; # I could not figure out how to get the value of the 'content-length +' key # despite examining the output from: # pp($http_resp_obj); print "Content length is $http_resp_obj->header('content-length')\n" +; } else { warn "Failed to download '$url' (" . $mech->status() . ")\n"; }
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |