Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Last mod not working

by Anonymous Monk
on Jan 28, 2004 at 18:30 UTC ( #324729=perlquestion: print w/replies, xml ) Need Help??

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

What am I doing wrong here? It was working but then again I say that all the time. Please advise.
use LWP::Simple; use warnings; use strict; my $url = 'www.perl.com'; my $content = get($url); my ($lastMod) = (head($url))[2]; my ($lastModFormat) = scalar localtime($lastMod); print "$lastModFormat\n";
The above prints out:
Use of uninitialized value in localtime at C:\Perl\bin\headerTest.pl l +ine 12. Wed Dec 31 19:00:00 1969

Replies are listed 'Best First'.
Re: Last mod not working
by monktim (Friar) on Jan 28, 2004 at 18:36 UTC
    $lastMod isn't defined.
    if (defined $lastMod) { my ($lastModFormat) = scalar localtime($lastMod); print "$lastModFormat\n"; } else { print "Time not available\n"; }
      If I keep getting "Time not available" does that mean the head method cant find any last mod time on the web page??
      use LWP::Simple; use warnings; use strict; my $url = 'www.perl.com'; my $content = get($url); my $lastMod = (head($url))[2]; if (defined $lastMod) { my ($lastModFormat) = scalar localtime($lastMod); print "$lastModFormat\n"; } else { print "Time not available\n"; }
        That's correct. Even with the http:// prefix as derby suggested, the modified time isn't passed back. Your code is fine, you just need to check if the values are defined.
        use LWP::Simple; use warnings; use strict; my $url = 'http://www.perl.com'; my $content = get($url); my (@header) = head($url); print "content type: $header[0]\n" if defined $header[0]; print "document length: $header[1]\n" if defined $header[1]; print "modified time: $header[2]\n" if defined $header[2]; print "expires: $header[3]\n" if defined $header[3]; print "server: $header[4]\n" if defined $header[4];
Re: Last mod not working
by ysth (Canon) on Jan 28, 2004 at 19:01 UTC
    With this line:
    my ($lastMod) = (head($url))[2];
    you lose any distinction between head() failing (which returns an empty list) and head succeeding but modtime not being available. If you need to differentiate between the two, you can do it (without introducing any temp variables) by saying:
    if (my (undef,undef,$lastMod) = head($url)) { # success if (!defined $lastMod) { # but alas no mod time } }
    or even
    my $gotHead = (my (undef,undef,$lastMod) = head($url));
    This are instances of a list assignment in scalar context, which yields the number of entries in the list on the right hand side of the assignment (5 if head succeeded, 0 if it failed).
Re: Last mod not working
by derby (Abbot) on Jan 28, 2004 at 18:40 UTC
    While monktim was correct, I think the real fix is:

    my $url = 'http://www.perl.com';

    -derby
      Even with that, it still sometimes doesn't return the mod time. Try:
      use Data::Dumper; print Dumper head($url);
      I get:
      $VAR1 = 'text/html; charset=ISO-8859-1'; $VAR2 = undef; $VAR3 = undef; $VAR4 = undef; $VAR5 = 'Apache/1.3.29 (Unix) PHP/4.3.4 mod_perl/1.29';
        Last The definition of Last-Modified leaves some lee-way for the servers:

        HTTP/1.1 servers SHOULD send Last-Modified whenever feasible.

        So when is it not feasible? Well, some servers leave it up to an application server (or cgi script, or SSI handler) to send the header for dynamically created pages. I know I rarely set it for cgi scripts since the cost of keeping track of changes, to me, outweighs the cost of just sending the content. So you cannot depend on it being there. You will get it from most web servers for static content but how do you know what's static and what's not (think rewrite rules).

        -derby

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2022-10-07 01:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (29 votes). Check out past polls.

    Notices?