misterperl has asked for the wisdom of the Perl Monks concerning the following question:
I want to use the same name for a var above and inside braces, but in some cases, I want to make a local copy to use in the braces:
.. but at the "say" line, self is undef when ARGV[0] ne 'Fido'; Which surprised me, since I thought OK- it wont create a new scope of $self in the braces, so the scope above should persist? I also tried ( my $self = new OO( $dog ) ) if $ARGV[0] eq 'Fido'; which I thought had an even better chance to succeed but didn't.# sorta like my $self = new OO( $cat ); { my $self = new OO( $dog ) if $ARGV[0] eq 'Fido'; say $self->species(); }
Seems counter-intuitive that ANY part inside of ( ) would have any influence, if followed by "if 0"?
When I emerge from the braces I want the original self there, and I dont want to use another variable name since I have hundreds of "self" refs in the braces... I also tried "local" instead of "my" but got a lexical error in that case.
TY
Back to
Seekers of Perl Wisdom