the subroutine checks $_[0] for both a
range check (in the correct numerical range) and
format check (in the correct string format)

and I need to do this a bunch of times,
in a bunch of different locations,
so I wanted to make it as simple as possible. i.e.:

validate($value);

heres a watered down version of the subroutine.

sub validate
{       
        my $val=$_[0];

        die "bad range" if ($val>100);

        $_[0]=sprintf("%02d", $val);
}

when I call 
validate('02');
it dies, even though its a good value.


I suppose I can make the script smarter
so that it sprintf's to $_[0] only if
the string format is not correct.
then it will only die if the format is
bad and it can't correct it.

sub validate
{       
        my $val=$_[0];

        die "bad range" if ($val>100);

        my $format = sprintf("%02d", $val);

        unless( $format eq $val )
                {
                eval    ( 
                        $_[0]=$format;
                        );
                die "bad format on read only value\n" if $@;
                }
}


then, if I say
validate('02');
it won't complain.

but if I say
validate('2');
it will die on the read-only problem.

not sure if the eval block is the best way
to trap on read-only parameters. but it'll do
for now.

thanks everyone.
Greg

Reply via email to