[30298] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1541 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 13 21:09:54 2008

Date: Tue, 13 May 2008 18:09:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 13 May 2008     Volume: 11 Number: 1541

Today's topics:
    Re: A little Direction Please <RedGrittyBrick@SpamWeary.foo>
    Re: A little Direction Please <someone@example.com>
    Re: A little Direction Please <someone@example.com>
    Re: A little Direction Please <jurgenex@hotmail.com>
    Re: A little Direction Please <tadmc@seesig.invalid>
    Re: A little Direction Please <ben@morrow.me.uk>
        Assigning a value to pos to control regular expression  <google@markginsburg.com>
    Re: Assigning a value to pos to control regular express <someone@example.com>
    Re: Assigning a value to pos to control regular express <noreply@gunnar.cc>
    Re: Assigning a value to pos to control regular express xhoster@gmail.com
    Re: Assigning a value to pos to control regular express <google@markginsburg.com>
    Re: Constants across package boundaries <spamtrap@dot-app.org>
        help with auth dbi <jcharth@gmail.com>
    Re: infinite loop to monitor directories -one last ques <mr.vlad.dracula@gmail.com>
    Re: infinite loop to monitor directories -one last ques <1usa@llenroc.ude.invalid>
        infinite loop to monitor directories mr.vlad.dracula@gmail.com
    Re: infinite loop to monitor directories <1usa@llenroc.ude.invalid>
    Re: infinite loop to monitor directories <mr.vlad.dracula@gmail.com>
    Re: infinite loop to monitor directories <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 13 May 2008 19:09:10 +0100
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.foo>
Subject: Re: A little Direction Please
Message-Id: <QLadneqG3r7IRLTVnZ2dnUVZ8vGdnZ2d@bt.com>

