Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Real life uses for closures.

by DrHyde (Prior)
on Feb 12, 2013 at 12:13 UTC ( #1018343=note: print w/replies, xml ) Need Help??

in reply to Real life uses for closures.

Here's some examples from my modules:

Replies are listed 'Best First'.
Re^2: Real life uses for closures.
by DrHyde (Prior) on Feb 13, 2013 at 12:00 UTC

    Oh, and if you want something really closurey, Sort::MultipleFields. There's no clean way of doing what that does without using closures.

    The other obvious approach to that problem is to build some code using eval. In fact, I find that you can almost always replace eval with a closure.

      Technically none of the examples from Sort::MultipleFields employ closures; they just use anonymous functions. Closures "close over" a variable.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

        Here's a closure from it ...

        $sortsub = sub($$) { $spec->($_[0]->{$field}, $_[1]->{$field}) || $oldsortsub->($_[0], $_[1]) }

        Lemme guess - did you just grep the code for /sub {/? :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018343]
[Eily]: bed to Glasgow? Where do you find those?
[marto]: hmm, Jeff Google for "flights from Bedford to Glasgow" shows lots of results :p

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2018-02-23 10:37 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (301 votes). Check out past polls.