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);
> }

Hi Siegfried.

Always use strict and warnings to avoid trivial errors.

Does this do ssomething like what you want? The function returns values in a
sequence to be assigned to a hash by the calling code. It lets you add more
keywords if you need them and the values can be used straight from the hash
instead of assigning them to scalars. You should make sure that a hash element
is present in the hash before going ahead and using it. Each significant line in
the file must have the parameter name and its value separated by an equals sign
with optional whitespace each side. An optional trailing semicolon will be
ignored. So

delay=1

and

       delay    =   1   ;

will both do the same thing.

HTH,

Rob


  use strict;
  use warnings;

  my %params = getParams('delay.txt');
  my $delay = $params{delay};
  my $continue = $params{continue};

  print $delay, "\n";
  print $continue, "\n";

  sub getParams {

    my $file = shift;
    open my $fh, $file or die $!;

    my @values;

    while (<$fh>) {
      next unless /(\S+)\s*=\s*(\S+?)\s*;?/;
      push @values, $1, $2;
    }

    @values;
  }



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to