dave741 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/local/bin/perl use strict; foreach my $name ('A', 'B') { my $res = 'Init' if (0); if (defined ($res)) { print "$name: res = $res\n"; } else { print "$name: res is undef\n" } $res = 'Post'; }
Result:
A: res is undef
B: res = Post
As $res is under lexical variable scope, shouldn't it disappear at the bottom of the block
and be recreated by the second pass, producing an identical result?
Bug? Feature? Saving CPU?
perl -v
This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
Thoughts?
Dave
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Variable Scope (updated)
by haukex (Archbishop) on Nov 10, 2017 at 16:55 UTC | |
Re: Variable Scope
by Eily (Monsignor) on Nov 10, 2017 at 16:55 UTC | |
Re: Variable Scope
by AnomalousMonk (Archbishop) on Nov 10, 2017 at 17:07 UTC |
Back to
Seekers of Perl Wisdom