mohadib_the_nasty has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I am still very very new to perl and hope such
a newbie question will go un-punished.
I get emails consisting of 3 lines in the body
that are generated by this command....

(uname -a ;uptime ;date) | mail -s uptime time@taproot.bz

I have made a little code that opens the user "times" mail
spool and grabs the next 3 lines after a blank line, this
is to cutout the header and grab the output of the uname
stuff. The script works...i guess, but complains about
Use of uninitialized value in string at ./mail.pl line 15.

Im guessing that the blank line after the body is producing
this....line 15 is trying to call up three more vars
from the @array that are not there when only one mail is
present.. or dealing with the last mail in spool ..
I think.
#! /usr/bin/perl -w $file = "/var/spool/mail/mohadib"; open(MAIL, "$file") || die "cant open spool\n"; @array = <MAIL>; close (MAIL); foreach $array (@array){ if($array !~ /[A-Za-z0-9]/){ @num = (1 .. 3); foreach $num (@num){ $current = $new + $num; print "$array[\"$current\"]"; } } $new++; }

could someone put my logic on the right path :)
maybe I can test for the words Linux or BSD in the
next line after i have matched a blank line
....and exit if not true?
Hope this is coherent,
jd

Replies are listed 'Best First'.
Re: Newbie Question - arrays
by dws (Chancellor) on Jan 13, 2003 at 03:18 UTC
    The quick fix to your problem is to change
    print "$array[\"$current\"]";
    to
    print "$array[$current]";
    since you really don't want to be escaping those quotes. Or, try
    print $array[$current];
    since there's no need to stringify a value that is already a string.

    The longer fix is to find one of the recipes for scanning mailbox files. The Perl Cookbook is an essential reference to have within reach when you're doing tasks like this.

      thank you
      I changed the script like you said. Looks better,
      but i still get the errors. the mail que has
      one mail in it and here is the out put of the script...

      root ns2 perl_scratch--->./mail.pl
      Linux ns2.taproot.bz 2.4.18-3 #1 Thu Apr 18 07:32:41 EDT 2002 i686 unknown
      8:46pm up 33 min, 4 users, load average: 0.00, 0.00, 0.00
      Sat Jan 11 20:46:21 MST 2003
      Use of uninitialized value in string at ./mail.pl line 15.
      Use of uninitialized value in string at ./mail.pl line 15.
      Use of uninitialized value in string at ./mail.pl line 15.

      as i mentioned before i belive the problem is
      line 15 trying to call vars from @array that dont exist.
      Im just not sure on how to fix it. Or if my logic to start
      with is sound.

      Thanks for the tip,
      jd
Re: Newbie Question - arrays
by pg (Canon) on Jan 13, 2003 at 04:09 UTC
    Here is a piece of code with comments for you:
    $file = "ex1.pl"; open(MAIL, "$file") || die "cant open spool\n"; @array = <MAIL>; close (MAIL); foreach $line_num (0 .. $#array) { chomp($array[$line_num]);#prepare for m// if ($array[$line_num] =~ /^\s*$/) {#a blank line foreach (1 .. 3) { print $array[$line_num + $_];#remember those are not chomp +'d } last;#without this you will see the warning about uninitialize +d values you saw } }
Re: Newbie Question - arrays
by Enlil (Parson) on Jan 13, 2003 at 03:48 UTC
    one could change the line:
    print "$array[\"$current\"]";
    to something like:
    print $array[$current] if $array[$current];
    which will test $array[$current] for "definedness" before it will attempt to print something that does not exist.

    -enlil

      I like this idea, and acutaaly tried it earlier. The thing
      is that the blank lines in my mail spool test true. I dont
      know if there is a unseen crl-left or what. When i use perl
      to search for \n or \n\n it doesent find any. So i dont
      know how to test for the blank line...thats why i do ...

      if($array !~ /A-Za-z0-9/){

      because

      if($array){

      finds truth in the blank lines ??
      any ideas?

      Thaks Again,
      jd
        ...the blank lines in my mail spool test true...

        In order to see if a line is "blank" in the sense that it contains nothing, or contains only whitespace, do this:

        if ( $array =~ /^\s*$/ ) { # true if line is empty or whitespace only .... }
        But looking at the whole problem... It will probably be easier if you can add a little bit to the script that creates the email in the first place, so that it causes the body of the message to begin with a constant, recognizable line that never occurs in the mail header (and is unlikely to be found in other, unrelated email messages that might happen to get sent to this mailbox); this way, you don't have to worry about making sure you can parse a whole mailbox file correctly. In other words, something like this to create the message:
        (echo SYSDATAMSG; uname -a; uptime; date) | mail -s uptime time@taproo +t.bz
        Now, when you process the contents of your mailbox, just look for the string "SYSDATAMSG", read the next three lines for those outputs (and make sure you document your code to specify how the messages are supposed to be created):
        open(MAIL, "$file") || die "cant open spool\n"; my @mail_lines = <MAIL>; close (MAIL); while ( @mail_lines ) { $_ = shift @mail_lines; if ( /^SYSDATAMSG$/ ) { # next three lines are needed $uname_data = shift @mail_lines; $uptime_data = shift @mail_lines; $time_stamp = shift @mail_lines; # (maybe you want to do things with those lines before/besides + printing) print $uname_data, $uptime_data, $time_stamp; } }
        Note that I'm opting to use a "while" loop, and taking stuff off the array until it's empty (so I don't have to count array indexes).

        Blank lines evaluate to true because in your case the blank line string is actually a string containing one character: the newline character.

        When Perl reads in line-at-a-time mode (actually record-at-a-time mode) it reads up to and including the newline character (the default record separator). You can use chomp to remove the record separator:

        @array = map { chomp; $_ } <MAIL>;

        A handy way to check that a line is not blank is to check if it contains a non-whitespace character (newline is a whitespace character):

        if($line =~ /\S/) { # line is not blank

        Or to test for blank:

        if($line !~ /\S/) { # line is blank

        There's no need to anchor the regex with ^ and/or $ since we want it to match (and stop searching) on the first non-whitespace character it finds.

Re: Newbie Question - arrays
by tonkin (Initiate) on Jan 13, 2003 at 07:29 UTC
    Always use strict! Always use strict! Always use strict!
Re: Newbie Question - arrays
by jdporter (Paladin) on Jan 13, 2003 at 20:29 UTC
    You can let perl do a lot more of the parsing work for you.
    #!/usr/bin/perl -w use strict; my $mailspoolfile = "/var/spool/mail/mohadib"; open MAIL, "< $mailspoolfile" or die "Error opening $mailspoolfile: $!\n"; # first, get (and discard) the headers, # which are in the first "paragraph": my $headers = do { local $/ = ""; <MAIL> }; # now read the remaining lines as normal. my( $uname, $uptime, $date ) = <MAIL>; close MAIL; # don't forget to chomp! chomp $uname, $uptime, $date; # now you can print them if you want: print <<EOF; uname: $uname uptime: $uptime date: $date EOF

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.