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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Not sure how to handle this scope (updated)
by haukex (Archbishop) on Aug 18, 2021 at 18:04 UTC | |
by Anonymous Monk on Aug 18, 2021 at 18:13 UTC | |
by misterperl (Pilgrim) on Aug 18, 2021 at 18:39 UTC | |
by Corion (Patriarch) on Aug 18, 2021 at 18:44 UTC | |
Re: Not sure how to handle this scope
by tybalt89 (Monsignor) on Aug 18, 2021 at 19:12 UTC | |
by Your Mother (Archbishop) on Aug 18, 2021 at 20:42 UTC | |
Re: Not sure how to handle this scope
by BillKSmith (Monsignor) on Aug 19, 2021 at 01:34 UTC | |
Re: Not sure how to handle this scope
by Anonymous Monk on Aug 19, 2021 at 13:52 UTC | |
by The Perlman (Scribe) on Aug 19, 2021 at 21:43 UTC | |
by Anonymous Monk on Aug 21, 2021 at 13:37 UTC |
Back to
Seekers of Perl Wisdom