My Favorite Warnings: closure
In the context of Perl, a closure is a piece of code that captures a specific instance of a lexical variable. A blog entry a month or so ago explores this in greater detail. If you review this blog entry, though, note that it does not cover lexical subroutines, which act more like anonymous subroutines even though they are named.
This blog entry, though, covers the closure
warnings
category.
This category has been around since warnings
were introduced in Perl 5.6. I find four diagnostics in this category as of Perl 5.34.0; two related to lexical variables and two related to lexical subroutines. The "Variable" and "Subroutine" versions of these have similar causes, so I will not distinguish them in my discussion.
All of these diagnostics seem to arise from the fact that named, non-lexical subroutines are created at compile time, whereas lexical and anonymous subroutines are created at run time.
Subroutine "&%s" is not available
Variable "%s" is not available
- This diagnostic can be issued under two conditions:
- A named, non-lexical subroutine refers to an entity (variable or subroutine) in a lexical or anonymous subroutine. This is a problem because the lexical or anonymous subroutine has not been created yet.
- A lexical or anonymous subroutine contains a stringy
eval
referring to a lexical entity. Because Perl does not look inside strings unless and until they areeval
-ed, it can not close over this entity.
Subroutine "%s" will not stay shared
Variable "%s" will not stay shared
- This diagnostic will be issued if a named, non-lexical subroutine refers to an entity (variable or subroutine) that may be created more than once. It is warning you that the named subroutine will only see the first instance of the entity, no matter how many instances are created in the course of execution.
PS, and somewhat off-topic: above, I said "... anonymous subroutines are created at run time." I believe the word "created" appeared in the Perl documentation I consulted for this blog post. A thorough blog entry (either this post or the "Closures" post) would address the subject of what "created" actually means. Unfortunately, I can not think of any way to address this other than reading the Perl code (see joe code in esr's Jargon File), and I have not gotten the courage to do that. I hope it does not mean "compile", but something more like "create an object consisting of the compiled code and all the lexical instances it closes over." If we're lucky, someone with a lot more Perl-fu than I have will clarify this.
Previous entries in this series:
Leave a comment