Steve Bertrand wrote:
> Uri Guttman wrote:
>>>>>>> "SHC" == Shawn H Corey <[email protected]> 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.
This tells me that pre-declaring a var with '' makes my original logic fail:
% perl -e '$var=""; print 2 if $var !~ /1/; print 1 if $var'
...so in this case, I'd have to switch my logic backwards, and change my
original regex check:
if ( $month !~ m{ \A \d{4}-\d{2} \z }xms )
to something like this:
if ( $month && $month !~ m{ \A \d{4}-\d{2} \z }xms )
yes?
*also*... how can I use 'perl' on the command line, but in a way that I
can use multiple lines in my terminal? I see many code snips for Perl
one-liners that cover multiple lines. Is this a copy/paste/cleanup job?
eg:
% perl -e 'print [ENTER pressed]
Unmatched '.
...on FreeBSD.
Steve
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/