Scan/Parse directory for newest file, print file timestamp and compute date/time difference - Some advice please

2011-03-02 Thread newbie01 perl
Hi all,

Am wanting to get some advise on how to write this Perl script.

I have a backup directory that I have to check for the existence of file/s
and if the latest file that exist there is 2 days old, that means I have a
problem and had to send an email notification.

To illustrate if, for example, a directory named /backup have the following
files and their timestamps:

/backup/file.01 01-Jan-2011 0500
/backup/file.02 02-Jan-2011 0500
/backup/file.03 03-Jan-2011 0500
..
..
/backup/file.31 31-Jan-2011 0500
/backup/file.32 01-Feb-2011 0500
/backup/file.33 02-Feb-2011 0500

I want the script to check the most recent file in a backup directory, i.e.
newest, for example /backup/file.33 and display the timestamp, i.e. date and
time, of the file. Then I want to compare it with today's date and display
the date/time difference in n days n hours n minutes. If the file is more
than 2 days old, that should mean that the backup did not run so I have to
send an email notification.

So for example, if today is 04-Feb-2011 1000 0800 and there is no backup
file created on 03-Feb-2011, when the script runs, it should detect that the
most recent backup file is file.33, computed that it is older than 2 days
and then send en email notification.

Sample output from the script should be as below:

===

Checking last backup file on 04-Feb-2011 10:05:
   Last backup file = /backup/file.33
   Timestamp = 02-Feb-2011 05:00
   The file is = 2 days 5 hour/s 5 minute/s old
   Send email notification to check backup

===

My main hurdle is the file timestamp and date arithmetic part. Frm
Google'ing, am leaning towards using stat which am hoping will work on both
Unix and Windows.

Some guidance will be much appreciated. Thanks in advance.


Re: Scan/Parse directory for newest file, print file timestamp and compute date/time difference - Some advice please

2011-03-02 Thread Shawn H Corey

On 11-03-02 11:34 AM, newbie01 perl wrote:

My main hurdle is the file timestamp and date arithmetic part. Frm
Google'ing, am leaning towards using stat which am hoping will work on both
Unix and Windows.

Some guidance will be much appreciated. Thanks in advance.


Use the stat function to get the mtime of the files.  The one with the 
biggest is the most recent.


Compare it to now and if now  mtime + 2 * 24 * 60 * 60 then output your 
message.


Use strftime() from POSIX to change the epoch seconds to human readable 
form.


See:
perldoc -f stat
perldoc -f time
perldoc -f localtime
pelrdoc POSIX and search for /strftime/.


--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early  often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




strawberry file end

2011-03-02 Thread Christian Henkel
Hi perlers,

I am using strawberry portable for windows.

In my tutorial they told my to read strings from keyboard (@array = STDIN)
and told my to press ctrl+Z for the file end. But pressing ctrl+Z doesn't do
anything. Can someone tell my what I have to do to simulate the EOF? Ctrl+D
works for ending the file, but the hole script crashes, too.

-- 
Yours sincerely,

Christian Henkel


www.HenkelChristian.de http://www.HenkelChristian.de?lang=en
Tel: +49 176 63247825


Re: Scan/Parse directory for newest file, print file timestamp and compute date/time difference - Some advice please

2011-03-02 Thread shawn wilson
First, use File::Find to get your info (or you could use system( ls -l ) and
split - either way). To compare your time stamps, use DateTime and do dt1 -
dt2


Re: Scan/Parse directory for newest file, print file timestamp and compute date/time difference - Some advice please

2011-03-02 Thread Shawn H Corey

On 11-03-02 12:12 PM, shawn wilson wrote:

First, use File::Find to get your info (or you could use system( ls -l ) and
split - either way). To compare your time stamps, use DateTime and do dt1 -
dt2



No.  If you can do all your calculations using seconds from the epoch, 
then do so.  Only convert them to human-readable form for the output.



--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early  often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Scan/Parse directory for newest file, print file timestamp and compute date/time difference - Some advice please

2011-03-02 Thread shawn wilson
On Mar 2, 2011 12:16 PM, Shawn H Corey shawnhco...@gmail.com wrote:

 On 11-03-02 12:12 PM, shawn wilson wrote:

 First, use File::Find to get your info (or you could use system( ls -l )
and
 split - either way). To compare your time stamps, use DateTime and do dt1
-
 dt2


 No.  If you can do all your calculations using seconds from the epoch,
then do so.  Only convert them to human-readable form for the output.


Seems like more of a pain for a modest gain in speed to me. I like find and
dt because its quick and easy. Either way though.


Re: Scan/Parse directory for newest file, print file timestamp and compute date/time difference - Some advice please

2011-03-02 Thread Shawn H Corey

