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:
# sorta like my $self = new OO( $cat ); { my $self = new OO( $dog ) if $ARGV[0] eq 'Fido'; say $self->species(); }
.. 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.

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


In reply to Not sure how to handle this scope by misterperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.