perlclosuressubroutinelexicaltypeglob

What is meant by proper closure here


This is a code lifted straight from Perl Cookbook:

@colors = qw(red blue green yellow orange purple violet);
for my $name (@colors) {
no strict 'refs';
*$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
}

It's intention is to form 6 different subroutines with names of different colors. In the explanation part, the book reads:

These functions all seem independent, but the real code was in fact only compiled once. This technique saves on both compile time and memory use. To create a proper closure, any variables in the anonymous subroutine must be lexicals. That's the reason for the my on the loop iteration variable.

What is meant by proper closure, and what will happen if the my is omitted? Plus how come a typeglob is working with a lexical variable, even though typeglobs cannot be defined for lexical variables and should throw error?


Solution

  • As others have mentioned the cookbook is using the term "proper" to refer to the fact that a subroutine is created that carries with it a variable that is from a higher lexical scope and that this variable can no longer be reached by any other means. I use the over simplified mnemonic "Access to the $color variable is 'closed'" to remember this part of closures.

    The statement "typeglobs cannot be defined for lexical variables" misunderstands a few key points about typeglobs. It is somewhat true it you read it as "you cannot use 'my' to create a typeglob". Consider the following:

    my *red = sub { 'this is red' };
    

    This will die with "syntax error near "my *red" because its trying to define a typeglob using the "my" keyword.

    However, the code from your example is not trying to do this. It is defining a typeglob which is global unless overridden. It is using the value of a lexical variable to define the name of the typeglob.

    Incidentally a typeglob can be lexically local. Consider the following:

    my $color = 'red';
    
    # create sub with the name "main::$color". Specifically "main:red"
    *$color = sub { $color };
    
    # preserve the sub we just created by storing a hard reference to it.
    my $global_sub = \&$color;
    
    {
      # create a lexically local sub with the name "main::$color".
      # this overrides "main::red" until this block ends
      local *$color = sub { "local $color" };
    
      # use our local version via a symbolic reference.
      # perl uses the value of the variable to find a
      # subroutine by name ("red") and executes it
      print &$color(), "\n";
    
      # use the global version in this scope via hard reference.
      # perl executes the value of the variable which is a CODE
      # reference.
      print &$global_sub(), "\n";
    
      # at the end of this block "main::red" goes back to being what
      # it was before we overrode it.
    }
    
    # use the global version by symbolic reference
    print &$color(), "\n";
    

    This is legal and the output will be

    local red
    red
    red
    

    Under warnings this will complain "Subroutine main::red redefined"