On 11-03-02 12:31 PM, shawn wilson wrote:

Seems like more of a pain for a modest gain in speed to me. I like find and
dt because its quick and easy. Either way though.


No, because you don't have to worry about daylight-savings time or 
switching time zones.  Seconds from the epoch is the same on all 
computers and in all time zones.



--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early  often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Scan/Parse directory for newest file, print file timestamp and compute date/time difference - Some advice please

2011-03-02 Thread Jim Gibson
On 3/2/11 Wed  Mar 2, 2011  8:34 AM, newbie01 perl
newbie01.p...@gmail.com scribbled:

 Hi all,
 
 Am wanting to get some advise on how to write this Perl script.
 
 I have a backup directory that I have to check for the existence of file/s
 and if the latest file that exist there is 2 days old, that means I have a
 problem and had to send an email notification.
 
 To illustrate if, for example, a directory named /backup have the following
 files and their timestamps:
 
 /backup/file.01 01-Jan-2011 0500
 /backup/file.02 02-Jan-2011 0500
 /backup/file.03 03-Jan-2011 0500
 ..
 ..
 /backup/file.31 31-Jan-2011 0500
 /backup/file.32 01-Feb-2011 0500
 /backup/file.33 02-Feb-2011 0500
 
 I want the script to check the most recent file in a backup directory, i.e.
 newest, for example /backup/file.33 and display the timestamp, i.e. date and
 time, of the file. Then I want to compare it with today's date and display
 the date/time difference in n days n hours n minutes. If the file is more
 than 2 days old, that should mean that the backup did not run so I have to
 send an email notification.
 
 So for example, if today is 04-Feb-2011 1000 0800 and there is no backup
 file created on 03-Feb-2011, when the script runs, it should detect that the
 most recent backup file is file.33, computed that it is older than 2 days
 and then send en email notification.

The file test operator -M, when applied to a file, will return the age of
the file in days. Thus, you can use a test such as if( -M $filename  2 ) to
determine if a file is more than 2 days old.

See:

perldoc -f -X




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Testing File Contents

2011-03-02 Thread Matt
I am looking for a simple way to test if a file does not contain a
string.  This is on a linux box.

if myfile does not contain mystring {
  #do_something;
  }

The file is basically a list of names and I want to test that a
certain name is not in there.  Is there an easy way to do that?

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How to list all Linux machines info with perl ?

2011-03-02 Thread Raj Shekhar
In infinite wisdom sync jian...@gmail.com wrote:


 I have many linux machines connected in a network.
 Now I want ot create a perl script which will list all the machines info  in
 a network.

Check the collect tool, written by Percona guys
http://aspersa.googlecode.com/svn/html/summary.html

-- 
Raj Shekhar
-
If there's anything more important than my ego around, I want it
caught and shot now.
- 
Read the latest at my blog: Caching is not a silver bullet 
http://rajshekhar.net/blog/archives/397-Caching-is-not-a-silver-bullet.html



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Parag Kalra
# Untested
use strict;
use warnings;

open my $fh, '', my_file;
while($fh){
if ($_ !~ /my_string/) {
# Do something
}
}

The other way would be on shell -

# Untested
grep my_string my_file
if [ $? -eq 1 ]
then
echo Do something
fi
~Parag



On Wed, Mar 2, 2011 at 9:55 AM, Matt lm7...@gmail.com wrote:

 I am looking for a simple way to test if a file does not contain a
 string.  This is on a linux box.

 if myfile does not contain mystring {
  #do_something;
  }

 The file is basically a list of names and I want to test that a
 certain name is not in there.  Is there an easy way to do that?

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/





Re: Testing File Contents

2011-03-02 Thread Parag Kalra
On Wed, Mar 2, 2011 at 10:11 AM, Parag Kalra paragka...@gmail.com wrote:

