Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Is it possible to create a sub exclusive to a sub?

by Aristotle (Chancellor)
on Sep 19, 2004 at 07:38 UTC ( [id://392130]=note: print w/replies, xml ) Need Help??


in reply to Is it possible to create a sub exclusive to a sub?

Update: ihb pointed this out before I did.

All of the other solutions so far create the anonymous sub inside the calling sub, so they recreate it on every call. A better way that avoids that wasted effort would be

BEGIN { my $bar = sub { # ... }; sub foo { # ... $bar->( $baz ); # ... $bar->( $quux ); # ... } }

The BEGIN avoids a trap for the unwary: you might otherwise call foo() before the assignment to $bar has run, blowing up the script. If this code is in a module, there is an implicit BEGIN around all of the code, so this might not be necessary (your call), but it's a good defensive habit nonetheless.

Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found