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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.