Skip to content

Commit

Permalink
Adding an example that clarifies how subs are accessed
Browse files Browse the repository at this point in the history
... from outer scopes. This closes #2220
  • Loading branch information
JJ committed Jul 30, 2018
1 parent 7390cfe commit cff167a
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion doc/Type/Sub.pod6
Expand Up @@ -46,7 +46,22 @@ X<|my (Sub)>X<|our (Sub)>
Subs can be nested and scoped with C<my> and C<our>, whereby C<my> is the
default. A sub declared with C<my> can not be reached from any outer scope. An
C<our> scoped sub will not redefine a sub of the same name in the outer scope.
Any sub can be accessed via a closure from any outer scope.
Any sub can be accessed via a closure from any outer scope. For instance, in
this example
sub can-be-seener( $whatever ) {
my sub can-be-seen ( $objection ) {
return $whatever but $objection;
}
return &can-be-seen
}
my $objectioner = can-be-seener( "Really?");
say $objectioner(42).Int; # OUTPUT: «42␤»
C<$objectioner> will contain the C<can-be-seen> subroutine, even if it has been
declared in another scope; calling it with C<42> will return C<"Really?"> with
the number 42 mixed in, as shown in the last sentence.
=head1 Traits
X<|trait_mod (declarator)>
Expand Down

0 comments on commit cff167a

Please sign in to comment.