Uri Guttman wrote:
>>>>>> "SHC" == Shawn H Corey <shawnhco...@gmail.com> writes:
> 
>   SHC> Steve Bertrand wrote:
>   >> my $month = $ARGV[0] if $ARGV[0];
> 
> that is similar to the broken my $foo = 1 if 0 used to make static vars
> inside a sub. the assignment won't even happen unless you have a true value.
> 
>   SHC> Try:
>   SHC> my $month = '';
>   SHC> $month = $ARGV[0] if $ARGV[0];
> 
> or just use ||:
> 
>       my $month = $ARGV[0] || '' ;
> 
> then the assignment always happens and the declaration should be critic
> clean.
> 
> you can even just do:
> 
>       my $month = $ARGV[0] ;
> 
> since you do a boolean test on $month later on. it will handle the same
> logic you have here. but you do a regex on $month and that would trigger
> an undef warning if you didn't pass in an arg. so the above code is better.
> 
> the rule is never use a statement modifier on a declaration. you can
> almost always get what you want with boolean ops.

Thanks Uri,

I was just thinking, but didn't get a chance to test what would happen
if I declared $var = '', and then performed a regex check on it.

...without testing yet, I expect ` '' ` to be different than undef, and
I would expect my regex check to fail if a variable was declared with a
blank string and there is no arg passed in to override the default
declaration.

I've been going through a lot of my code lately, just to find quicker
exit points in subs where code is being executed unnecessarily.

The subs that produced warnings tipped me off that I'm executing too
much code before jumping out much of the time, so its the ones that
don't fire warnings that I've been trying to scrutinize.

...off to see what happens.

Steve

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to