Says ctl:
Actually, all he has to do is:
sub A { local $x = 3; &B; } sub B{ $x } my $x = 2; print &A; #prints 3 not 2
That's not the right example either, because you don't need local at all in that case:

sub A { $x = 3; &B; } sub B{ $x } my $x = 2; print &A;
It still prints 3 and not 2. And nobody who understands what my is about could expect it to print 2.

weren't you a bit harsh to the previous poster as they made a simple mistake about where they put a line?
I was harsh, and I apologize. I get very steamed when people post corrections that include broken code, but that's no excuse for being rude.
it's also good to not give the original poster the impression that local() is a bug which should be gotten rid of
Well, here we disagree. I think that's a fine impression to give them. The fraction of correct uses of local by beginning programmers is vanishingly small or zero.

--
Mark Dominus
Perl Paraphernalia


In reply to Re: I need a simple explantion of the difference between my() and local() by Dominus
in thread I need a simple explantion of the difference between my() and local() by Jemts

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.