Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Syntax Problem...

Reply
Thread Tools

Syntax Problem...

 
 
Clifford Bracht
Guest
Posts: n/a
 
      01-08-2004
I'm tring to write a script that verifies if the time of the last
modification is more recent than the time of the last access. Because
I would like to know when any action (save, add text, delete text
etc.) has occurred to the file. But I think that I have some syntax
problems with either my if statement or the stat function becuase my
atime and mtime are the same number no matter what kind of changes I
make to the file. Do any of you guys have any solutions or advice
for what I have here:
Thanks in Advance

#!/usr/bin/perl -w
#perl -w

use strict;

my $filename = "C:/temp/test.txt";

open FILEHANDLE, $filename or die "Cannot open $filename,
err=\"$!\"\n";

#While file is open do the following...
while (<FILEHANDLE>)
{
#Assign $atime to access time & $mtime to modification time
#$atime and $mtime are refreshed after each time the info is gathered.

my $atime = (stat ($filename))[8] || die "Sorry, cannot stat! \n";
my $mtime = (stat ($filename))[9] || die "Sorry, cannot stat! \n";

#test output to see what is that is going to be compared.
print "$atime \n";
print "$mtime \n";

#Check to see if modification time is more recent then the time of
last access.
if ($mtime == $atime) #$mtime = $ctime)
{
print "$filename is OK! \n";
}
else
{
print "$filename has been modified. \n";
}
}
close FILEHANDLE;
print "$filename is done testing. \n";
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      01-08-2004
"Clifford Bracht" <> wrote in message
news: m...

[snip]

