date modified if statement

N

Naji

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!
 
J

J. Gleixner

Naji said:
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
 
N

Naji

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.
 
I

Ian Wilson

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
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.
 
P

Paul Lalli

Naji said:
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
 
W

wisefamily

Naji said:
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
 
M

Mothra

Naji said:
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
 
N

Naji

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.
 
N

Naji

Thank you for your help! I appreciate it a lot!!!!
Naji said:
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
 
A

A. Sinan Unur

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.

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__
 
R

RedGrittyBrick

Naji said:
I appreciate the help.

If you really did then you should have read and acted on the bit where I
said:
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";
}
 
M

Mothra

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
 
J

John W. Krahn

Mothra said:
Hi A. Sinan Unur,

A. Sinan Unur wrote:

(Excellent advice 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.



John
 
M

Mothra

Hi John,
Mothra wrote: (snipped)

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
 
U

usenet

A. Sinan Unur said:
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.
 
A

A. Sinan Unur

....

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

A. Sinan Unur

RedGrittyBrick said:
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 ...
 
J

Jürgen Exner

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
 
W

wisefamily

Jürgen Exner said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top