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

Posting as a new subject, as this doesn't really relate to the last subject I was asking about, even though it's the same code

Alright, I'm trying to get the date to be human readable... I figured the best thing for this is Date::Manip, however, I can't seem to get the filestat information into the datemanip...
foreach my $farm (@farms) { my @fileStat = stat($farm); printf "%s: Age [%s], size [%d]\n", $farm, &ParseDateString("epoc +h $s->mtime()"), $s->size(); } # end of foreach statement
I've tried a couple things, but nothing I try seems to work...

here's the entire script so far:

#!/usr/bin/perl use strict; use File::stat; use Date::Manip; # This is a program that is intended to allow FEP operators # a tool that will facilitate in monitoring incoming transmissions # and preprocessor logs. #------------------- Define Subroutines ------------------# # # # Main is a subroutine simply written for flow control sub Main { my $corp = $_[0]; print "Input:\n"; farmInput($corp,'input'); } # end Main # farmInput does a wildcard search of all /usr/local/farm/*/*/input fo +lders # looking for the <STDIN> corp number and printing those files to scre +en. sub farmInput { my $corp = $_[0]; my $finalPath = $_[1]; my $path = "/usr/local/farm/*/*/$finalPath/*$corp*"; my @farms = glob($path); foreach my $farm (@farms) { my $s = stat($farm); printf "%s: Age [%s], size [%d]\n", $farm, &ParseDateString("epoc +h $s->mtime()"), $s->size(); } # end of foreach statement } # end farmInput #------------------- End Subroutines ---------------------# print "What corp are you looking for? " ; chomp(my $whatWeWant = <STDIN>); Main($whatWeWant);
I'm trying to streamline it as I go, I know that in the farmInput subrouting I could put the $corp and $finalPath variables on the same line, just haven't gotten to that yet... I'd like my code to be as small and clean as possible... but it needs to work first! ;o)

Replies are listed 'Best First'.
Re: Human Readable Date
by davido (Cardinal) on Jan 18, 2005 at 05:10 UTC
    Global symbol "$s" requires explicit package name at /home/bishopjd/documents/Goomba line 35. Global symbol "$s" requires explicit package name at /home/bishopjd/documents/Goomba line 35. Execution of /home/bishopjd/documents/Goomba aborted due to compilation errors.

    You changed the code in your question. In the original version of this post, before you edited it, the line, "my $s = stat( $farm );" wasn't there. And that's why you were getting a message saying that you hadn't properly declared $s. Now that you've updated your code, you should probably update what the error message is that you're now getting.

    Update: And now you've changed it again, removing the error message that you originally posted, and simply stating the extremely vague "Nothing seems to work." How can we help you when you don't tell us exactly what it's doing. And how can we help when you change the question while we're responding?


    Dave

      I apologize, I've tried a couple things, and was working on the code as I was asking about it, if I can figure out myself, all the better, right? the lack of $s was because I was trying to put the results of the stat into an array and then take the date part and convert it to human readable date/time... that's what's not working... the rest of the code seems to do exactly what it needs... but I have 2 lines:
      printf "%s: Age [%s], size [%d]\n", $farm, ParseDateString("epoch $s-> +mtime()"), $s->size();
      Which gives me a blank area where it prints: Age [ ]

      And the other line
      printf "%s: Age [%s], size [%d]\n", $farm, $s->mtime(), $s->size();
      which prints the unix time in the age: Age [1106013532]

      so the question is why isn't it being formatted into a human readable date?

        Here's the problem, and you get credit for discovering what I believe to be a bug in Date::Manip (or its documentation).

        The documentation for Date::Manip correctly states that ParseDateString() will not correctly handle a raw number of seconds since epoch. Obviously, seconds since epoch is exactly what $s->mtime() is giving you. So you need a way of getting Date::Manip's ParseDateString() to recognize that what you're handing it is seconds since epoch.

        The POD for Date::Manip goes on to tell you how to do that:

        NOTE: One of the most frequently asked questions that I have gotten is how to parse seconds since the epoch. ParseDateString cannot simply parse a number as the seconds since the epoch (it conflicts with some ISO-8601 date formats). There are two ways to get this information. First, you can do the following:

        $secs = ... # seconds since Jan 1, 1970 00:00:00 GMT $date = &DateCalc("Jan 1, 1970 00:00:00 GMT",$secs);

        Second, you can call it directly as:

        $date = &ParseDateString("epoch $secs");

        You're attempting to use the second method, and at least for me on Win32 ActivePerl 5.8.6 with Date::Manip version 5.42, that second method simply doesn't work as advertised. It fails to convince Date::Manip that you've handed it a string of seconds since epoch, and the result is that ParseDateString doesn't know what it's looking at, and defaults to returning an empty string.

        I have gone on to confirm that at least the first method suggested in the documentation for Date::Manip does work. That is,

        printf "%s: Age [%s], size[%d]\n", $farm, DateCalc( "Jan 1, 1970 00:00:00 GMT",$s->mtime() ), $s->size();

        Try that on for size. ;)

        Update: Bug alert has been rescinded. See my followup below.


        Dave

Re: Human Readable Date
by edan (Curate) on Jan 18, 2005 at 10:24 UTC

    What's wrong with the perl builtin functions localtime or gmtime? To get a human readable string from epoch time you can simply:

    my $str = scalar localtime $s->mtime();

    Substitute localtime with gmtime if you need GMT time. If you want more control over the format of the time, look at POSIX::strftime(), as in the following example:

    use POSIX qw/strftime/; my $str = POSIX::strftime "%F %T", localtime $s->mtime();
    --
    edan

Re: Human Readable Date
by duff (Parson) on Jan 18, 2005 at 06:10 UTC

    Interpolation of $s->mtime() isn't doing what you think it is. Try it without interpolating, or save that value to a simple scalar and interpolate that simple scalar.

Re: Human Readable Date
by YuckFoo (Abbot) on Jan 18, 2005 at 19:31 UTC
    I've always been partial to Date::Format

    YuckFormat

    #!/usr/bin/perl use Date::Format; print time2str("%Y/%m/%d %T\n", time());