Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Closures

by Aristotle (Chancellor)
on Aug 11, 2002 at 04:53 UTC ( #189269=note: print w/ replies, xml ) Need Help??


in reply to Building an anonymous subroutine

How about:

sub template { my $e = shift; my $not_common_a = $e eq 'b' ? sub { ... } : $e eq 'u' ? sub { ... } : sub { ... } ; my $not_common_b = $e eq 'x' ? sub { ... } : sub { ... } ; return sub { $common_stuff; $not_common_a->(); $more_common_stuff; $not_common_b->(); } }
____________
Makeshifts last the longest.


Comment on Re: Closures
Download Code
Replies are listed 'Best First'.
Re: Building an anonymous function
by BorgCopyeditor (Friar) on Aug 11, 2002 at 06:19 UTC

    Thanks. It looks obvious ... now that I've seen a solution. :) I ended up doing it inside out, as it were, initially defining an anonymous sub just for the part of the enclosing sub that needed differing behaviors, and calling that within that enclosing sub.

    The conditional assignment thingy with nested ternary '?'s is very elegant, I think.

    Thanks again,

    BCE

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2015-07-29 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls