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

Hi all, I am retriving page with get($url); method and i need to know the size of that page in bytes, is there any quick way to do it any help is appreciated! Thanks

Replies are listed 'Best First'.
Re: Document Size
by miyagawa (Chaplain) on Oct 05, 2001 at 06:46 UTC
      Thanks so much! You made my task very easy!
Re: Document Size
by Zaxo (Archbishop) on Oct 05, 2001 at 07:15 UTC

    You can also look at the Content-Length header in the LWP::Response object returned by the request. To get the vital statistics before downloading:

    use LWP::Simple; my ($content_type, $document_length, $modified_time, $expires, $server) = head( $url);

    After Compline,
    Zaxo

Re: Document Size
by japhy (Canon) on Oct 05, 2001 at 07:06 UTC
    No need to get() the file, then:
    ($size) = head($url) =~ /^Content-Length:\s+(\d+)/im;

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Beware that the Content-Length header is optional though, and many dynamic documents will not output it. Also, some servers tend to do different things with it depending on the request method:

      % HEAD http://www.cnn.com/ 200 OK Cache-Control: private,max-age=60 Connection: close Date: Fri, 05 Oct 2001 08:14:42 GMT Server: Netscape-Enterprise/4.1 Content-Length: 0

      Then there is the case of servers which will not handle HEAD requests. In the end the only guaranteed way is to use GET and length().

Re: Document Size
by EvanK (Chaplain) on Oct 05, 2001 at 08:52 UTC