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

Hi Monks,
This is crazy but I can't figure this one out. This time all I am trying to do is print the age of the session files found in a directory. Why is the time been displayed is the same for all the files, and how would I format the results to be readable in hours, minutes etc.
Here is my nightmare code,and the time been printed is not correct for these files as well. Any help please, or a better way to get get the age of files in hours,minutes etc, will be very helpful!
#!/usr/bin/perl -w use strict; use CGI qw(-oldstyle_urls :standard); use CGI::Carp qw(fatalsToBrowser); use File::Find; use File::stat; my $mytime=(time); my $file_stat; my $all_session = "../sessions"; print header(); print qq(<br><br><table width="600" border="1" bgcolor="#808080" cellp +adding="2" cellspacing="0"> <tr><form action="wacko_del_old_files.pl" method="post" style +="margin: 0px; padding: 0px;"> <td align="left" colspan=\"2\">&nbsp;Sessions.</td> </tr> <tr><td align="left" colspan=\"2\">&nbsp;</td></tr>); opendir(DIR, $all_session) or die "Couldn't open directory, $!"; while (my $sessions = readdir DIR) { # Use a regular expression to ignore files beginning with a peri +od next if ($sessions =~ m/^\./); my $age = (time() - (stat("$all_session/$sessions"))[10]); print "<tr> <td align=\"left\">&nbsp;$sessions - age= $age</td> <td align=\"left\">&nbsp;<input type=\"checkbox\" name= +\"del_sessions\" value=\"$sessions\">&nbsp;</td> </tr>"; } closedir DIR; print "<tr> <td colspan=\"2\" align=\"right\">&nbsp;<td><input type=\"s +ubmit\" value=\"Delete\"></td></td> </tr></form> </table>";

Thanks for looking!

Replies are listed 'Best First'.
Re: Printing files age information help!
by oko1 (Deacon) on Jan 16, 2011 at 19:07 UTC

    The problem is that you're using File::stat, but you're using 'stat' as if it was the built-in version. Either switch to 'stat->mtime' in your invocation or get rid of 'use File::stat;' at the top of the script; either one should fix you up just fine.

    -- 
    Education is not the filling of a pail, but the lighting of a fire.
     -- W. B. Yeats
Re: Printing files age information help!
by Anonymous Monk on Jan 16, 2011 at 18:46 UTC
    Why are you avoiding using CGI::Session?
      Honestly I tried but couldn't get to delete the sessions in the directory specific. My plan is to find the sessions older than a specific time and use "unlink" to manually delete old left sessions by the user.
        But CGI::Session->find does work, and does delete old sessions without compunction