Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Closure - Perl Perspective

by perlron (Pilgrim)
on Oct 17, 2014 at 08:49 UTC ( #1104143=perlquestion: print w/replies, xml ) Need Help??

perlron has asked for the wisdom of the Perl Monks concerning the following question:

HI, Im trying to understand closures from a perl perspective. The one on Wiki is more a Scheme based/ Scientific interpretation, going over my head. So is it documented in the man pages ?
Related question - why in the input profile specification of Data::Form::Validator are we passing Closure as input ?

Replies are listed 'Best First'.
Re: Closure - Perl Perspective
by tobyink (Canon) on Oct 17, 2014 at 09:13 UTC

    A closure is a subroutine that makes use of a lexical variable defined outside the subroutine. For example:

    use v5.14; my $foo = 40; sub foo { return(2 + $foo); }

    The foo() sub is said to be a "closure", and the $foo variable has be "closed over".

    When most people talk about closures though, they are usually referring to anonymous functions rather than named ones like above. Something like:

    use v5.14; sub get_counter { my ($count) = @_; my $closure = sub { $count++ }; return $closure; } my $counter = get_counter(40); my $other = get_counter(99); say $counter->(); say $other->(); say $counter->(); say $other->(); say $counter->();

    In fact, people often use the word "closures" to refer to anonymous functions even if the function doesn't close over any variables!

    This is probably due to the fact that in most programming languages (including Perl) there's no syntactic difference between defining an anonymous sub that closes over a variable verses one that does not. They are implemented subtly differently internally though as can be seen by this example:

    use v5.14; use Scalar::Util qw/refaddr/; my @closures; my @nonclosures; for my $i (qw/ a b c /) { push @closures, sub { $i }; push @nonclosures, sub { 42 }; } say join "|" => map refaddr($_), @closures; say join "|" => map refaddr($_), @nonclosures;

    You'll notice that the three non-closure coderefs share a reference address. This is an optimization that Perl does. (I have at least twice had to work around this optimization by defining a dummy variable and closing over it. But then again, I do a lot of weird stuff in Perl. Most people probably never need to care about this difference.)

    Anyway, coming back to Data::Form::Validator - it does accept coderefs as arguments in some places. Whether those coderefs are technically closures (i.e. they close over external variables), it probably doesn't care.

Re: Closure - Perl Perspective
by syphilis (Archbishop) on Oct 17, 2014 at 08:57 UTC
    So is it documented in the man pages ?

    There's a perlfaq entry "What's a closure?" - see perldoc -q closure.
    In addition to the info contained therein, it says that closures are documented in the perlref pod.

      this is perfect. exactly what i wanted. thanks
Re: Closure - Perl Perspective
by choroba (Archbishop) on Oct 17, 2014 at 08:56 UTC
      i should have done that. ill be careful next time :D
Re: Closure - Perl Perspective
by Anonymous Monk on Oct 17, 2014 at 09:18 UTC
      yes im very edgy when i see something i dont understand. i have to explain my code to others too.. thanks !!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1104143]
Approved by Eily
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2022-05-18 07:22 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (68 votes). Check out past polls.