Two things are going on here:

1. named subroutines have package scope. it doesn't matter if you declare them inside braces, or another sub, there will always be just one sub with any given name in a single package. anonymous subs don't have this restriction, since they don't have names.

2. subroutines are normally compiled before execution of anything else.

Combined, these two rules mean that the last subroutine (which is the bottom one in this example) is the one that's active when myfunction_one is called.

In general, it also means declaring nested named subroutines is only going to be confusing. This is one of the areas where perl just isn't as consistent as we'd like it to be, but since it's not clear what the really good solution is, and it's fairly easy to work around the problems it causes, don't expect this to be "solved" in perl 5.


In reply to Re: Scope of subroutines with same name by Joost
in thread Scope of subroutines with same name by atemon

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.