http://www.perlmonks.org?node_id=1018528


in reply to Re: Real life uses for closures.
in thread Real life uses for closures.

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.

Replies are listed 'Best First'.
Re^3: Real life uses for closures.
by tobyink (Canon) on Feb 13, 2013 at 13:14 UTC

    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 {/? :-)

        I didn't look at the source code at all - I was just referring to the usage examples in the pod.

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