Video: Yet Another Program on Closures ~ Steven Lembark ~ TPRC 2025 - YouTube"

Recordings from YAPC::NA::2025 aka The Perl and Raku Conference June 27-29 2025 in Greenville, SC, USA

Posted here one by one for promotion and feedback.

Homepage: "Closing in on an Application: Applying Closures with Perl and Raku"

Abstract:

Over the last few years we’ve had the opportunity to look at what closures are and basics how to create them in Perl & Raku. We even had a chance to look at how Raku’s more advanced signatures can improve closures there. Now it’s time to take a deeper look at how apply them. This talk will quickly review what we’ve see so far on how to define closures, then look at specific examples in testing and data processing where they can simplify frameworks used for CI, data munging, or general data processing

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

  • Comment on Yet Another Program on Closures ~ Steven Lembark ~ TPRC 2025 - YouTube

Replies are listed 'Best First'.
Re: Yet Another Program on Closures ~ Steven Lembark ~ TPRC 2025 - YouTube
by ysth (Canon) on Jul 30, 2025 at 16:10 UTC
    Something I've noticed is that Perl has a very restricted definition of closure - a sub that is bound to external lexical(s). Other languages seem to use it other ways, including to mean any anonymous (which a Perl closure doesn't even have to be) sub. Does Raku's definition match Perl's?
      Other languages seem to use it other ways, including to mean any anonymous ... sub.

      If an anonymous sub doesn't close over an external variable then it isn't a closure. The clue is in the name. :-)


      🦛

        > If an anonymous sub doesn't close over an external variable then it isn't a closure.

        And if a named sub does, it's also a closure.

        Indeed two unrelated concepts.

        UPDATE

        I just checked the etymology, the fact that the bound variables are not accessible from the outside of their defining scope makes the sub "closed".

        { my $state = 42; sub closed_sub { return $state++; } } say closed_sub(); # 42 say closed_sub(); # 43

        Contrary to subs binding global variables, which are "open".

        { our $state = 42; sub open_sub { return $state++; } } say open_sub(); # 42 $main::state = 666; say open_sub(); # 666

        (of course Perl allows you to bind both kinds, so it's not an exclusive attribute here)

        see references from WP:

        • Moses, Joel (June 1970). "The Function of FUNCTION in LISP, or Why the FUNARG Problem Should Be Called the Environment Problem". ACM SIGSAM Bulletin (15): 13–27. doi:10.1145/1093410.1093411. hdl:1721.1/5854. S2CID 17514262. AI Memo 199. A useful metaphor for the difference between FUNCTION and QUOTE in LISP is to think of QUOTE as a porous or an open covering of the function since free variables escape to the current environment. FUNCTION acts as a closed or nonporous covering (hence the term "closure" used by Landin). Thus we talk of "open" Lambda expressions (functions in LISP are usually Lambda expressions) and "closed" Lambda expressions. ... My interest in the environment problem began while Landin, who had a deep understanding of the problem, visited MIT during 1966–67. I then realized the correspondence between the FUNARG lists which are the results of the evaluation of "closed" Lambda expressions in LISP and ISWIM's Lambda Closures.
        • Wikström, Åke (1987). Functional Programming using Standard ML. Prentice Hall. ISBN 0-13-331968-7. The reason it is called a "closure" is that an expression containing free variables is called an "open" expression, and by associating to it the bindings of its free variables, you close it.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

      Dunno about Raku but I already talked here about this BS in PHP. (AFAIK they've corrected the documentation in the meantime.)

      The term stems IIRC from Lisp was popularized in Scheme and means a "hull" structure containing a sub plus bound lexical variables.

      Not sure if other languages also implement this with lex-pads like Perl does. (See e.g. PadWalker)

      PS: I'm happy to see that these videos initiate discussions. :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

      Update

      see also WP:Closure (computer programming)

      That's because Perl is true to the historical and generally accepted definition.

      The term closure is often used as a synonym for anonymous function, though strictly, an anonymous function is a function literal without a name, while a closure is an instance of a function, a value, whose non-local variables have been bound either to values or to storage locations

      The languages to which you allude would be "stretching the definition of an established term". An anonymous function without[edited] the ability to access a captured environment isn't a closure.

        > An anonymous function with the ability to access a captured environment isn't a closure.

        Do you mean without?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        I think one should always give the source when citing a quote.

        In your case you seem to copy a paragraph verbatim from a Wikipedia chapter, which is unfortunate.

        Your definition seems to say that a function using global variables (which are "non local") is a closure. I'd say no! (Hence also the term lexical closure")

        In the end it depends how we identify mechanisms used in Lisp inside Perl for interpretation of the original definitions.

        Like "free variable" and "binding"

        Are global variables bound in Perl?

        I'd say no, they are looked up in the symbol table.

        No reference is stored in the lex pad of a sub.

        But is this an accurate definition of binding?

        (FWIW ... To increase the confusion, PadWalker is is capable to list non-local our variables, since they are lexical aliases to package variables)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery