Re: Syntax Problem...

From: J. Gleixner (glex_nospam_at_qwest.invalid)
Date: 01/09/04


Date: Fri, 09 Jan 2004 15:31:25 -0600

Clifford Bracht wrote:
> "Paul Boardman" <Paul.E.Boardman@umist.ac.uk> wrote in message news:<3ffe6eb4@news.umist.ac.uk>...
>
>>"Clifford Bracht" <beaton@franklincollege.edu> wrote in message
>>news:41e4115.0401081224.315618ae@posting.google.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.



Relevant Pages

  • RE: Email and Fax a Report
    ... Opening a report in a loop seems odd, ... By placing the sub on its own, ... Dim objOutlook As Outlook.Application ... Set newMail = objOutlook.CreateItem ...
    (microsoft.public.access.modulesdaovba)
  • Re: arrays verodern
    ... interessant wenn SUB ... unnötige Bytes in der Schleife hat, ... welches LOOP, ... mov ecx, CCount ...
    (de.comp.lang.delphi.misc)
  • Re: Recognizing When An IE window changes URLs and closing the IE
    ... lpClassName As String, ByVal lpWindowName As String) As Long ... Private Sub x_NewWindow2 ... Dim ie1 As New IEClass ... On Error GoTo Here ' Error handling sequence that breaks the loop, ...
    (microsoft.public.excel.programming)
  • Re: Trying to "Drop-up" a dropped-down combobox
    ... Thanks for the warning about the continuous loop problem. ... reflected an attempt I had made to solve the dropdown issue. ... > Private Sub Combo2_NotInList(NewData As String, Response As Integer) ...
    (microsoft.public.access.formscoding)
  • Re: MKDir not working
    ... Public Sub MkDirs ... first argument of the Split function in the active loop statement; ... track and return a Boolean status from the function. ... MkDirs = MkDirs + Err.Number ...
    (microsoft.public.vb.general.discussion)

Loading