Sorry for the top post. I should have done bottom post. :(

# Untested
 use strict;
 use warnings;

 open my $fh, '', my_file;
 while($fh){
 if ($_ !~ /my_string/) {
 # Do something
 }
 }

 The other way would be on shell -

 # Untested
 grep my_string my_file
 if [ $? -eq 1 ]
 then
 echo Do something
 fi
 ~Parag





 On Wed, Mar 2, 2011 at 9:55 AM, Matt lm7...@gmail.com wrote:

 I am looking for a simple way to test if a file does not contain a
 string.  This is on a linux box.

 if myfile does not contain mystring {
  #do_something;
  }

 The file is basically a list of names and I want to test that a
 certain name is not in there.  Is there an easy way to do that?

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/






AW: Testing File Contents

2011-03-02 Thread Christian Marquardt
The easiest way in my opinion is to use the 'grep' function like this:

my $searchstring=whatever;
open CFG, '', $_file || die(could not open file: $_file!);
my @data=CFG;
close CFG;
if ( grep /$searchstring/i, @data ) {
  print $searchstring found\n;
}

If you negate the grep like this:

@data = grep !/$searchstring/i, @data;

... you can remove the searchstring from your array (file-text).


best regards
Christian



Von: Matt [lm7...@gmail.com]
Gesendet: Mittwoch, 2. März 2011 18:55
Bis: beginners@perl.org
Betreff: Testing File Contents

I am looking for a simple way to test if a file does not contain a
string.  This is on a linux box.

if myfile does not contain mystring {
  #do_something;
  }

The file is basically a list of names and I want to test that a
certain name is not in there.  Is there an easy way to do that?

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Matt
 # Untested
 use strict;
 use warnings;
 open my $fh, '', my_file;
 while($fh){
     if ($_ !~ /my_string/) {
         # Do something
     }
 }

This triggers on EVERY line of the file that does not contain the
string.  I just want to trigger once if its no where in the file.


 The other way would be on shell -
 # Untested
 grep my_string my_file
 if [ $? -eq 1 ]
 then
     echo Do something
 fi
 ~Parag



 On Wed, Mar 2, 2011 at 9:55 AM, Matt lm7...@gmail.com wrote:

 I am looking for a simple way to test if a file does not contain a
 string.  This is on a linux box.

 if myfile does not contain mystring {
  #do_something;
  }

 The file is basically a list of names and I want to test that a
 certain name is not in there.  Is there an easy way to do that?

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Matt
 The easiest way in my opinion is to use the 'grep' function like this:

 my $searchstring=whatever;
 open CFG, '', $_file || die(could not open file: $_file!);
 my @data=CFG;
 close CFG;
 if ( grep /$searchstring/i, @data ) {
  print $searchstring found\n;
 }


This sorta worked.  Needed a minor change.

 unless ( grep /$searchstring/i, @data ) {
  print $searchstring not found\n;

Thanks.

 If you negate the grep like this:

 @data = grep !/$searchstring/i, @data;

 ... you can remove the searchstring from your array (file-text).



 I am looking for a simple way to test if a file does not contain a
 string.  This is on a linux box.

 if myfile does not contain mystring {
  #do_something;
  }

 The file is basically a list of names and I want to test that a
 certain name is not in there.  Is there an easy way to do that?

 --

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: strawberry file end

2011-03-02 Thread Brandon McCaig
On Wed, Mar 2, 2011 at 12:02 PM, Christian Henkel
p...@henkelchristian.de wrote:
 In my tutorial they told my to read strings from keyboard (@array = STDIN)
 and told my to press ctrl+Z for the file end. But pressing ctrl+Z doesn't do
 anything. Can someone tell my what I have to do to simulate the EOF? Ctrl+D
 works for ending the file, but the hole script crashes, too.

Ctrl+Z works for me in Windows (XP) from the Command Prompt
application. Initially pressing Ctrl+Z only adds the character to the
line (appearing as ^Z). If I then press enter then it is sent and
works. Perhaps the confusion is that the character isn't immediately
sent as it is in UNIX-like shells?


-- 
Brandon McCaig http://www.bamccaig.com/ bamcc...@gmail.com
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software http://www.castopulence.org/ bamcc...@castopulence.org

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Brian F. Yulga

On Wed, 2 Mar 2011, Matt wrote:

  The easiest way in my opinion is to use the 'grep' function like this:
 
  my $searchstring=whatever;
  open CFG, '', $_file || die(could not open file: $_file!);
  my @data=CFG;
  close CFG;
  if ( grep /$searchstring/i, @data ) {
   print $searchstring found\n;
  }
 
 
 This sorta worked.  Needed a minor change.
 
  unless ( grep /$searchstring/i, @data ) {
   print $searchstring not found\n;
 
 Thanks.
 

My apologies if I'm beating a dead horse here, but I'm new to Perl and 
thought of a slightly different approach:


my $searchrx = qr/whatever/;  # or q/whatever/ if you don't need regexp
@ARGV or die qq/you didn't specify a filename\n/;
open FH, q//, shift @ARGV or die qq/file open error: $!/;
$_ = join q//, FH;
close FH;
if ( ! m/$searchrx/s ) {
print qq/pattern not found\n/;
# do something
}


I'm not sure which is more Perlish, but I hear TMTOWTDI is supposed to 
be valued, too.  I welcome any criticism.

Brian
-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


RE: Testing File Contents

2011-03-02 Thread Ken Slater
From: Brian F. Yulga [mailto:byu...@langly.dyndns.org] 

On Wed, 2 Mar 2011, Matt wrote:

  The easiest way in my opinion is to use the 'grep' function like this:
 
  my $searchstring=whatever;
  open CFG, '', $_file || die(could not open file: $_file!); my 
  @data=CFG; close CFG; if ( grep /$searchstring/i, @data ) {
   print $searchstring found\n;
  }
 
 
 This sorta worked.  Needed a minor change.
 
  unless ( grep /$searchstring/i, @data ) {  print $searchstring not 
  found\n;
 
 Thanks.
 

My apologies if I'm beating a dead horse here, but I'm new to Perl and thought 
of a slightly different approach:


my $searchrx = qr/whatever/;  # or q/whatever/ if you don't need regexp

This is probably personal preference, but I prefer to provide a more
meaningful name rather than using @ARGV. Plus you have lost the filename
once you 'shifted' @ARGV in the open statement. May want to use the file name
in the open error statement or later.
Also, I would not use the '/' as your quote delimiter - that's too recognizable 
as
being used as the pattern match delimiter. Use actual double quotes () unless 
there are
double quotes in the string - also less typing. Instead of '/' may want to use 
'{' and '}'
if using qq (see perldoc perlop, Quote and Quote-like Operators).

my $fileName = shift @ARGV or die You did not specify a filename\n;
@ARGV or die qq/you didn't specify a filename\n/;

Use lexical variable for filehandle. Makes things easier - such as passing to a 
function.

open my $FH,'', $fileName or die Error opening $fileName: $!\n;
open FH, q//, shift @ARGV or die qq/file open error: $!/;

The join is unnecessary, set the input_record_separator ($/) to undef or use a 
local
copy of $/ (see perldoc perlvar). If this is undefined, the entire file will be 
read into
a variable.

$_ = join q//, FH;
close FH;
if ( ! m/$searchrx/s ) {
   print qq/pattern not found\n/;
   # do something
}


I'm not sure which is more Perlish, but I hear TMTOWTDI is supposed to 
be valued, too.  I welcome any criticism.

Brian

HTH, Ken





--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Uri Guttman
 BFY == Brian F Yulga byu...@langly.dyndns.org writes:

  BFY My apologies if I'm beating a dead horse here, but I'm new to Perl and 
  BFY thought of a slightly different approach:


  BFY my $searchrx = qr/whatever/;  # or q/whatever/ if you don't need regexp
  BFY @ARGV or die qq/you didn't specify a filename\n/;
  BFY open FH, q//, shift @ARGV or die qq/file open error: $!/;
  BFY $_ = join q//, FH;

that is very slow and clunky. perl could slurp the file for you if you
set $/ to undef. or better yet, use File::Slurp to do it

  BFY close FH;
  BFY if ( ! m/$searchrx/s ) {

why are you using m//? the m isn't needed if you use // for delimiters

the OP wants to know if a file has something or not, not to print each
line.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Uri Guttman
 M == Matt  lm7...@gmail.com writes:

  M I am looking for a simple way to test if a file does not contain a
  M string.  This is on a linux box.

  M if myfile does not contain mystring {
  M   #do_something;
  M   }

  M The file is basically a list of names and I want to test that a
  M certain name is not in there.  Is there an easy way to do that?

2 lines will do it:

use File::Slurp ;

unless( read_file( $file ) =~ /$whatever/ ) {

# do something
}

faster and cleaner than the line by line methods others have posted.

and if you want even more speed, and the file is long, then shell out to
the grep utility and it has an option to only show you if a line
is/isn't in file. i normally don't recommend shelling out but that would
likely be the fastest solution for a large file.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread shawn wilson
On Wed, Mar 2, 2011 at 3:37 PM, Uri Guttman u...@stemsystems.com wrote:

  M == Matt  lm7...@gmail.com writes:

 2 lines will do it:

 use File::Slurp ;

unless( read_file( $file ) =~ /$whatever/ ) {

# do something
}


 what's better about File::Slurp than just doing:

my( $file, $string ) = @argv;
open my $fh, '', $file;

while( $fh ) {
 print found if /$string/ ;
}

???


How does the function loading works in Perl

2011-03-02 Thread Parag Kalra
Hi,

I have this basic question from a long time now, so thought of asking.

A Perl script may have many functions. When we execute the script via Perl
Interpreter, does all the functions are loaded into memory?

Sometimes we just like to keep the coded functions in the script with the
idea that we may need them in future. Will removing an un-called functions
improve the execution time?

~Parag


Re: How does the function loading works in Perl

2011-03-02 Thread terry peng
2011/3/3 Parag Kalra paragka...@gmail.com:
 Hi,

 I have this basic question from a long time now, so thought of asking.

 A Perl script may have many functions. When we execute the script via Perl
 Interpreter, does all the functions are loaded into memory?


Though there is a stuff called AUTOLOAD, but I think all the
functions have been loaded into the memory once the script is run.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Uri Guttman
 sw == shawn wilson ag4ve...@gmail.com writes:

  sw On Wed, Mar 2, 2011 at 3:37 PM, Uri Guttman u...@stemsystems.com wrote:
M == Matt  lm7...@gmail.com writes:
   
   2 lines will do it:
   
   use File::Slurp ;
   
   unless( read_file( $file ) =~ /$whatever/ ) {
   
   # do something
   }
   
   
   what's better about File::Slurp than just doing:

  sw my( $file, $string ) = @argv;
  sw open my $fh, '', $file;

  sw while( $fh ) {
  sw  print found if /$string/ ;
  sw }

less code, much much faster. you loop over each line. my code does one
regex call and stays inside perl for that. inside perl is usually faster
than running perl op. use the benchmark module and look at the
difference. it will be noticeable.

also your code looks at every line whereas my will match the first time
and then quit. that is another optimization. you could do the same if
you exited the loop upon a match.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How does the function loading works in Perl

2011-03-02 Thread Shawn H Corey

On 11-03-02 04:15 PM, Parag Kalra wrote:

A Perl script may have many functions. When we execute the script via Perl
Interpreter, does all the functions are loaded into memory?



All the functions are compiled and loaded.


Sometimes we just like to keep the coded functions in the script with the
idea that we may need them in future. Will removing an un-called functions
improve the execution time?


Yes but on modern machines, you will need an awful lot of subs to get a 
noticeable slow down.


The AUTOLOAD feature can be used to delay the compilation and loading of 
a sub but I have never seen it used in practice.  Most modern machines 
are fast enough so compiling unused subs is not a problem.



--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early  often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread shawn wilson
On Mar 2, 2011 4:47 PM, Uri Guttman u...@stemsystems.com wrote:

  sw == shawn wilson ag4ve...@gmail.com writes:

  sw On Wed, Mar 2, 2011 at 3:37 PM, Uri Guttman u...@stemsystems.com
wrote:
M == Matt  lm7...@gmail.com writes:
  
   2 lines will do it:
  
   use File::Slurp ;
  
   unless( read_file( $file ) =~ /$whatever/ ) {
  
   # do something
   }
  
  
   what's better about File::Slurp than just doing:

  sw my( $file, $string ) = @argv;
  sw open my $fh, '', $file;

  sw while( $fh ) {
  sw  print found if /$string/ ;
  sw }

 less code, much much faster. you loop over each line. my code does one
 regex call and stays inside perl for that. inside perl is usually faster
 than running perl op. use the benchmark module and look at the
 difference. it will be noticeable.


How can I tell or do I figure out if code 'stays inside perl' or not? And,
I'm not exactly sure what you mean by that either?


Beginner of Beginner getting an error

2011-03-02 Thread TaP
Hi Guys,

I have this script:

#!/usr/bin/perl
# track, find email addresses in emails,
# writes them to /var/opt/ITrootmail/bad/ with a date
# set up as a pipe for procmail or use with Pine
 $now = `date`;

# find included email-like addresses
 while ($line = STDIN) {
if ($line =~ /\b([\w_\-\.]+ at [\w_\-\.]+)\b/) {
# exclude email addresses not needed
(next) if ( $1 =~ /\d{8,}/ );
(next) if ( $1 =~ /postmaster/i );
(next) if ( $1 =~ /DAEMON/i );
(next) if ( $1 =~ /nobody/i );
(next) if ( $1 =~ /localhost/i );
push( at names,$1);
}
}
 close (STDIN);
 foreach $idx (0 .. $#names) {
$uniq{$names[$idx]}=1;
}
 undef  at names;
 foreach $item (keys %uniq) {
push ( at names,$item);
}
 foreach $address (0 .. $#names) {
open (RECORD, /var/opt/ITrootmail/bad/
$names[$address]);
print (RECORD $now);
close RECORD;
}

I'm getting these two errors:

Type of arg 1 to push must be array (not subroutine entry) at /etc/opt/
ITrootmail/track line 16, near $1)
Type of arg 1 to push must be array (not subroutine entry) at /etc/opt/
ITrootmail/track line 25, near $item)

Any idea?

Thanks


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Testing File Contents

2011-03-02 Thread Wagner, David --- Senior Programmer Analyst --- CFS
-Original Message-
From: Matt [mailto:lm7...@gmail.com]
Sent: Wednesday, March 02, 2011 11:25
To: beginners@perl.org
Subject: Re: Testing File Contents

 # Untested
 use strict;
 use warnings;
 open my $fh, '', my_file;
 while($fh){
     if ($_ !~ /my_string/) {
         # Do something
     }
 }

This triggers on EVERY line of the file that does not contain the
string.  I just want to trigger once if its no where in the file.

Add a simple switch which is set to zero. Then look for the value you 
want. When found then you should be able to set to nonzero and leave the while.
Then you should be able to check:  if switch is not true, then send the 
email...

 If you have any questions and/or problems, please let me know. 
 Thanks. 
 
Wags ;) 
David R. Wagner 
Senior Programmer Analyst 
FedEx Services 
1.719.484.2097 Tel 
1.719.484.2419 Fax 
1.408.623.5963 Cell
http://Fedex.com/us





 The other way would be on shell -
 # Untested
 grep my_string my_file
 if [ $? -eq 1 ]
 then
     echo Do something
 fi
 ~Parag



 On Wed, Mar 2, 2011 at 9:55 AM, Matt lm7...@gmail.com wrote:

 I am looking for a simple way to test if a file does not contain a
 string.  This is on a linux box.

 if myfile does not contain mystring {
  #do_something;
  }

 The file is basically a list of names and I want to test that a
 certain name is not in there.  Is there an easy way to do that?

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Uri Guttman
 sw == shawn wilson ag4ve...@gmail.com writes:

   less code, much much faster. you loop over each line. my code does one
   regex call and stays inside perl for that. inside perl is usually faster
   than running perl op. use the benchmark module and look at the
   difference. it will be noticeable.

  sw How can I tell or do I figure out if code 'stays inside perl' or not? And,
  sw I'm not exactly sure what you mean by that either?

a regex will go inside perl's internals and run there. a perl loop as
you wrote will execute perl operations. the perl interpreter is slow
when executing operations (all perl ops, loop, syntax, etc). but once
you get inside an operation it runs fast because the code is in c.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Beginner of Beginner getting an error

2011-03-02 Thread Uri Guttman
 T == TaP  4eversp...@gmail.com writes:

  T Hi Guys,
  T I have this script:

  T #!/usr/bin/perl
  T # track, find email addresses in emails,
  T # writes them to /var/opt/ITrootmail/bad/ with a date
  T # set up as a pipe for procmail or use with Pine
  T  $now = `date`;

why are you executing a shell command when perl can do that for you with
localtime?

and why is everything indented when you don't have any subs? code should
be at the left if possible and then you indent from there.

  T # find included email-like addresses
  T  while ($line = STDIN) {
  T if ($line =~ /\b([\w_\-\.]+ at [\w_\-\.]+)\b/) {
  T # exclude email addresses not needed
  T (next) if ( $1 =~ /\d{8,}/ );
  T (next) if ( $1 =~ /postmaster/i );
  T (next) if ( $1 =~ /DAEMON/i );
  T (next) if ( $1 =~ /nobody/i );
  T (next) if ( $1 =~ /localhost/i );

why are those next's in parens? no need for it. nor are the parens
needed around the conditionals.


  T push( at names,$1);

what is 'at'?? did you means @names? i know it should be @names so where
did this code come from with that strange spam hiding @ to at thing?

  T }
  T }
  T  close (STDIN);

why close STDIN? no need.

  T  foreach $idx (0 .. $#names) {
  T $uniq{$names[$idx]}=1;
  T }

gack, that is bad.

@uniq{ @names } = (1) x @names ;

  T  undef  at names;

again the 'at' names. someone ran this through a stupid address hider

  T  foreach $item (keys %uniq) {
  T push ( at names,$item);
  T }

and again.

  T  foreach $address (0 .. $#names) {
  T open (RECORD, /var/opt/ITrootmail/bad/
  T $names[$address]);
  T print (RECORD $now);
  T close RECORD;
  T }

  T I'm getting these two errors:

  T Type of arg 1 to push must be array (not subroutine entry) at /etc/opt/
  T ITrootmail/track line 16, near $1)
  T Type of arg 1 to push must be array (not subroutine entry) at /etc/opt/
  T ITrootmail/track line 25, near $item)

  T Any idea?