> #While file is open do the following...
> while (<FILEHANDLE>)
> {


your comment is misleading, i hope this is not your understanding
of what is happening

....
> my $atime = (stat ($filename))[8] || die "Sorry, cannot stat! \n";
> my $mtime = (stat ($filename))[9] || die "Sorry, cannot stat! \n";


maybe your filingsystem does not support both these fields.

gnari



 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-09-2004
Clifford Bracht <> wrote:

> I'm tring to write a script that verifies if the time of the last
> modification is more recent than the time of the last access.



A modification _is_ an access, _both_ timestamps are updated.

This has nothing to do with Perl.


> my
> atime and mtime are the same number no matter what kind of changes I
> make to the file.



That is how it is supposed to be right after the modification.

Are they different when you do a access (non-modifying) between
the modification and your Perl program?


> open FILEHANDLE, $filename or die "Cannot open $filename,



Why are you open()ing the file?

You do not need to open the file to examine its timestamps.


> #While file is open do the following...



That comment is misleading.

The file will _still_ be open, even after the while()
condition becomes false.

# while there are lines left in the file

Except now it repeats what is already said in the code, a sign
that it is a "bad" comment.


> while (<FILEHANDLE>)



Why are you looping through all the lines in the file?

You never use them in your program.


> my $atime = (stat ($filename))[8] || die "Sorry, cannot stat! \n";
> my $mtime = (stat ($filename))[9] || die "Sorry, cannot stat! \n";


my($atime, $mtime) = (stat $filename)[8] || die "Sorry, cannot stat! \n";


> if ($mtime == $atime) #$mtime = $ctime)



Huh?

What were you hoping that that line of code would do?


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Paul Boardman
Guest
Posts: n/a
 
      01-09-2004

"Clifford Bracht" <> wrote in message
news: m...
> I'm tring to write a script that verifies if the time of the last
> modification is more recent than the time of the last access.


<snip code>

Why are you opening the file and reading through it?

Try using the file test operators ("perldoc -f -X" for more info)

e.g.

my $filename = shift;
my $modified = -C $filename;
my $accessed = -A $filename;

print "Modified $modified\nAccessed $accessed\n";

Paul


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-09-2004
I myself <> wrote:
> Clifford Bracht <> wrote:



>> my $atime = (stat ($filename))[8] || die "Sorry, cannot stat! \n";
>> my $mtime = (stat ($filename))[9] || die "Sorry, cannot stat! \n";

>
> my($atime, $mtime) = (stat $filename)[8] || die "Sorry, cannot stat! \n";



Errr, make that:

my($atime, $mtime) = (stat $filename)[8,9] || die "Sorry, cannot stat! \n";


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Clifford Bracht
Guest
Posts: n/a
 
      01-09-2004
"Paul Boardman" <> wrote in message news:<>...
> "Clifford Bracht" <> wrote in message
> news: m...
> > I'm tring to write a script that verifies if the time of the last
> > modification is more recent than the time of the last access.

>
> <snip code>
>
>> Why are you opening the file and reading through it?

>
>

First off, Thank you to everyone for the suggestions and help.
I figured out a few of my mistakes shortly after I posted my message
and after working on the coding some I have come up with this:

#!/usr/bin/perl -w
#perl -w
$filename = "C:/temp/test.txt";
Set_File();

sub Set_File
{

open (FILEHANDLE, "<$filename") || die "Cannot open $filename,
err=\"$!\"\n";

if ($filename ne " \n")
{
$mtime = (stat ($filename))[9] || die "Sorry, cannot stat! \n";
Check_Mod();
}
}

sub Check_Mod
{
while(1)
{
$mtime_2 = (stat ($filename))[9] || die "Sorry, cannot stat! \n";

#View the two Mod times for test purposes.
print "previous_mtime = $mtime and mtime = $mtime_2 \n";

#Test to see if Modification have been made to file
if ($mtime_2 ne $mtime)
{
print "$filename has been modified.! \n";
Set_File();
}
else
{
print "$filename is OK. \n";
}

print "$filename is done testing. \n";
#sleep 60;
}
}

I have tested it and when I change the file it sends the correct
message and then runs through a new loop.

But it runs in an infinite loop that I would only like to run every 60
seconds, but when I put the code the line:
sleep 60; the script will not output anything??? Does anyone have any
suggestions. Do I have the sleep function in the correct place, or do
I need to go about this in a differe way??

I also don't know if I used proper Perl practices by calling the sub
routines the way that I did at the beginning and in the while loop, so
that might contribute to the problem?? Thanks
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      01-09-2004
Clifford Bracht wrote:
> "Paul Boardman" <> wrote in message news:<>...
>
>>"Clifford Bracht" <> wrote in message
>>news:. com...
>>
>>>I'm tring to write a script that verifies if the time of the last
>>>modification is more recent than the time of the last access.

>>
>><snip code>
>>
>>>Why are you opening the file and reading through it?

>>
>>

> First off, Thank you to everyone for the suggestions and help.
> I figured out a few of my mistakes shortly after I posted my message
> and after working on the coding some I have come up with this:
>
> #!/usr/bin/perl -w
> #perl -w
> $filename = "C:/temp/test.txt";

my $filename = 'C:/temp/test.txt';
my $mtime;
> Set_File();
>
> sub Set_File
> {
>
> open (FILEHANDLE, "<$filename") || die "Cannot open $filename,
> err=\"$!\"\n";


Again, as others have said and you have included in this post, you don't
need to.. or to make it clear DON'T open the file. You're not reading
the contents within the file, so don't open it.

You can use "-f" to see if the file exists.

>
> if ($filename ne " \n")
> {
> $mtime = (stat ($filename))[9] || die "Sorry, cannot stat! \n";
> Check_Mod();
> }
> }


Your subroutine could simply be replaced by:

die "$filename doesn't exist." if !-f $filename;
my $mtime = (stat ($filename))[9] || die "..."

Also, if you want to use subroutines, get in the habit of passing things
to them and not relying on global variables.

Set_file($filename); #is Set_file a good name for this sub??
Check_mod($filename);

sub Set_File
{
my $fname = shift;
..blah..
}
sub Check_Mod
{
my $fname = shift;
...blah blah blah...
}

>
> sub Check_Mod
> {
> while(1)
> {
> $mtime_2 = (stat ($filename))[9] || die "Sorry, cannot stat! \n";


Can't stat what?? or why??.. include $filename and $! in your error message.

my $mtime_2 = (stat ($filename))[9] || die "Sorry, cannot stat
$filename: $! \n";

>
> #View the two Mod times for test purposes.
> print "previous_mtime = $mtime and mtime = $mtime_2 \n";
>
> #Test to see if Modification have been made to file
> if ($mtime_2 ne $mtime)

Although it doesn't affect the result, since mtime and mtime_2 are
integers, you should use integer's equality (==).

> {
> print "$filename has been modified.! \n";
> Set_File();


There's no need to call Set_file, you already have the new modification
time in mtime_2, just update your mtime variable;

print "$filename has been modified.! \n";
$mtime = $mtime2;
> }
> else
> {
> print "$filename is OK. \n";
> }
>
> print "$filename is done testing. \n";
> #sleep 60;
> }
> }
>
> I have tested it and when I change the file it sends the correct
> message and then runs through a new loop.
>
> But it runs in an infinite loop that I would only like to run every 60
> seconds, but when I put the code the line:
> sleep 60; the script will not output anything??? Does anyone have any
> suggestions. Do I have the sleep function in the correct place, or do
> I need to go about this in a differe way??


Well, you'd need to wait for up to 60 seconds after the file is modified
before it'd print anything.

It's probably best to examine why you need to know that the file's
modification time has changed and how often you should check it. The
longer can wait to poll the file, the better. Usually find (man find or
perldoc File::Find) is a common method of finding out if a file has been
modified in the past number of seconds, hours, days.

>
> I also don't know if I used proper Perl practices by calling the sub
> routines the way that I did at the beginning and in the while loop, so
> that might contribute to the problem?? Thanks


You called them correctly, however the call in the while() wasn't needed
and using them at all wasn't really needed. In your example, the code is
so short that using subroutines doesn't help or hurt. If you're
learning about subroutines, then by all means use them a lot, and get
used to passing information to them and returning information from them.
After a while you'll determine when it's best to/not to use them.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-09-2004
Clifford Bracht <> wrote:
> "Paul Boardman" <> wrote in message news:<>...
>> "Clifford Bracht" <> wrote in message
>> news: m...
>> > I'm tring to write a script that verifies if the time of the last
>> > modification is more recent than the time of the last access.

>>
>> <snip code>
>>
>>> Why are you opening the file and reading through it?

>>
>>

> First off, Thank you to everyone for the suggestions and help.



But you are not going to answer the question that you were asked?

I don't think it was a rhetorical question.

It appears that you have a profound misunderstanding somewhere and
the answer to the question will help us figure out where you went
wrong so that we can help you.


> $filename = "C:/temp/test.txt";



> open (FILEHANDLE, "<$filename") || die "Cannot open $filename,
> err=\"$!\"\n";



Why are you open()ing the file?

You do not need to open() the file!


> if ($filename ne " \n")



Why is this test here?

How is it that you expect to have that value in $filename?

It is hardcoded above and never changed as far as I can see...


> #Test to see if Modification have been made to file
> if ($mtime_2 ne $mtime)



That is not the right test.

ne is for comparing strings, != is for comparing numbers, you have numbers:

if ($mtime_2 != $mtime)


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      01-09-2004

> Although it doesn't affect the result, since mtime and mtime_2 are
> integers, you should use integer's equality (==).


I meant inequality.. (!=)

 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      01-10-2004
On Thu, 08 Jan 2004 12:24:47 -0800, Clifford Bracht wrote:
> I'm tring to write a script that verifies if the time of the last
> modification is more recent than the time of the last access. Because
> I would like to know when any action (save, add text, delete text
> etc.) has occurred to the file. But I think that I have some syntax
> problems with either my if statement or the stat function becuase my
> atime and mtime are the same number no matter what kind of changes I
> make to the file.


You have a problem with the logic here; If you change a file _both_ the
access time _and_ the modification time of that file will be changed (to
the same value).

You have to access a file when modifying it, right?

Based on this, you should create a script which runs forever (...) and
does something if the current modification time is different from the
previous modification time.

Something like this should get you going (untested):

#!/usr/bin/perl
#
use strict;
use warnings;

my $filename = 'test.txt';
unless ( -e $filename ) {
die "The file '$filename' doesn't exist!\n";
}

my $sleep = 2; # How many seconds to sleep() between each test
my $prev_mtime = 0; # Previous modification time

while ( 1 ) {
my $mtime = ( stat($filename) )[9];
if ( $prev_mtime != 0 && $prev_mtime != $mtime ) {
# File has been modified
}
$prev_mtime = $mtime;
sleep( $sleep );
}


--
Tore Aursand <>
"Nothing is certain but death and taxes. Of the two, taxes happen
annually." -- Joel Fox
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57