sub NAME BLOCK # A declaration and a definition.
sub NAME(PROTO) BLOCK # ditto, but with prototypes
- sub NAME(SIG) BLOCK # with a signature instead
sub NAME : ATTRS BLOCK # with attributes
sub NAME(PROTO) : ATTRS BLOCK # with prototypes and attributes
- sub NAME(SIG) : ATTRS BLOCK # with a signature and attributes
+
+ use feature 'signatures';
+ sub NAME(SIG) BLOCK # with signature
+ sub NAME :ATTRS (SIG) BLOCK # with signature, attributes
+ sub NAME :prototype(PROTO) (SIG) BLOCK # with signature, prototype
To define an anonymous subroutine at runtime:
X<subroutine, anonymous>
$subref = sub BLOCK; # no proto
$subref = sub (PROTO) BLOCK; # with proto
- $subref = sub (SIG) BLOCK; # with signature
$subref = sub : ATTRS BLOCK; # with attributes
$subref = sub (PROTO) : ATTRS BLOCK; # with proto and attributes
- $subref = sub (SIG) : ATTRS BLOCK; # with signature and attributes
+
+ use feature 'signatures';
+ $subref = sub (SIG) BLOCK; # with signature
+ $subref = sub : ATTRS(SIG) BLOCK; # with signature, attributes
To import subroutines:
X<import>
disabled.
The signature is part of a subroutine's body. Normally the body of a
-subroutine is simply a braced block of code. When using a signature,
-the signature is a parenthesised list that goes immediately after
-the subroutine name (or, for anonymous subroutines, immediately after
-the C<sub> keyword). The signature declares lexical variables that are
+subroutine is simply a braced block of code, but when using a signature,
+the signature is a parenthesised list that goes immediately before the
+block, after any name or attributes.
+
+For example,
+
+ sub foo :lvalue ($a, $b = 1, @c) { .... }
+
+The signature declares lexical variables that are
in scope for the block. When the subroutine is called, the signature
takes control first. It populates the signature variables from the
list of arguments that were passed. If the argument list doesn't meet
of calls to the subroutine, and the signature puts argument values into
lexical variables at runtime. You can therefore write
- sub foo ($left, $right) : prototype($$) {
+ sub foo :prototype($$) ($left, $right) {
return $left + $right;
}
-The prototype attribute, and any other attributes, come after
-the signature.
+The prototype attribute, and any other attributes, must come before
+the signature. The signature always immediately precedes the block of
+the subroutine's body.
=head2 Private Variables via my()
X<my> X<variable, lexical> X<lexical> X<lexical variable> X<scope, lexical>
The array has 6 elements: 0, 1, 2, undef, undef, 5
The behavior of local() on non-existent members of composite
-types is subject to change in future.
+types is subject to change in future. The behavior of local()
+on array elements specified using negative indexes is particularly
+surprising, and is very likely to change.
=head3 Localized deletion of elements of composite types
X<delete> X<local, composite type element> X<local, array element> X<local, hash element>
C<system> with those arguments. All you'd do is:
sub AUTOLOAD {
- my $program = $AUTOLOAD;
- $program =~ s/.*:://;
- system($program, @_);
+ our $AUTOLOAD; # keep 'use strict' happy
+ my $program = $AUTOLOAD;
+ $program =~ s/.*:://;
+ system($program, @_);
}
date();
- who('am', 'i');
+ who();
ls('-l');
In fact, if you predeclare functions you want to call that way, you don't
use subs qw(date who ls);
date;
- who "am", "i";
+ who;
ls '-l';
A more complete example of this is the Shell module on CPAN, which