plenty. you didn't write this code. you should learn perl and not use
code from places if you don't understand it. otherwise you will always
be asking for help and can't do the work yourself.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Rob Dixon

On 02/03/2011 17:55, Matt wrote:


I am looking for a simple way to test if a file does not contain a
string.  This is on a linux box.

if myfile does not contain mystring {
   #do_something;
   }

The file is basically a list of names and I want to test that a
certain name is not in there.  Is there an easy way to do that?


Hey Matt

If your file is small then this subroutine will do what you need.

  sub file_contains {
my $file, $string = @_;
open $file, '', $file or die $!;
my %names = map { chomp; ($_ = 1) } DATA;
return $names{$string};
  }


HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Uri Guttman
 RD == Rob Dixon rob.di...@gmx.com writes:

  RD On 02/03/2011 17:55, Matt wrote:
   
   I am looking for a simple way to test if a file does not contain a
   string.  This is on a linux box.
   
   if myfile does not contain mystring {
   #do_something;
   }
   
   The file is basically a list of names and I want to test that a
   certain name is not in there.  Is there an easy way to do that?

  RD Hey Matt

  RD If your file is small then this subroutine will do what you need.

  RD   sub file_contains {
  RD my $file, $string = @_;

you forgot the () around the my vars.
that will assign the count of @_ to $file. not what you want.

perl -le '@foo = qw( a b ) ;my $x, $y = @foo ; print $x $y'
 2


  RD open $file, '', $file or die $!;
  RD my %names = map { chomp; ($_ = 1) } DATA;

don't you mean $file there?

  RD return $names{$string};
  RD   }

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Rob Dixon

