in reply to Yet Another Program on Closures ~ Steven Lembark ~ TPRC 2025 - YouTube

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?
  • Comment on Re: Yet Another Program on Closures ~ Steven Lembark ~ TPRC 2025 - YouTube

Replies are listed 'Best First'.
Re^2: Yet Another Program on Closures ~ Steven Lembark ~ TPRC 2025 - YouTube
by hippo (Archbishop) on Jul 30, 2025 at 18:00 UTC
    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

Re^2: Yet Another Program on Closures ~ Steven Lembark ~ TPRC 2025 - YouTube
by LanX (Saint) on Jul 30, 2025 at 16:40 UTC
    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)

Re^2: Yet Another Program on Closures ~ Steven Lembark ~ TPRC 2025 - YouTube
by ikegami (Patriarch) on Jul 31, 2025 at 16:07 UTC

    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]

        yeah :(

      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

        I think one should always give the source when citing a quote.

        I did.

        Your definition seems to say that a function using global variables

        Odd. Did I even even provide a definition?

        I'm certain I didn't come remotely close to opining about whether globals are captured or not.

        And you seem to be referring to package vars, which aren't global. Only punctuation vars and a few others deserve that name (ARGV, STDOUT, ENV, SIG, etc).