Skip to content

Commit

Permalink
perlsub: update wording around usage of &
Browse files Browse the repository at this point in the history
  • Loading branch information
mauke authored and leonerd committed Apr 27, 2024
1 parent abc5e0f commit 4780f19
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions pod/perlsub.pod
@@ -1,7 +1,7 @@
=head1 NAME
X<subroutine> X<function>

perlsub - Perl subroutines
perlsub - Perl subroutines (user-defined functions)

=head1 SYNOPSIS

Expand Down Expand Up @@ -43,7 +43,7 @@ X<import>
To call subroutines:
X<subroutine, call> X<call>

NAME(LIST); # & is optional with parentheses.
NAME(LIST); # Regular subroutine call.
NAME LIST; # Parentheses optional if predeclared/imported.
&NAME(LIST); # Circumvent prototypes.
&NAME; # Makes current @_ visible to called subroutine.
Expand Down Expand Up @@ -223,10 +223,10 @@ X<recursion>
&foo(1,2,3); # pass three arguments
foo(1,2,3); # the same

foo(); # pass a null list
foo(); # pass an empty argument list
&foo(); # the same

&foo; # foo() get current args, like foo(@_) !!
&foo; # foo() gets current args, like foo(@_)!
use strict 'subs';
foo; # like foo() iff sub foo predeclared, else
# a compile-time error
Expand Down Expand Up @@ -743,7 +743,7 @@ an anonymous sub reference:

my $secret_version = '1.001-beta';
my $secret_sub = sub { print $secret_version };
&$secret_sub();
$secret_sub->();

As long as the reference is never returned by any function within the
module, no outside module can see the subroutine, because its name is not in
Expand Down Expand Up @@ -877,7 +877,7 @@ Synopsis:
local $foo; # make $foo dynamically local
local (@wid, %get); # make list of variables local
local $foo = "flurp"; # make $foo dynamic, and init it
local @oof = @bar; # make @oof dynamic, and init it
local @oof = @bar; # make @oof dynamic, and init it

local $hash{key} = "val"; # sets a local value for this hash entry
delete local $hash{key}; # delete this entry for the current block
Expand Down Expand Up @@ -1488,14 +1488,14 @@ of an attribute.

The
function declaration must be visible at compile time. The prototype
affects only interpretation of new-style calls to the function,
where new-style is defined as not using the C<&> character. In
affects only interpretation of regular calls to the function,
where regular is defined as not using the C<&> sigil. In
other words, if you call it like a built-in function, then it behaves
like a built-in function. If you call it like an old-fashioned
like a built-in function. If you call it like an old-fashioned (perl4)
subroutine, then it behaves like an old-fashioned subroutine. It
naturally falls out from this rule that prototypes have no influence
on subroutine references like C<\&foo> or on indirect subroutine
calls like C<&{$subref}> or C<< $subref->() >>.
calls like C<&{$subref}()> or C<< $subref->() >>.

Method calls are not influenced by prototypes either, because the
function to be called is indeterminate at compile time, since
Expand All @@ -1510,7 +1510,7 @@ corresponding built-in.

sub mylink ($$) mylink $old, $new
sub myvec ($$$) myvec $var, $offset, 1
sub myindex ($$;$) myindex &getstring, "substr"
sub myindex ($$;$) myindex getstring(), "substr"
sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off
sub myreverse (@) myreverse $x, $y, $z
sub myjoin ($@) myjoin ":", $x, $y, $z
Expand Down Expand Up @@ -1545,13 +1545,14 @@ will allow calling myref() as
myref *glob

and the first argument of myref() will be a reference to
a scalar, an array, a hash, a code, or a glob.
a scalar, an array, a hash, a subroutine, or a glob.

Unbackslashed prototype characters have special meanings. Any
unbackslashed C<@> or C<%> eats all remaining arguments, and forces
list context. An argument represented by C<$> forces scalar context. An
C<&> requires an anonymous subroutine, which, if passed as the first
argument, does not require the C<sub> keyword or a subsequent comma.
argument, may look like a bare block: It does not require the C<sub> keyword
or a subsequent comma.

A C<*> allows the subroutine to accept a bareword, constant, scalar expression,
typeglob, or a reference to a typeglob in that slot. The value will be
Expand Down

0 comments on commit 4780f19

Please sign in to comment.