On 02/03/2011 23:56, Uri Guttman wrote:

RD == Rob Dixonrob.di...@gmx.com  writes:


   RD  On 02/03/2011 17:55, Matt wrote:
   
 I am looking for a simple way to test if a file does not contain a
 string.  This is on a linux box.
   
 if myfile does not contain mystring {
 #do_something;
 }
   
 The file is basically a list of names and I want to test that a
 certain name is not in there.  Is there an easy way to do that?

   RD  Hey Matt

   RD  If your file is small then this subroutine will do what you need.

   RDsub file_contains {
   RD  my $file, $string = @_;

you forgot the () around the my vars.
that will assign the count of @_ to $file. not what you want.

perl -le '@foo = qw( a b ) ;my $x, $y = @foo ; print $x $y'
  2


   RD  open $file, '', $file or die $!;
   RD  my %names = map { chomp; ($_ =  1) }DATA;

don't you mean$file  there?

   RD  return $names{$string};
   RD}


Thanks Uri. It's midnight and I should be in bed :-/

Version 2:

  sub file_contains {
my ($file, $string) = @_;
open my $fh, '', $file or die $!;
my %names = map { chomp; ($_ = 1) } $fh;
return $names{$string};
  }

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Uri Guttman
 RD == Rob Dixon rob.di...@gmx.com writes:

  RD Thanks Uri. It's midnight and I should be in bed :-/

