date modified if statement

Discussion in 'Perl Misc' started by Naji, Oct 25, 2005.

  1. Naji

    Naji Guest

    I'm rather new to Perl and its syntax, and I did try multiple searches
    before posting this, but I am using Perl to create a Macro that will
    automatically send an e-mail and a specific attachment every time the
    attachment is updated. What would the syntax be for the if statement
    for code that would send the attachment if it has been modified in the
    past 12 hours? Any help would be appreciated. Thanks!
    Naji, Oct 25, 2005
    #1
    1. Advertising

  2. Naji

    J. Gleixner Guest

    Naji wrote:
    > I'm rather new to Perl and its syntax, and I did try multiple searches
    > before posting this, but I am using Perl to create a Macro that will
    > automatically send an e-mail and a specific attachment every time the
    > attachment is updated. What would the syntax be for the if statement
    > for code that would send the attachment if it has been modified in the
    > past 12 hours? Any help would be appreciated. Thanks!
    >


    you_will_find_your_answer() if $you_read_the_documentation_for_stat;

    perldoc -f stat
    J. Gleixner, Oct 25, 2005
    #2
    1. Advertising

  3. Naji

    Naji Guest

    So I searched for documentation for stat which you speak about, I
    google'd it and got this:

    use File::stat;
    $st = stat($file) or die "No $file: $!";
    if ( ($st->mode & 0111) && $st->nlink > 1) ) {
    print "$file is executable with lotsa links\n";
    }
    use File::stat qw:)FIELDS);
    stat($file) or die "No $file: $!";
    if ( ($st_mode & 0111) && $st_nlink > 1) ) {
    print "$file is executable with lotsa links\n";
    }


    I do not think this code is what I'm looking for. Where can I get this
    documentation which you speak of? Thanks.
    Naji, Oct 25, 2005
    #3
  4. Naji

    Ian Wilson Guest

    Dear Google-Groups victim, please quote some context!
    http://groups.google.com/googlegroups/posting_style.html#summarize

    Here's how:
    http://cfaj.freeshell.org/google/google-groups.html

    Naji wrote:
    > So I searched for documentation for stat which you speak about, I
    > google'd it and got this:
    >
    > use File::stat;
    > $st = stat($file) or die "No $file: $!";
    > if ( ($st->mode & 0111) && $st->nlink > 1) ) {
    > print "$file is executable with lotsa links\n";
    > }
    > use File::stat qw:)FIELDS);
    > stat($file) or die "No $file: $!";
    > if ( ($st_mode & 0111) && $st_nlink > 1) ) {
    > print "$file is executable with lotsa links\n";
    > }
    >
    >
    > I do not think this code is what I'm looking for.


    It is and it isn't.

    > Where can I get this
    > documentation which you speak of? Thanks.
    >


    The docs are installed with perl. At a command line type this command:
    perldoc -f stat.
    Ian Wilson, Oct 25, 2005
    #4
  5. Naji

    Paul Lalli Guest

    Naji wrote:
    > So I searched for documentation for stat which you speak about


    Who's "you"? Please provide some context when replying to a Usenet
    newsgroup.

    > I google'd it and got this:


    Google is far from the best way to find documentation for Perl. Use

    > use File::stat;
    > $st = stat($file) or die "No $file: $!";
    > if ( ($st->mode & 0111) && $st->nlink > 1) ) {
    > print "$file is executable with lotsa links\n";
    > }
    > use File::stat qw:)FIELDS);
    > stat($file) or die "No $file: $!";
    > if ( ($st_mode & 0111) && $st_nlink > 1) ) {
    > print "$file is executable with lotsa links\n";
    > }
    >
    > I do not think this code is what I'm looking for. Where can I get this
    > documentation which you speak of? Thanks.


    Run this command at the command line:
    perldoc -f stat
    or, for the command-line impaired, use the web interface at:
    http://perldoc.perl.org/functions/stat.html

    Paul Lalli
    Paul Lalli, Oct 25, 2005
    #5
  6. Naji

    Guest

    Naji wrote:
    > I'm rather new to Perl and its syntax, and I did try multiple searches
    > before posting this, but I am using Perl to create a Macro that will
    > automatically send an e-mail and a specific attachment every time the
    > attachment is updated. What would the syntax be for the if statement
    > for code that would send the attachment if it has been modified in the
    > past 12 hours? Any help would be appreciated. Thanks!


    I think that syntax would be like this (assuming you have the
    attachment name in the variable $attachment):

    if(-M $attachment <= 0.5) {
    # Attachment was modified in the last 12 hours
    }

    The part "-M $attachment" is a file test. File tests are made up
    of a dash, followed by a letter, and then the name of the file. The
    "-M" file test returns the number of days since the file was last
    modified (as of the script starting time). 12 hours is 0.5 days, so we
    check to see if the modification time is less than or equal to 0.5.
    The code sending the attachment would replace the comment inside.

    Also, as J. Gleixner wrote, you can use the stat function like this:

    ($device, $inode, $mode, $numOfHardLinks, $userId, $groupId,
    $deviceIdentifier, $size, $lastAccessed, $lastModified,
    $inodeLastChanged) = stat($attachment);

    We are interested in the $lastModified variable which would the last
    modification time for $attachment, in the number of seconds since the
    epoch. Using the stat function, you could also test the modification
    time this way:

    $currentTime = time();
    ($device, $inode, $mode, $numOfHardLinks, $userId, $groupId,
    $deviceIdentifier, $size, $lastAccessed, $lastModified,
    $inodeLastChanged) = stat($attachment);

    if($lastModified + 43200 >= $currentTime) { # 43200 is the number of
    seconds in 12 hours.
    # Attachment was modified in the last 12 hours
    }

    Hope this helps,
    David
    , Oct 25, 2005
    #6
  7. Naji

    Mothra Guest

    Naji wrote:
    > I'm rather new to Perl and its syntax, and I did try multiple searches
    > before posting this, but I am using Perl to create a Macro that will
    > automatically send an e-mail and a specific attachment every time the
    > attachment is updated. What would the syntax be for the if statement
    > for code that would send the attachment if it has been modified in the
    > past 12 hours? Any help would be appreciated. Thanks!


    This might help.

    use strict;
    use warnings;
    use DateTime;
    my $file = 'your_file_here';
    my $dt1 = DateTime->now;
    my $dt2 = $dt1 - DateTime::Duration->new( hours => 12 );
    unless ( -e $file ) {
    die "$file does not exists\n";
    }
    my $dt_file = DateTime->from_epoch( epoch => ( stat($file) )[9] );
    #make sure $dt1 is less that $dt2
    ( $dt1, $dt2 ) = ( $dt2, $dt1 ) if $dt1 > $dt2;

    if ( $dt1 <= $dt_file and $dt_file <= $dt2 ) {
    print "yep! we need to mail the file";
    }

    I hope this helps

    Mothra
    Mothra, Oct 25, 2005
    #7
  8. Naji

    Naji Guest

    I appreciate the help. I did find the online stat documentation,
    however I am a Perl newbie and need to grasp some of the other
    concepts. What would the If Statement look like if I'm trying to test
    for files that have been modified in the last 24 hours? I will go
    through some tutorials and try to figure it out on my own, but any help
    or direction would be much appreciated.
    Naji, Oct 25, 2005
    #8
  9. Naji

    Naji Guest

    Thank you for your help! I appreciate it a lot!!!!

    Mothra wrote:
    > Naji wrote:
    > > I'm rather new to Perl and its syntax, and I did try multiple searches
    > > before posting this, but I am using Perl to create a Macro that will
    > > automatically send an e-mail and a specific attachment every time the
    > > attachment is updated. What would the syntax be for the if statement
    > > for code that would send the attachment if it has been modified in the
    > > past 12 hours? Any help would be appreciated. Thanks!

    >
    > This might help.
    >
    > use strict;
    > use warnings;
    > use DateTime;
    > my $file = 'your_file_here';
    > my $dt1 = DateTime->now;
    > my $dt2 = $dt1 - DateTime::Duration->new( hours => 12 );
    > unless ( -e $file ) {
    > die "$file does not exists\n";
    > }
    > my $dt_file = DateTime->from_epoch( epoch => ( stat($file) )[9] );
    > #make sure $dt1 is less that $dt2
    > ( $dt1, $dt2 ) = ( $dt2, $dt1 ) if $dt1 > $dt2;
    >
    > if ( $dt1 <= $dt_file and $dt_file <= $dt2 ) {
    > print "yep! we need to mail the file";
    > }
    >
    > I hope this helps
    >
    > Mothra
    Naji, Oct 25, 2005
    #9
  10. Purl Gurl <> wrote in news:435E68F3.8080800
    @purlgurl.net:

    > In the future, work at writing articles which are clear,
    > concise and coherent so as not to leave readers guessing.
    >
    > Purl Gurl


    Please read the posting guidelines for this group.

    > #!perl


    use strict;
    use warnings;

    missing.

    > opendir (DIRECTORY, "c:/apache/users/test/");


    You never check the whether this call succeeded:

    opendir my $dir, 'c:/apache/users/test/'
    or die "Cannot opendir c:/apache/users/test/: $!";

    > while (defined ($filename = readdir (DIRECTORY)))
    > {
    > if ( ($^T - 43200) <= (stat ($filename)) [9])


    You should read the documents for readdir. Since you haven't changed
    directory to c:/apache/users/test/, the stat call is wrong.

    > { push (@Array, $filename); }
    > }
    > if (scalar (@Array) > 0)


    No need for scalar here:

    if (@Array)

    works just as well.

    Here is a better way (in the sense that it is correct, easier to
    understand etc):

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $src_dir = $ARGV[0] || '.';

    opendir my $dir, $src_dir or die "Cannot opendir '$src_dir': $!";

    my @files_to_email = grep {
    plain_files_modified_in_last_12_hours($src_dir, $_)
    } readdir $dir;

    closedir $dir;

    {
    local $" = "\n";
    print "@files_to_email\n";
    }

    use File::Spec::Functions 'catfile';
    sub plain_files_modified_in_last_12_hours {
    my ($dir, $file) = @_;
    return if $file =~ m{ \A \.{1,2} \z }xms;
    my $path = catfile $dir, $file;
    return unless -f $path;
    return unless (time - (stat $path)[9]) <= 12 * 60 * 60;
    return $path;
    }

    __END__
    A. Sinan Unur, Oct 25, 2005
    #10
  11. Naji wrote:
    > I appreciate the help.


    If you really did then you should have read and acted on the bit where I
    said:

    >> Dear Google-Groups victim, please quote some context!
    >> http://groups.google.com/googlegroups/posting_style.html#summarize
    >>
    >> Here's how:
    >> http://cfaj.freeshell.org/google/google-groups.html
    >>



    > I did find the online stat documentation,
    > however I am a Perl newbie and need to grasp some of the other
    > concepts. What would the If Statement look like if I'm trying to test
    > for files that have been modified in the last 24 hours? I will go
    > through some tutorials and try to figure it out on my own,


    Well please show your appreciation by following the newsgroup
    conventions that I pointed out.

    > but any help
    > or direction would be much appreciated.
    >


    if ((time - (stat 'filename')[9]) < 24*60*60) {
    print "recently modified/n";
    }
    RedGrittyBrick, Oct 25, 2005
    #11
  12. Feeble troll Kiralynne Schilitubi (AKA Purl Gurl AKA Godzilla) wrote:
    > A. Sinan Unur trolls:
    > > Purl Gurl wrote:

    >
    > I am curious. Are your knuckles often raw and sore?
    >
    > Purl Gurl


    http://www.catb.org/~esr/faqs/smart-questions.html#not_losing

    At least Xah (another longstanding Troll) shows occasional signs of
    learning something over the years!
    foo bar baz qux, Oct 25, 2005
    #12
  13. Naji

    Mothra Guest

    Hi A. Sinan Unur,

    A. Sinan Unur wrote:

    (Excellent advice snipped)

    > use File::Spec::Functions 'catfile';
    > sub plain_files_modified_in_last_12_hours {
    > my ($dir, $file) = @_;
    > return if $file =~ m{ \A \.{1,2} \z }xms;


    Just a quick question, any reason for the xms switches?
    (I like the \A and the \z, it's better that ^ and $ I confess
    I had to look them up)

    Mothra
    Mothra, Oct 25, 2005
    #13
  14. Mothra wrote:
    > Hi A. Sinan Unur,
    >
    > A. Sinan Unur wrote:
    >
    > (Excellent advice snipped)
    >
    >>use File::Spec::Functions 'catfile';
    >>sub plain_files_modified_in_last_12_hours {
    >> my ($dir, $file) = @_;
    >> return if $file =~ m{ \A \.{1,2} \z }xms;

    >
    > Just a quick question, any reason for the xms switches?
    > (I like the \A and the \z, it's better that ^ and $ I confess
    > I had to look them up)


    The /x option determines if whitespace in the pattern is literal or ignored so
    yes, it is required.

    The /m option determines if the ^ and $ anchors match the beginning and end of
    the string or lines within the string but they aren't used so no, it is NOT
    required.

    The /s option determines if the . character matches every character or every
    character except newline but is isn't used so no, it is NOT required.



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Oct 26, 2005
    #14
  15. Naji

    Mothra Guest

    Hi John,

    John W. Krahn wrote:
    > Mothra wrote:

    (snipped)
    >> Just a quick question, any reason for the xms switches?
    >> (I like the \A and the \z, it's better that ^ and $ I confess
    >> I had to look them up)

    >
    > The /x option determines if whitespace in the pattern is literal or
    > ignored so yes, it is required.
    >
    > The /m option determines if the ^ and $ anchors match the beginning
    > and end of the string or lines within the string but they aren't used
    > so no, it is NOT required.
    >
    > The /s option determines if the . character matches every character
    > or every character except newline but is isn't used so no, it is NOT
    > required.

    I missed the spaces in the pattern :-(

    Thanks for the explanation.

    Mothra
    Mothra, Oct 26, 2005
    #15
  16. Naji

    Guest

    A. Sinan Unur wrote:
    > Here is a better way (in the sense that it is correct, easier to
    > understand etc):
    > [snipped code]


    Hmmmmmm. Or maybe this:

    #!/usr/bin/perl
    use strict; use warnings;
    use IO::All;

    my $age = time - 12 * 60 * 60;

    my @files_to_email = io($ARGV[0] || '.')
    -> filter(sub { $_->mtime > $age })
    -> all_files(1);

    print map {"$_\n"} @files_to_email;

    __END__

    Basic error handling is provided by the module.
    , Oct 26, 2005
    #16
  17. "John W. Krahn" <> wrote in
    news:ZSy7f.63594$ir4.51055@edtnps90:

    > Mothra wrote:
    >> Hi A. Sinan Unur,
    >>
    >> A. Sinan Unur wrote:

    ....
    >>> return if $file =~ m{ \A \.{1,2} \z }xms;

    >>
    >> Just a quick question, any reason for the xms switches?
    >> (I like the \A and the \z, it's better that ^ and $ I confess
    >> I had to look them up)


    ....

    > The /m option determines if the ^ and $ anchors match the beginning
    > and end of the string or lines within the string but they aren't used
    > so no, it is NOT required.
    >
    > The /s option determines if the . character matches every character or
    > every character except newline but is isn't used so no, it is NOT
    > required.


    True. It was a little brainless typing on my part.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 26, 2005
    #17
  18. Purl Gurl <> wrote in
    news::

    > RedGrittyBrick wrote:
    >> Naji wrote:

    >
    > (snipped)
    >
    >> if ((time - (stat 'filename')[9]) < 24*60*60) {
    >> print "recently modified/n";
    >> }

    >
    > Both you and readers will be as surprised as I by benchmark results.
    >
    > Initially, I became curious if use of time and multiplication would
    > be less efficient than use of $^T and 86400 to compare Epoch
    > seconds. Test results don't disclose this but rather a real surprise
    > related to use of Perl's default time function.
    >
    > Purl Gurl
    >
    > I plugged these codes into benchmark:
    >
    > sub Time
    > {
    > timethese (100000,
    > {
    > 'Brick' =>
    > 'if (time > 24*60*60)
    > { $out1 = "true"; } ',
    >
    > 'PurlGurl' =>
    > 'if ($^T > 86400)
    > { $out2 = "true"; }',
    > } );
    > }


    You are comparing apples and oranges. From perldoc perlvar:

    $BASETIME
    $^T The time at which the program began running, in seconds since
    the epoch (beginning of 1970). The values returned by the -M,
    -A, and -C filetests are based on this value.

    Once the program starts running, the value of $^T is fixed. Whereas time
    computes the number of seconds elapsed since epoch in each invocation.
    Comparing the value of a constant to another constant is going to be
    faster than invoking a function and comparing the returned value to a
    constant. No mystery there.

    However, the two comparisons have different meaning. Consequently, speed
    cannot be a valid criterion for choosing between them.

    On the other hand:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use Benchmark 'timethese';

    my $out;

    timethese (-1, {
    Brick => sub { if (time > 24*60*60) { $out = "true"; } },
    PurlGurl => sub { if ($^T > 86400) { $out = "true"; } },
    });

    __END__

    D:\Home\asu1\UseNet\clpmisc> tt
    Benchmark: running Brick, PurlGurl for at least 1 CPU seconds...
    Brick: 2 wallclock secs ( 1.13 usr + 0.00 sys = 1.13 CPU) @
    2616767.11/s (n=2943863)
    PurlGurl: 2 wallclock secs ( 1.14 usr + 0.00 sys = 1.14 CPU) @
    1929893.95/s (n=2202009)

    Hmmmmm ...


    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 26, 2005
    #18
  19. wrote:
    > Naji wrote:

    [...]
    >> every time the attachment is updated. What would the syntax be for
    >> the if statement for code that would send the attachment if it has
    >> been modified in the past 12 hours? Any help would be appreciated.
    >> Thanks!

    >
    > I think that syntax would be like this (assuming you have the
    > attachment name in the variable $attachment):
    >
    > if(-M $attachment <= 0.5) {


    I was about to suggest the same when i double checked the documentation:

    -M Age of file in days when script started.
    -A Same for access time.
    -C Same for inode change time.

    Unfortunately there is no -X file test for modified time. If you really want
    modified time then you are stuck with stat().

    jue
    Jürgen Exner, Oct 26, 2005
    #19
  20. Naji

    Guest

    Jürgen Exner wrote:
    > wrote:
    > > I think that syntax would be like this (assuming you have the
    > > attachment name in the variable $attachment):
    > >
    > > if(-M $attachment <= 0.5) {

    >
    > I was about to suggest the same when i double checked the documentation:
    >
    > -M Age of file in days when script started.
    > -A Same for access time.
    > -C Same for inode change time.


    This is a confusing file test. There is some documentation that says
    that -M is the age, others say that it is the modification date.
    However, from what I noticed, there is more documentation that says it
    is the modification date.

    A Google search will reveal sites that agree that is is the
    modification date, and others that say that it is the age.

    I decided to write a test script (I assigned $0 to $file to make the
    test file become the perl file itself):

    #!/usr/bin/perl -w

    use strict;

    # Helper subroutine to format time:
    sub formatTime ($);

    # The file for the modification test:
    my $file = $0;
    # The number of seconds since the file was modified from the
    # -M file test:
    my $mSeconds = -M $file;

    # Convert from fraction of day to seconds.
    # 86400 is the number of seconds in a day.
    $mSeconds *= 86400;

    # Get the time the file was modified.
    # $seconds is the number of seconds from when the file was
    # modified to when the script started.
    my $mTime = $^T - $mSeconds;

    print "Stat: Last Modified for $file: ", formatTime((stat($file))[9]),
    "\n";
    print " -M: Last Modified for $file: ", getTime $mTime, "\n";

    # Helper subroutine to format time:
    sub formatTime ($) {
    my($second, $minute, $hour, $monthDay, $month, $year, $weekDay,
    $yearDay, $isDST) = localtime $_[0];
    $year += 1900;
    $month += 1;
    my $ampm;
    if($hour > 12) {
    $hour -= 12;
    $ampm = 'P.M.';
    } else {
    if($hour != 12) { # Noon is P.M.
    $ampm = 'A.M.';
    } else {
    if($hour == 0) { # Midnight is A.M.
    $hour = 12;
    $ampm = 'A.M.';
    } else {
    $ampm = 'P.M.';
    }
    }
    }

    return sprintf("%02d/%02d/%02d %02d:%02d:%02d $ampm", $month,
    $monthDay, $year, $hour, $minute, $second);
    }

    Results:
    Stat: Last Modified for Modified.pl: 10/26/2005 12:11:39 P.M.
    -M: Last Modified for Modified.pl: 10/26/2005 12:11:39 P.M.

    The test makes me think that the -M test returns the modification date.

    David
    , Oct 26, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rich Pasco
    Replies:
    2
    Views:
    19,312
    Rich Pasco
    Oct 9, 2003
  2. James
    Replies:
    2
    Views:
    388
    Chris Chilvers
    Oct 18, 2003
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,243
    Michael Borgwardt
    May 30, 2004
  4. Roedy Green

    Date last Accessed vs Date Modified

    Roedy Green, Feb 21, 2008, in forum: Java
    Replies:
    1
    Views:
    1,454
    Nigel Wade
    Feb 22, 2008
  5. Zhang Yuan

    Varibly modified type in switch statement

    Zhang Yuan, Dec 24, 2012, in forum: C Programming
    Replies:
    5
    Views:
    224
    Shao Miller
    Jan 14, 2013
Loading...

Share This Page