Jürgen Exner wrote:
> RedGrittyBrick <RedGrittyBrick@SpamWeary.foo> wrote:
>> Andy wrote:
>>>          if ($line =~ m/^(.+226\s+0\s+-\s+.*)$/) {
>> Matching ^.+ is wasteful.
>> You don't need to capture the whole line using ().
>>
>>>               print OUTPUT "$1\n";
>> Unless you chomp your input you'll output an extra blank line.
> 
> My first thought, too. However because of the rather 'interesting' way
> he is printing the captured group instead of just the plain line he is
> loosing the newline in the pattern match. Therefore he has to add it
> back explicitely.
> 

Ah yes. Thanks for the correction.


-- 
RGB


------------------------------

Date: Tue, 13 May 2008 20:45:03 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: A little Direction Please
Message-Id: <j5nWj.2037$Yp.1686@edtnps92>

Ben Morrow wrote:
> Quoth Andy <Ramroop@gmail.com>:
>>
>>          if ($line =~ m/^(.+226\s+0\s+-\s+.*)$/) {
>>               print OUTPUT "$1\n";
>>          }
> 
> I would recommend splitting the line into a hash first, and then
> selecting lines based on that. Something like
> 
>     my @fields = qw/
>         date time c_ip 
>         cs_username cs_method cs_uri_stem 
>         sc_status sc_bytes cs_host
>     /;
> 
>     while (my $line = <$INPUT>) {
> 
>         # Here I assume fields are delimited by a single space, and
>         # spaces and newlines *never* appear in a field (not even inside
>         # quotes). If this isn't true, you probably want to use the
>         # Text::CSV_XS module, which can parse all sorts of
>         # <foo>-delimited files.
> 
>         my %record;
>         @record{@fields} = split / /, $line;
> 
>         $record{sc_status} == 226 
>             and $record{sc_bytes} == 0
>             and $record{cs_host} eq '-'

Because you are using "split / /, $line" $record{cs_host} will probably 
contain "-\n" instead of '-'.


>             or next;
> 
>         print $OUTPUT $line;
>     }


John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


------------------------------

Date: Tue, 13 May 2008 20:56:37 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: A little Direction Please
Message-Id: <9gnWj.2038$Yp.1809@edtnps92>

J=FCrgen Exner wrote:
> RedGrittyBrick <RedGrittyBrick@SpamWeary.foo> wrote:
>> Andy wrote:
>>>          if ($line =3D~ m/^(.+226\s+0\s+-\s+.*)$/) {
>> Matching ^.+ is wasteful.
>> You don't need to capture the whole line using ().
>>
>>>               print OUTPUT "$1\n";
>> Unless you chomp your input you'll output an extra blank line.
>=20
> My first thought, too. However because of the rather 'interesting' way
> he is printing the captured group instead of just the plain line he is
> loosing the newline in the pattern match. Therefore he has to add it
> back explicitely.

The \s+ at the end is greedy and will match everything at the end=20
including the newline unless there is a non-whitespace character after=20
it that .* will match.


John
--=20
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


------------------------------

Date: Tue, 13 May 2008 21:14:13 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: A little Direction Please
Message-Id: <751k2410abi8lk9m0sfpoopisg7c4vahlr@4ax.com>

"John W. Krahn" <someone@example.com> wrote:
>Jürgen Exner wrote:
>> RedGrittyBrick <RedGrittyBrick@SpamWeary.foo> wrote:
>>> Andy wrote:
>>>>          if ($line =~ m/^(.+226\s+0\s+-\s+.*)$/) {
>>> Matching ^.+ is wasteful.
>>> You don't need to capture the whole line using ().
>>>
>>>>               print OUTPUT "$1\n";
>>> Unless you chomp your input you'll output an extra blank line.
>> 
>> My first thought, too. However because of the rather 'interesting' way
>> he is printing the captured group instead of just the plain line he is
>> loosing the newline in the pattern match. Therefore he has to add it
>> back explicitely.
>
>The \s+ at the end is greedy and will match everything at the end 
>including the newline unless there is a non-whitespace character after 
>it that .* will match.

You are right. I was looking at the trailing .* only and didn't dissect
the RE beyond that. 
This RE certainly has some Interesting side effects. 

jue


------------------------------

Date: Tue, 13 May 2008 18:11:29 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: A little Direction Please
Message-Id: <slrng2k811.vkd.tadmc@tadmc30.sbcglobal.net>

Andy <Ramroop@gmail.com> wrote:

> Subject: A little Direction Please


Please put the subject of your article in the Subject of your article.


> while (<INPUT>) {
>  my $line = $_;


If you want the line in $line, then put it in $line rather than
put it somewhere else, only to then copy it to $line:

   while ( my $line = <INPUT> )  {


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Tue, 13 May 2008 23:53:27 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: A little Direction Please
Message-Id: <7oipf5-47k1.ln1@osiris.mauzo.dyndns.org>


Quoth "John W. Krahn" <krahnj@telus.net>:
> Ben Morrow wrote:
> > 
> >     while (my $line = <$INPUT>) {
<snip>
> >         my %record;
> >         @record{@fields} = split / /, $line;
> > 
> >         $record{sc_status} == 226 
> >             and $record{sc_bytes} == 0
> >             and $record{cs_host} eq '-'
> 
> Because you are using "split / /, $line" $record{cs_host} will probably 
> contain "-\n" instead of '-'.

Good point. I'm too used to -l :)

Ben

-- 
"Faith has you at a disadvantage, Buffy."
"'Cause I'm not crazy, or 'cause I don't kill people?"
"Both, actually."
                                                         [ben@morrow.me.uk]


------------------------------

Date: Tue, 13 May 2008 14:15:17 -0700 (PDT)
From: Mark <google@markginsburg.com>
Subject: Assigning a value to pos to control regular expression matching
Message-Id: <5d8246ae-a288-47f0-ae90-190c070458e9@y18g2000pre.googlegroups.com>

use strict ;
use warnings ;

my $txt = 'abc Start something' ;
my $startpoint = index($txt,'Start') ;
die "Start point not found" if $startpoint < 0 ;

$_ = $txt ;

pos = $startpoint ;
if (/\G(.*)/g) {
    print "matched=$1\n" ; # prints "matched=Start something"
}
else {
    print "no match\n" ; }

pos = $startpoint ;
if ($txt =~ /\G(.*)/g) {
    print "matched=$1\n" ; # prints "matched=abc Start something"

}
else {
    print "no match\n" ;
}

I expected the matched part in both cases to be the same.  The second
case doesn't seem to honor the value of pos.

Can someone please explain.


------------------------------

Date: Tue, 13 May 2008 21:31:11 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Assigning a value to pos to control regular expression matching
Message-Id: <zMnWj.2048$Yp.1692@edtnps92>

Mark wrote:
> use strict ;
> use warnings ;
> 
> my $txt = 'abc Start something' ;
> my $startpoint = index($txt,'Start') ;
> die "Start point not found" if $startpoint < 0 ;
> 
> $_ = $txt ;
> 
> pos = $startpoint ;
> if (/\G(.*)/g) {
>     print "matched=$1\n" ; # prints "matched=Start something"
> }
> else {
>     print "no match\n" ; }
> 
> pos = $startpoint ;

You are now matching with the $txt variable instead of the $_ variable 
so that should be:

pos( $txt ) = $startpoint;

> if ($txt =~ /\G(.*)/g) {
>     print "matched=$1\n" ; # prints "matched=abc Start something"
> 
> }
> else {
>     print "no match\n" ;
> }
> 
> I expected the matched part in both cases to be the same.  The second
> case doesn't seem to honor the value of pos.
> 
> Can someone please explain.


John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


------------------------------

Date: Tue, 13 May 2008 23:34:43 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Assigning a value to pos to control regular expression matching
Message-Id: <68ufo3F2ue994U1@mid.individual.net>

Mark wrote:
> use strict ;
> use warnings ;
> 
> my $txt = 'abc Start something' ;
> my $startpoint = index($txt,'Start') ;
> die "Start point not found" if $startpoint < 0 ;
> 
> $_ = $txt ;
> 
> pos = $startpoint ;
> if (/\G(.*)/g) {
>     print "matched=$1\n" ; # prints "matched=Start something"
> }
> else {
>     print "no match\n" ; }
> 
> pos = $startpoint ;
> if ($txt =~ /\G(.*)/g) {
>     print "matched=$1\n" ; # prints "matched=abc Start something"
> 
> }
> else {
>     print "no match\n" ;
> }
> 
> I expected the matched part in both cases to be the same.

Try

     pos($txt) = $startpoint;

in the second case.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: 13 May 2008 21:38:39 GMT
From: xhoster@gmail.com
Subject: Re: Assigning a value to pos to control regular expression matching
Message-Id: <20080513173842.283$t1@newsreader.com>

Mark <google@markginsburg.com> wrote:
>
> pos = $startpoint ;

Using pos without an argument implicitly uses $_ as the argument.
Thus this resets pos for the next time a regex is done on $_.

Since you want to use $txt, not $_, for the next match,
change that to:

pos $txt = $startpoint;



> if ($txt =~ /\G(.*)/g) {
>     print "matched=$1\n" ; # prints "matched=abc Start something"
>
> }
> else {
>     print "no match\n" ;
> }
>
> I expected the matched part in both cases to be the same.  The second
> case doesn't seem to honor the value of pos.
>
> Can someone please explain.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


------------------------------

Date: Tue, 13 May 2008 14:43:15 -0700 (PDT)
From: Mark <google@markginsburg.com>
Subject: Re: Assigning a value to pos to control regular expression matching
Message-Id: <3af22833-d3d7-4b33-bad8-e75b864e537e@y18g2000pre.googlegroups.com>

On May 13, 2:15=A0pm, Mark <goo...@markginsburg.com> wrote:
> use strict ;
> use warnings ;
>
> my $txt =3D 'abc Start something' ;
> my $startpoint =3D index($txt,'Start') ;
> die "Start point not found" if $startpoint < 0 ;
>
> $_ =3D $txt ;
>
> pos =3D $startpoint ;
> if (/\G(.*)/g) {
> =A0 =A0 print "matched=3D$1\n" ; # prints "matched=3DStart something"}
>
> else {
> =A0 =A0 print "no match\n" ; }
>
> pos =3D $startpoint ;
> if ($txt =3D~ /\G(.*)/g) {
> =A0 =A0 print "matched=3D$1\n" ; # prints "matched=3Dabc Start something"
>
> }
>
> else {
> =A0 =A0 print "no match\n" ;
>
> }
>
> I expected the matched part in both cases to be the same. =A0The second
> case doesn't seem to honor the value of pos.
>
> Can someone please explain.

Thank you to all that responded.


------------------------------

Date: Tue, 13 May 2008 17:31:32 -0400
From: Sherman Pendley <spamtrap@dot-app.org>
Subject: Re: Constants across package boundaries
Message-Id: <m1wslxyjpn.fsf@dot-app.org>

bernie@fantasyfarm.com writes:

> On May 13, 9:02 am, Joost Diepenmaat <jo...@zeekat.nl> wrote:
>> ber...@fantasyfarm.com writes:
>
>> I think you'd rather just export the constants to any package that
>> needs them, instead of making them truly global (which would be more
>> or less equivalent to exporting them to *all* packages)
>
> Can you export from main *to* a package?  Using Exporter in this way
> looks like it is a little confusing (it might become clearer after
> another six readings of the man page..:o)).  I guess I could do
> something along the lines of putting require Exporter in my main
> program, setting up @EXPORT_OK in the main program the "constants" I
> want the interior packages to be able to import, and then do
> a ::import(stuff I need) after the 'package' for the interior object-
> section?  You're right: that kind of approach would make the interior
> packages be more self-contained and clearer (by making explicit that
> they're pulling in those vbls from "main::")

They really shouldn't be pulling them from main:: IMHO. I'd rather put
the constants in a module of their own, which inherits from Exporter, and
include them in that module's @EXPORT_OK.

Then you'd just use() your constants module anywhere you need access to
them, including your main script.

sherm--

-- 
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net


------------------------------

Date: Tue, 13 May 2008 13:50:34 -0700 (PDT)
From: joe <jcharth@gmail.com>
Subject: help with auth dbi
Message-Id: <c22d8b7d-b8fc-4fb3-b6b4-875109e9bf24@y38g2000hsy.googlegroups.com>

Hello I have an old linux box I need to enable auth dbi does anyone
know what sources do i need to do this? I believe I will need mod_perl
and auth dbi and apache but havent done this before. Thanks.


------------------------------

Date: Tue, 13 May 2008 15:09:16 -0700 (PDT)
From: dracula <mr.vlad.dracula@gmail.com>
Subject: Re: infinite loop to monitor directories -one last question
Message-Id: <3ff67dde-aca5-4106-bd08-c3c789823bcd@b9g2000prh.googlegroups.com>

I read the documentation for file::find but still a little unclear.

Would this be the only thing I need to skip subdirectories and hidden
files/directories?
find( { wanted => \&process, no_chdir => 1 }, @directories );

If so, then I don't need this line then right?.
return if not -f or /^\./;

please advise and thanks in advance.
:)

<snip>
>
> > #!/usr/bin/perl
>
> > use warnings;
> > use strict;
> > use File::Find;
>
> > use constant SECONDS_PER_DAY => 24*60*60;
>
> > my @directories = ( "$ENV{HOME}/Src/Test" );
>
> > while ( 1 ) {
> >     find( { wanted => \&process, no_chdir => 1 }, @directories );
> >     sleep 1;
>
> > }
>
> > sub process {
> >     return if not -f or /^\./;
> >     my $mtime = -M;
> >     my $age = $^T - $mtime * SECONDS_PER_DAY ;
>
> >     if ( time - $age > 60 ) {
> >         print "$_ : $age\n";
> >     }
>
> > }

<snip>




On May 13, 11:18 am, dracula <mr.vlad.drac...@gmail.com> wrote:
> Thank you very much. I appreciate your inputs and suggestions.
>
> /dracula
>
> On May 13, 10:50 am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
>
> > mr.vlad.drac...@gmail.com wrote in news:5ff50268-c5d2-41d3-93dd-
> > cf92ce7f0...@w4g2000prd.googlegroups.com:
>
> > > Do you think you can point me on the right direction?
>
> > > Desired result: Monitor directories indefinitely, print on screen if a
> > > file found is older than 1 minute.
>
> > ...
>
> > > #!/usr/bin/perl
>
> > > use warnings;
> > > use strict;
> > > use File::Find;
>
> > > my @directories = ("/home/foo/dir1","/home/foo/dir2");
> > > my $MINUTES = 0.0007; #<-1 min expressed in days
> > > my $condition = 1;
>
> > > #supposedly loops forever
> > > while ($condition == 1){
>
> > while ( 1 ) {
>
> > would get rid of the unnecessary variable $condition.
>
> > >                  find(\&process, @directories, no_chdir => 1);
> > > }
>
> > They syntax of your call is incorrect. From perldoc File::Find:
>
> > find
> >       find(\&wanted,  @directories);
> >       find(\%options, @directories);
>
> > You would have noticed this if you had used some debugging print
> > statements:
>
> > Can't stat no_chdir: No such file or directory
> > Can't stat 1: No such file or directory
>
> > find( { wanted => \&process, no_chdir => 1 }, @directories );
>
> > > sub process {
> > >      #only files, not directories.  skip . and .. files
> > >         if (-f $_ and !/^\./){
> > >                if (-M $_ >= $MINUTES){
> > >                      print "$File::Find::name\n";
>
> > perldoc -f -X
>
> >  -M  Script start time minus file modification time, in days.
>
> > Now, the main problem. You have
>
> > > my $MINUTES = 0.0007; #<-1 min expressed in days
>
> > If you create a new file, -M on that file will return a negative number.
> > You have assigned to $MINUTES a positive number so the condition
>
> > -M >= $MINUTES
>
> > will never hold for any file created after the script has been started.
>
> > Because of this logic error, I am a bit confused about what you really
> > want. Assuming you mean what you say about files "older than one
> > minute", you need to compare to current time not to -M.
>
> > From perldoc perlvar:
>
> >   $BASETIME
> >   $^T     The time at which the program began running, in seconds since
>
> > So, to find the files older than 60 seconds, you would use the
> > following:
>
> > #!/usr/bin/perl
>
> > use warnings;
> > use strict;
> > use File::Find;
>
> > use constant SECONDS_PER_DAY => 24*60*60;
>
> > my @directories = ( "$ENV{HOME}/Src/Test" );
>
> > while ( 1 ) {
> >     find( { wanted => \&process, no_chdir => 1 }, @directories );
> >     sleep 1;
>
> > }
>
> > sub process {
> >     return if not -f or /^\./;
> >     my $mtime = -M;
> >     my $age = $^T - $mtime * SECONDS_PER_DAY ;
>
> >     if ( time - $age > 60 ) {
> >         print "$_ : $age\n";
> >     }
>
> > }
>
> > __END__
>
> > --
> > A. Sinan Unur <1...@llenroc.ude.invalid>
> > (remove .invalid and reverse each component for email address)
>
> > comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/



------------------------------

Date: Tue, 13 May 2008 22:17:39 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: infinite loop to monitor directories -one last question
Message-Id: <Xns9A9DBA18752BFasu1cornelledu@127.0.0.1>

dracula <mr.vlad.dracula@gmail.com> wrote in
news:3ff67dde-aca5-4106-bd08-c3c789823bcd@b9g2000prh.googlegroups.com: 

[ Do *not* top-post and do *not* quote messages in full ]

> I read the documentation for file::find but still a little unclear.

The module is File::Find. Case matters.

> Would this be the only thing I need to skip subdirectories and hidden
> files/directories?
> find( { wanted => \&process, no_chdir => 1 }, @directories );

Of course not. What gave you that impression.

> If so, then I don't need this line then right?.
> return if not -f or /^\./;

You do need that line in sub process to skip things that are not plain 
files or files whose names begin with a dot.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/


------------------------------

Date: Tue, 13 May 2008 11:43:04 -0700 (PDT)
From: mr.vlad.dracula@gmail.com
Subject: infinite loop to monitor directories
Message-Id: <5ff50268-c5d2-41d3-93dd-cf92ce7f01f3@w4g2000prd.googlegroups.com>

Do you think you can point me on the right direction?

Desired result: Monitor directories indefinitely, print on screen if a
file found is older than 1 minute.  It's supposed to bypass
subdirectories, hidden files/directories as well.

Problem: if a file is there already on any of the directories, it will
print out the file just fine.  However, if I delete that file and drop
another one, wait a minute or more, nothing displays. It's not really
going inside the directories after the first time.

Thank you...

#!/usr/bin/perl

use warnings;
use strict;
use File::Find;

my @directories = ("/home/foo/dir1","/home/foo/dir2");
my $MINUTES = 0.0007; #<-1 min expressed in days
my $condition = 1;

#supposedly loops forever
while ($condition == 1){
   	    	find(\&process, @directories, no_chdir => 1);
}


sub process {

	#only files, not directories.  skip . and .. files

        if (-f $_ and !/^\./){

               if (-M $_ >= $MINUTES){

                	print "$File::Find::name\n";

                   }

       }

     sleep 1;

 }


------------------------------

Date: Tue, 13 May 2008 20:50:38 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: infinite loop to monitor directories
Message-Id: <Xns9A9DAB57CB7C1asu1cornelledu@127.0.0.1>

mr.vlad.dracula@gmail.com wrote in news:5ff50268-c5d2-41d3-93dd-
cf92ce7f01f3@w4g2000prd.googlegroups.com:

> Do you think you can point me on the right direction?
> 
> Desired result: Monitor directories indefinitely, print on screen if a
> file found is older than 1 minute.
 ...

> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> use File::Find;
> 
> my @directories = ("/home/foo/dir1","/home/foo/dir2");
> my $MINUTES = 0.0007; #<-1 min expressed in days


> my $condition = 1;
> 
> #supposedly loops forever
> while ($condition == 1){

while ( 1 ) {

would get rid of the unnecessary variable $condition.

>                  find(\&process, @directories, no_chdir => 1);
> }

They syntax of your call is incorrect. From perldoc File::Find:

find
      find(\&wanted,  @directories);
      find(\%options, @directories);

You would have noticed this if you had used some debugging print 
statements:

Can't stat no_chdir: No such file or directory
Can't stat 1: No such file or directory

find( { wanted => \&process, no_chdir => 1 }, @directories );

> sub process {
>      #only files, not directories.  skip . and .. files
>         if (-f $_ and !/^\./){
>                if (-M $_ >= $MINUTES){
>                      print "$File::Find::name\n";

perldoc -f -X

 -M  Script start time minus file modification time, in days.

Now, the main problem. You have

> my $MINUTES = 0.0007; #<-1 min expressed in days

If you create a new file, -M on that file will return a negative number. 
You have assigned to $MINUTES a positive number so the condition

-M >= $MINUTES

will never hold for any file created after the script has been started.

Because of this logic error, I am a bit confused about what you really 
want. Assuming you mean what you say about files "older than one 
minute", you need to compare to current time not to -M.

From perldoc perlvar:

  $BASETIME
  $^T     The time at which the program began running, in seconds since

So, to find the files older than 60 seconds, you would use the 
following:

#!/usr/bin/perl

use warnings;
use strict;
use File::Find;

use constant SECONDS_PER_DAY => 24*60*60;

my @directories = ( "$ENV{HOME}/Src/Test" );

while ( 1 ) {
    find( { wanted => \&process, no_chdir => 1 }, @directories );
    sleep 1;
}

sub process {
    return if not -f or /^\./;
    my $mtime = -M;
    my $age = $^T - $mtime * SECONDS_PER_DAY ;

    if ( time - $age > 60 ) {
        print "$_ : $age\n";
    }
}

__END__


-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/


------------------------------

Date: Tue, 13 May 2008 14:18:03 -0700 (PDT)
From: dracula <mr.vlad.dracula@gmail.com>
Subject: Re: infinite loop to monitor directories
Message-Id: <3f96d738-a527-4f3b-bd48-e279f295d46c@b9g2000prh.googlegroups.com>

Thank you very much. I appreciate your inputs and suggestions.

/dracula


On May 13, 10:50 am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> mr.vlad.drac...@gmail.com wrote in news:5ff50268-c5d2-41d3-93dd-
> cf92ce7f0...@w4g2000prd.googlegroups.com:
>
> > Do you think you can point me on the right direction?
>
> > Desired result: Monitor directories indefinitely, print on screen if a
> > file found is older than 1 minute.
>
> ...
>
> > #!/usr/bin/perl
>
> > use warnings;
> > use strict;
> > use File::Find;
>
> > my @directories = ("/home/foo/dir1","/home/foo/dir2");
> > my $MINUTES = 0.0007; #<-1 min expressed in days
> > my $condition = 1;
>
> > #supposedly loops forever
> > while ($condition == 1){
>
> while ( 1 ) {
>
> would get rid of the unnecessary variable $condition.
>
> >                  find(\&process, @directories, no_chdir => 1);
> > }
>
> They syntax of your call is incorrect. From perldoc File::Find:
>
> find
>       find(\&wanted,  @directories);
>       find(\%options, @directories);
>
> You would have noticed this if you had used some debugging print
> statements:
>
> Can't stat no_chdir: No such file or directory
> Can't stat 1: No such file or directory
>
> find( { wanted => \&process, no_chdir => 1 }, @directories );
>
> > sub process {
> >      #only files, not directories.  skip . and .. files
> >         if (-f $_ and !/^\./){
> >                if (-M $_ >= $MINUTES){
> >                      print "$File::Find::name\n";
>
> perldoc -f -X
>
>  -M  Script start time minus file modification time, in days.
>
> Now, the main problem. You have
>
> > my $MINUTES = 0.0007; #<-1 min expressed in days
>
> If you create a new file, -M on that file will return a negative number.
> You have assigned to $MINUTES a positive number so the condition
>
> -M >= $MINUTES
>
> will never hold for any file created after the script has been started.
>
> Because of this logic error, I am a bit confused about what you really
> want. Assuming you mean what you say about files "older than one
> minute", you need to compare to current time not to -M.
>
> From perldoc perlvar:
>
>   $BASETIME
>   $^T     The time at which the program began running, in seconds since
>
> So, to find the files older than 60 seconds, you would use the
> following:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
> use File::Find;
>
> use constant SECONDS_PER_DAY => 24*60*60;
>
> my @directories = ( "$ENV{HOME}/Src/Test" );
>
> while ( 1 ) {
>     find( { wanted => \&process, no_chdir => 1 }, @directories );
>     sleep 1;
>
> }
>
> sub process {
>     return if not -f or /^\./;
>     my $mtime = -M;
>     my $age = $^T - $mtime * SECONDS_PER_DAY ;
>
>     if ( time - $age > 60 ) {
>         print "$_ : $age\n";
>     }
>
> }
>
> __END__
>
> --
> A. Sinan Unur <1...@llenroc.ude.invalid>
> (remove .invalid and reverse each component for email address)
>
> comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/



------------------------------

Date: Wed, 14 May 2008 00:01:55 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: infinite loop to monitor directories
Message-Id: <38jpf5-47k1.ln1@osiris.mauzo.dyndns.org>


Quoth "A. Sinan Unur" <1usa@llenroc.ude.invalid>:
> 
> If you create a new file, -M on that file will return a negative number. 
> You have assigned to $MINUTES a positive number so the condition
> 
> -M >= $MINUTES
> 
> will never hold for any file created after the script has been started.
> 
> Because of this logic error, I am a bit confused about what you really 
> want. Assuming you mean what you say about files "older than one 
> minute", you need to compare to current time not to -M.
> 
> From perldoc perlvar:
> 
>   $BASETIME
>   $^T     The time at which the program began running, in seconds since
> 
> So, to find the files older than 60 seconds, you would use the 
> following:
> 
<snip>
>     my $mtime = -M;
>     my $age = $^T - $mtime * SECONDS_PER_DAY ;
> 
>     if ( time - $age > 60 ) {

 ...or forget both -M and $^T (especially since converting a time to a
float and back probably isn't a good idea) and just use

    use File::stat;

    if (time - stat($_)->mtime <= 60)

Ben

-- 
        I must not fear. Fear is the mind-killer. I will face my fear and
        I will let it pass through me. When the fear is gone there will be 
        nothing. Only I will remain.
ben@morrow.me.uk                                          Frank Herbert, 'Dune'


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 1541
***************************************


home help back first fref pref prev next nref lref last post