we should all be in bed!

  RD Version 2:

  RD   sub file_contains {
  RD my ($file, $string) = @_;
  RD open my $fh, '', $file or die $!;
  RD my %names = map { chomp; ($_ = 1) } $fh;
  RD return $names{$string};
  RD   }

did you see my fast slurp version in this thread?

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Beginner of Beginner getting an error

2011-03-02 Thread Jim Gibson
On 3/2/11 Wed  Mar 2, 2011  3:33 PM, Uri Guttman u...@stemsystems.com
scribbled:

 T == TaP  4eversp...@gmail.com writes:
 
   T Hi Guys,
   T I have this script:
 

   T if ($line =~ /\b([\w_\-\.]+ at [\w_\-\.]+)\b/) {

The ' at ' in the above line should also be '@'.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Brian F. Yulga



Ken Slater wrote:

 From: Brian F. Yulga [mailto:byu...@langly.dyndns.org]

 On Wed, 2 Mar 2011, Matt wrote:

 The easiest way in my opinion is to use the 'grep' function like
 this:

 my $searchstring=whatever; open CFG, '', $_file || die(could
 not open file: $_file!); my @data=CFG; close CFG; if ( grep
 /$searchstring/i, @data ) { print $searchstring found\n; }


 This sorta worked.  Needed a minor change.

 unless ( grep /$searchstring/i, @data ) {  print $searchstring
 not found\n;

 Thanks.


 My apologies if I'm beating a dead horse here, but I'm new to Perl
 and thought of a slightly different approach:


 my $searchrx = qr/whatever/;  # or q/whatever/ if you don't need
 regexp

 This is probably personal preference, but I prefer to provide a more
 meaningful name rather than using @ARGV. Plus you have lost the
 filename once you 'shifted' @ARGV in the open statement. May want to
 use the file name in the open error statement or later.


I agree with you, saving the filename to a variable is probably a better 
practice, in general.  In this context it wasn't specified that it was 
needed later, so I opted to just 'shift' it to use once.



 Also, I would not use the '/' as your quote delimiter - that's too
 recognizable as being used as the pattern match delimiter. Use actual
 double quotes () unless there are double quotes in the string - also
 less typing. Instead of '/' may want to use '{' and '}' if using qq
 (see perldoc perlop, Quote and Quote-like Operators).


Good point.  I started using generic quotes almost exclusively when I 
discovered that they avoided some of the possible shell-interpolation 
issues that arise when executing perl one-liners.  I've probably taken 
it too far.  I can see that '/' is not a great idea.  Besides '{' and 
'}' as quote delimiters, do you think it's generally tolerated to use 
'(', ')', '[', ']'  ?




 my $fileName = shift @ARGV or die You did not specify a
 filename\n;
 @ARGV or die qq/you didn't specify a filename\n/;

 Use lexical variable for filehandle. Makes things easier - such as
 passing to a function.

 open my $FH,'', $fileName or die Error opening $fileName: $!\n;
 open FH, q//, shift @ARGV or die qq/file open error: $!/;


Okay, I'll work on breaking that habit.  My first lessons in Perl used 
the open FH convention, and I just got used to it.



 The join is unnecessary, set the input_record_separator ($/) to undef
 or use a local copy of $/ (see perldoc perlvar). If this is
 undefined, the entire file will be read into a variable.



Oh, I totally forgot about the input_record_separator ( $/ )...
That's WAY better (actually when I wrote the 'join', I was thinking that 
I shouldn't need to do it that way!)


Thanks much for the suggestions,

Brian


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Testing File Contents

2011-03-02 Thread Brian F. Yulga

Uri Guttman wrote:

 BFY == Brian F Yulga byu...@langly.dyndns.org writes:

 BFY My apologies if I'm beating a dead horse here, but I'm new to
 Perl and BFY thought of a slightly different approach:


 BFY my $searchrx = qr/whatever/;  # or q/whatever/ if you don't need
 regexp BFY @ARGV or die qq/you didn't specify a filename\n/; BFY
 open FH, q//, shift @ARGV or die qq/file open error: $!/; BFY $_ =
 join q//, FH;

 that is very slow and clunky. perl could slurp the file for you if
 you set $/ to undef. or better yet, use File::Slurp to do it


Yes, definitely better to set $/ to undef.

I'm still trying to get a handle on the libraries at my disposal -- 
There's so much in CPAN it's hard to know where to start.  Is it 
equivalently efficient to use IO::All or IO::Simple, or is File::Slurp 
truly the best for this purpose?


I ask because I played with IO:All to read files, enjoyed the simple, 
intuitive syntax   my $filecontents  io('filename'); , but (perhaps 
naively) assumed the native open my $fh to be faster since it doesn't 
require a use to load a library.




 BFY close FH; BFY if ( ! m/$searchrx/s ) {

 why are you using m//? the m isn't needed if you use // for
 delimiters


Yeah, I know, I don't need it... being verbose (sometimes) keeps a 
newbie like me from making silly mistakes :-)


My initial experimentation with Perl has been mostly in a line-by-line 
mind-set because I have a tendency to write scripts that are used like:

some-unix-command | perl -wne 'do some processing'  myresults.txt

I'm trying to expand my horizons, thanks for help,

Brian


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Beginner of Beginner getting an error

2011-03-02 Thread Uri Guttman
 JG == Jim Gibson jimsgib...@gmail.com writes:

  JG On 3/2/11 Wed  Mar 2, 2011  3:33 PM, Uri Guttman u...@stemsystems.com
  JG scribbled:

   T == TaP  4eversp...@gmail.com writes:
   
  T Hi Guys,
  T I have this script:
   

  T if ($line =~ /\b([\w_\-\.]+ at [\w_\-\.]+)\b/) {

  JG The ' at ' in the above line should also be '@'.

my eyeballs missed that one. very odd hiding harmless @'s from
spammers. never seen perl code like that before.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How does the function loading works in Perl

2011-03-02 Thread Peter Scott
On Wed, 02 Mar 2011 17:02:30 -0500, Shawn H Corey wrote:

 On 11-03-02 04:15 PM, Parag Kalra wrote:
 A Perl script may have many functions. When we execute the script via
 Perl Interpreter, does all the functions are loaded into memory?


 All the functions are compiled and loaded.
 
 Sometimes we just like to keep the coded functions in the script with
 the idea that we may need them in future. Will removing an un-called
 functions improve the execution time?
 
 Yes but on modern machines, you will need an awful lot of subs to get a
 noticeable slow down.

Right.

 The AUTOLOAD feature can be used to delay the compilation and loading of
 a sub 

I think you mean AutoLoader.  Or SelfLoader.  They may use AUTOLOAD under 
the hood but that's not the direction to point the user in.

 but I have never seen it used in practice.  

There are a fair number of modules on CPAN using it.  And in the core: 
POSIX, for instance.

-- 
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274
http://www.oreillyschool.com/courses/perl3/

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/