gam3 has asked for the wisdom of the Perl Monks concerning the following question:
I did not know that the following code was correct:
I had thought that the scope of $t was only the if block and did not extend to the else blocks. Of course it makes perfect sense now. I just thought I would mention it here on the off chance that there is some other perlmonk as ignorant as me.use strict; use warnings; sub bob { if (!(my $t = shift)) { warn "data is false\n"; } elsif ($t eq 'a') { warn "a\n"; } elsif ($t eq '1') { warn "one\n"; } else { warn $t; } } bob(); bob(1); bob('a'); bob('c');
BTW I noticed this because I was running with use warnings, something I don't normally do.
UPDATE: [Thilosophy]
I got a warning because I had:
This gives the warning:if (my $a = $c) { #... } elsif (my $a = $b) { #... }
"my" variable $a masks earlier declaration in same scope at ...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Duh. 'my' scope in if else blocks.
by bobf (Monsignor) on Dec 10, 2007 at 23:17 UTC | |
Re: Duh. 'my' scope in if else blocks.
by doom (Deacon) on Dec 11, 2007 at 01:50 UTC | |
by Thilosophy (Curate) on Dec 11, 2007 at 11:14 UTC | |
by ikegami (Patriarch) on Dec 11, 2007 at 19:57 UTC | |
by doom (Deacon) on Dec 11, 2007 at 19:14 UTC | |
Re: Duh. 'my' scope in if else blocks.
by Thilosophy (Curate) on Dec 11, 2007 at 02:53 UTC | |
Re: Duh. 'my' scope in if else blocks.
by ikegami (Patriarch) on Dec 11, 2007 at 19:20 UTC | |
by TimToady (Parson) on Dec 12, 2007 at 16:48 UTC | |
by tye (Sage) on Dec 12, 2007 at 17:31 UTC | |
by moritz (Cardinal) on Dec 12, 2007 at 17:58 UTC |