diff options
author | Father Chrysostomos <sprout@cpan.org> | 2015-01-19 20:33:01 -0800 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2015-01-19 20:34:13 -0800 |
commit | 997340692b82e720b661394ed6fca1989b1abc2d (patch) | |
tree | 7c116e013e9068f6df896444236131546419edc4 | |
parent | 4a873d7a0f3c3698425007a136dda05b0adef997 (diff) | |
download | perl-997340692b82e720b661394ed6fca1989b1abc2d.tar.gz |
Document :const
-rw-r--r-- | ext/attributes/attributes.pm | 7 | ||||
-rw-r--r-- | pod/perlexperiment.pod | 12 | ||||
-rw-r--r-- | pod/perlsub.pod | 15 |
3 files changed, 34 insertions, 0 deletions
diff --git a/ext/attributes/attributes.pm b/ext/attributes/attributes.pm index bac3d7be99..062cd77ce0 100644 --- a/ext/attributes/attributes.pm +++ b/ext/attributes/attributes.pm @@ -260,6 +260,13 @@ attribute will be sanity checked at compile time. The "locked" attribute is deprecated, and has no effect in 5.10.0 and later. It was used as part of the now-removed "Perl 5.005 threads". +=item const + +This experimental attribute, introduced in Perl 5.22, only applies to +anonymous subroutines. It causes the subroutine to be called as soon as +the C<sub> expression is evaluated. The return value is captured and +turned into a constant subroutine. + =back The following are the built-in attributes for variables: diff --git a/pod/perlexperiment.pod b/pod/perlexperiment.pod index ee48e23a4d..be0e0deb93 100644 --- a/pod/perlexperiment.pod +++ b/pod/perlexperiment.pod @@ -123,6 +123,18 @@ L<[perl #122947]|https://rt.perl.org/rt3/Ticket/Display.html?id=122947>. See also: L<perlref/Assigning to References> +=item The "const" attribute + +Introduced in Perl 5.22.0 + +Using this feature triggers warnings in the category +C<experimental::const_attr>. + +The ticket for this feature is +L<[perl #xxxxx]|https://rt.perl.org/rt3/Ticket/Display.html?id=xxxxx>. + +See also: L<perlsub/Constant Functions> + =item The <:win32> IO pseudolayer The ticket for this feature is diff --git a/pod/perlsub.pod b/pod/perlsub.pod index a6d000de1e..ea275838b3 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -1701,6 +1701,21 @@ declared. *ALSO_INLINED = sub () { $x }; } +Perl 5.22 also introduces the experimental "const" attribute as an +alternative. (Disable the "experimental::const_attr" warnings if you want +to use it.) When applied to an anonymous subroutine, it forces the sub to +be called when the C<sub> expression is evaluated. The return value is +captured and turned into a constant subroutine: + + my $x = 54321; + *INLINED = sub : const { $x }; + $x++; + +The return value of C<INLINED> in this example will always be 54321, +regardless of later modifications to $x. You can also put any arbitrary +code inside the sub, at it will be executed immediately and its return +value captured the same way. + If you really want a subroutine with a C<()> prototype that returns a lexical variable you can easily force it to not be inlined by adding an explicit C<return>: |