Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Scope of subroutines with same name

by Joost (Canon)
on Feb 05, 2009 at 23:42 UTC ( [id://741744]=note: print w/replies, xml ) Need Help??


in reply to Scope of subroutines with same name

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.

  • Comment on Re: Scope of subroutines with same name

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://741744]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found