Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to reliably get file size after calling WWW::Mechanize->get(file)?

by CoVAX (Beadle)
on Feb 16, 2015 at 20:59 UTC ( [id://1116927]=perlquestion: print w/replies, xml ) Need Help??

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

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"; }
Searched for donut and crumpit. Found donate and stumbit instead.
  • Comment on How to reliably get file size after calling WWW::Mechanize->get(file)?
  • Download Code

Replies are listed 'Best First'.
Re: How to reliably get file size after calling WWW::Mechanize->get(file)?
by Anonymous Monk on Feb 17, 2015 at 00:39 UTC

    Um, what?

    Here is some "mechanize"

    so it all pretty much "just works"

    So what was the question?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1116927]
Approved by philipbailey
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2025-12-06 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (85 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.