On Tuesday 25 July 2006 09:23, siegfried wrote: > I have some cron jobs running perl for many hours. Sometimes I would like > to control things dynamically or even shutdown the job if I notice it is > not running properly (based on the log files). > > Below is what I am doing presently (inside a loop) and I feel there must be > a more elegant solution where I can label my values. Presently my file > "delay.txt" looks like > 1 > 1 > > And I would prefer it look like > > delay= 1; > continue = 1; > > Thanks, > Siegfried > > > sub getDelay{ > local *FILE; > my $delay; > my $continue; > open (FILE,"delay.txt"); > $delay = <FILE>; > $continue = <FILE>; > $delay = $delay + 0; # force integer > close(FILE); > return ($delay, $continue); > }
You may want to try something like this: sub getDelay{ local *FILE; my $delay; my $continue; # added check that file was found and opened open (FILE,"delay.txt") or die "'delay.txt' open: $!\n"; my $contents = do { local $/; <FILE>; }; # 'slurp' the file into $contents # regex over multiple lines for keyword and pick up single digit # defaults to 0 if not found $delay = $contents =~ /delay\s*=\s*(\d)/s ? $1 : 0; $continue = $contents =~ /continue\s*=\s*(\d)/s ? $1 : 0; close(FILE); return ($delay, $continue); } Untested! Beware of typos. -- Aloha => Beau; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>