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

The following is the code I'm having trouble with. I am reading a text file with 5 lines in it. Each line contains a variable=value with a new line at the end. Some of these files have a variable that equals nothing, in which case I want the sub sendemailspecial to run. If the two contain values, then sub sendemailnormal. Unforturnately the if statement does not appear to be working. Thanx for anyone's help and suggestions.
############## Initialize variables ################# $watcheddir="/samba/accpac/email"; chdir "$watcheddir"; use Mail::Sendmail; unshift @{$Mail::Sendmail::mailcfg{'smtp'}},'mail.aidusa.com'; ##################################################### ####### Translate salesperson number to name ######## %personname=('1001', 'Jerry Clavin', '1009', 'Mike Lay', '1020', 'Vern +on Simpson', '1030', 'Mike Manuel', '1040', 'Ted Zerbey', '1050', 'Mark Akin', '1060', 'Mike Hendricks', +'1500', 'Bill Taylor', '2003', 'House Account (OKC)', '2004', 'Vince Rada', '2007', 'Jerry Hopper', '2009', 'Bryan Montgome +ry', '3000', 'Mike Lay', '3100', 'Jerry Clavin', '5300', 'Mike Lay'); ##################################################### ##### Associate salesperson number with address ##### %personaddress=('1001', 'jclavin@aidusa.com', '1009', 'mlay@aidusa.com +', '1020', 'vsimpson@aidusa.com', '1030', 'mmanuel@aidusa.com', '1040', 'tzerbey@aidusa.com', '1050', 'makin@aidusa.com', '1060', 'mh +endricks@aidusa.com', '1500', 'btaylor@aidusa.com', '2003', 'jhopper@ +aidusa.com', '2004', 'vrada@aidusa.com', '2007', 'jhopper@aidusa.com', '2009', 'bm +ontgomery@aidusa.com', '3000', 'mlay@aidusa.com', '3100', 'jclavin@ai +dusa.com', '5300', 'mlay@aidusa.com'); ##################################################### ################## Monitor loop ##################### loop: opendir (DIR, $watcheddir); @files=grep {!/^\./} readdir (DIR);@files removing files that closedir DIR; foreach $currentfile (@files) { &filecontents; } sleep 10; goto loop; ##################################################### ########## Get Data from Printed File ############### ########## Send Message to Address ############### ########## Delete File After Success ############### sub filecontents { print "Current File: $currentfile\n"; $date=localtime(time); open FILE, $currentfile; $.=0; do { $firstline=<FILE>; ($name, $value)=split(/=/, $firstline); $value =~ s/^\s+//; $value =~ s/\s+$//g; $value =~ s/\n//g; print "$name\=$value\n"; $data{$name}=$value; } while $.<5; if ($data{'SONUMBER'} eq "" || $data{'SALESPERSON'} eq "") { $data{'SONUMBER'}="No S.O. attached"; $data{'SALESPERSON'}="No salesperson assigned"; &sendemailspecial; } else{ &sendmailnormal; } unlink $currentfile; }

Replies are listed 'Best First'.
Re: Funny if else problem (boo)
by boo_radley (Parson) on Jan 12, 2002 at 03:10 UTC
    What you say :
    ...I am reading a text file with 5 lines in it. Each line contains a variable=value with a new line at the end. Some of these files have a variable that equals nothing, in which case I want the sub sendemailspecial to run. If the two contain values, then sub sendemailnormal.
    Unforturnately the if statement does not appear to be working. Thanx for anyone's help and suggestions.


    What this seems to me to mean :
    I want to parse a file for paired data separated by an equals sign. If I come across a pair where the second datum is undefined, run sendmailspecial. What you're doing, listed with line numbers, in two parts

    Part 1 : Parsing the file

    #1 do { #2 $firstline=<FILE>; #3 ($name, $value)=split(/=/, $firstline); #4 $value =~ s/^\s+//; #5 $value =~ s/\s+$//g; #6 $value =~ s/\n//g; #7 print "$name\=$value\n"; #8 $data{$name}=$value; #9 } while $.<5;
    #1 : the do block strikes me as odd. If the file will always contain exactly 5 lines, just use a while. This do block would be appropriate if you needed to skim off a header of some sort, but for the data you described, this is more effort than you need.

    #4- #6 : the space removal regexes are fine. Why did you choose to use a regex to delete newlines rather than using chomp?

    #8 : hash storage. I worry that you could clobber a hash entry, but this probably isn't a concern in your scenario.

    Part 2 : Business Logic

    #1 if ($data{'SONUMBER'} eq "" || $data{'SALESPERSON'} eq "") { #2 $data{'SONUMBER'}="No S.O. attached"; #3 $data{'SALESPERSON'}="No salesperson assigned"; #4 &sendemailspecial; #5 } else{ #6 &sendmailnormal; #7 } #8 #9 unlink $currentfile;
    #1 : this if statement makes a few assumptions which makes me uncomfortable. How do you know that SONUMBER and SALESPERSON are really there?

    I would use something like : if ($hash{key}= "" || !defined $hash{key})
    or
    unless (defined $hash{key} and $hash{key} ne "")
    just to make sure that we do the special emailing if the keys aren't there at all.

    #9 : Please close files before unlinking them.

    To answer your question, your if statement is probably defective. Make sure you check for definedness as suggested.
Re: Funny if else problem
by dereks (Scribe) on Jan 12, 2002 at 04:10 UTC
    You really should check to see if you're opening the file at all. I can't tell by your post if you meant the if statement didn't work at all (as in, it wasn't doing anything), or that it was only executing one part of the if statement.
    open FILE, $currentfile;
    This does nothing if the system can't find the file, and if it can't get the file, obviously the rest of your program won't work.
    open (FILE, $currentfile) or die $!;
    This will let you know if the file can't be opened. The same goes for your opendir before that. If you still can't get the if statement to work properly, then it may be the if statement itself, or one of the many other problems boo_radely pointed out.
Re: Funny if else problem
by Anonymous Monk on Jan 12, 2002 at 16:01 UTC

    try some of these:

    %person = ( # much easier to maintain this way. 1001 => { name => 'Jerry Calvin', email => 'jcalvin@aidusa.com'}, 1002 => { name => 'John Q. Random', email => 'jqrandom@aidusa.com'}, ); @lines = <FILE>; warn "wrong number of lines: $currentfile\n" unless (@lines == 5); chomp @lines; foreach $line (@lines) { ($name,$value) = split( /=/, $line); $value =~ s/^\s+//; $value =~ s/\s+$//; # 'g' isn't needed $data{$name} = $value; } # some more ()'s never hurt, sure this wasn't if (($data{'SONUMBER'} eq '') || ($data{'SALESPERSON'} eq '')) { $data{'SONUMBER'} = 'No S.O. attached'; $data{'SALESPERSON'} = 'No salesperson assigned'; &sendemailspecial; } else { &sendmailnormal; }
Re: Funny if else problem
by Anonymous Monk on Jan 14, 2002 at 23:24 UTC
    First, thank you all for your help. And a thumbs-up to boo_radley. My problem with the if else statement was the undefined portion I didn't include. I started getting into Perl a few weeks ago, so I lack a working knowledge of all of the commands (such as chomp, which I have included this time). But thanks to this website, I think I'm on the right path. ;-)