1PERLSUB(1) Perl Programmers Reference Guide PERLSUB(1)
2
3
4
6 perlsub - Perl subroutines
7
9 To declare subroutines:
10
11 sub NAME; # A "forward" declaration.
12 sub NAME(PROTO); # ditto, but with prototypes
13 sub NAME : ATTRS; # with attributes
14 sub NAME(PROTO) : ATTRS; # with attributes and prototypes
15
16 sub NAME BLOCK # A declaration and a definition.
17 sub NAME(PROTO) BLOCK # ditto, but with prototypes
18 sub NAME : ATTRS BLOCK # with attributes
19 sub NAME(PROTO) : ATTRS BLOCK # with prototypes and attributes
20
21 use feature 'signatures';
22 sub NAME(SIG) BLOCK # with signature
23 sub NAME :ATTRS (SIG) BLOCK # with signature, attributes
24 sub NAME :prototype(PROTO) (SIG) BLOCK # with signature, prototype
25
26 To define an anonymous subroutine at runtime:
27
28 $subref = sub BLOCK; # no proto
29 $subref = sub (PROTO) BLOCK; # with proto
30 $subref = sub : ATTRS BLOCK; # with attributes
31 $subref = sub (PROTO) : ATTRS BLOCK; # with proto and attributes
32
33 use feature 'signatures';
34 $subref = sub (SIG) BLOCK; # with signature
35 $subref = sub : ATTRS(SIG) BLOCK; # with signature, attributes
36
37 To import subroutines:
38
39 use MODULE qw(NAME1 NAME2 NAME3);
40
41 To call subroutines:
42
43 NAME(LIST); # & is optional with parentheses.
44 NAME LIST; # Parentheses optional if predeclared/imported.
45 &NAME(LIST); # Circumvent prototypes.
46 &NAME; # Makes current @_ visible to called subroutine.
47
49 Like many languages, Perl provides for user-defined subroutines. These
50 may be located anywhere in the main program, loaded in from other files
51 via the "do", "require", or "use" keywords, or generated on the fly
52 using "eval" or anonymous subroutines. You can even call a function
53 indirectly using a variable containing its name or a CODE reference.
54
55 The Perl model for function call and return values is simple: all
56 functions are passed as parameters one single flat list of scalars, and
57 all functions likewise return to their caller one single flat list of
58 scalars. Any arrays or hashes in these call and return lists will
59 collapse, losing their identities--but you may always use pass-by-
60 reference instead to avoid this. Both call and return lists may
61 contain as many or as few scalar elements as you'd like. (Often a
62 function without an explicit return statement is called a subroutine,
63 but there's really no difference from Perl's perspective.)
64
65 Any arguments passed in show up in the array @_. (They may also show
66 up in lexical variables introduced by a signature; see "Signatures"
67 below.) Therefore, if you called a function with two arguments, those
68 would be stored in $_[0] and $_[1]. The array @_ is a local array, but
69 its elements are aliases for the actual scalar parameters. In
70 particular, if an element $_[0] is updated, the corresponding argument
71 is updated (or an error occurs if it is not updatable). If an argument
72 is an array or hash element which did not exist when the function was
73 called, that element is created only when (and if) it is modified or a
74 reference to it is taken. (Some earlier versions of Perl created the
75 element whether or not the element was assigned to.) Assigning to the
76 whole array @_ removes that aliasing, and does not update any
77 arguments.
78
79 A "return" statement may be used to exit a subroutine, optionally
80 specifying the returned value, which will be evaluated in the
81 appropriate context (list, scalar, or void) depending on the context of
82 the subroutine call. If you specify no return value, the subroutine
83 returns an empty list in list context, the undefined value in scalar
84 context, or nothing in void context. If you return one or more
85 aggregates (arrays and hashes), these will be flattened together into
86 one large indistinguishable list.
87
88 If no "return" is found and if the last statement is an expression, its
89 value is returned. If the last statement is a loop control structure
90 like a "foreach" or a "while", the returned value is unspecified. The
91 empty sub returns the empty list.
92
93 Aside from an experimental facility (see "Signatures" below), Perl does
94 not have named formal parameters. In practice all you do is assign to
95 a "my()" list of these. Variables that aren't declared to be private
96 are global variables. For gory details on creating private variables,
97 see "Private Variables via my()" and "Temporary Values via local()".
98 To create protected environments for a set of functions in a separate
99 package (and probably a separate file), see "Packages" in perlmod.
100
101 Example:
102
103 sub max {
104 my $max = shift(@_);
105 foreach $foo (@_) {
106 $max = $foo if $max < $foo;
107 }
108 return $max;
109 }
110 $bestday = max($mon,$tue,$wed,$thu,$fri);
111
112 Example:
113
114 # get a line, combining continuation lines
115 # that start with whitespace
116
117 sub get_line {
118 $thisline = $lookahead; # global variables!
119 LINE: while (defined($lookahead = <STDIN>)) {
120 if ($lookahead =~ /^[ \t]/) {
121 $thisline .= $lookahead;
122 }
123 else {
124 last LINE;
125 }
126 }
127 return $thisline;
128 }
129
130 $lookahead = <STDIN>; # get first line
131 while (defined($line = get_line())) {
132 ...
133 }
134
135 Assigning to a list of private variables to name your arguments:
136
137 sub maybeset {
138 my($key, $value) = @_;
139 $Foo{$key} = $value unless $Foo{$key};
140 }
141
142 Because the assignment copies the values, this also has the effect of
143 turning call-by-reference into call-by-value. Otherwise a function is
144 free to do in-place modifications of @_ and change its caller's values.
145
146 upcase_in($v1, $v2); # this changes $v1 and $v2
147 sub upcase_in {
148 for (@_) { tr/a-z/A-Z/ }
149 }
150
151 You aren't allowed to modify constants in this way, of course. If an
152 argument were actually literal and you tried to change it, you'd take a
153 (presumably fatal) exception. For example, this won't work:
154
155 upcase_in("frederick");
156
157 It would be much safer if the "upcase_in()" function were written to
158 return a copy of its parameters instead of changing them in place:
159
160 ($v3, $v4) = upcase($v1, $v2); # this doesn't change $v1 and $v2
161 sub upcase {
162 return unless defined wantarray; # void context, do nothing
163 my @parms = @_;
164 for (@parms) { tr/a-z/A-Z/ }
165 return wantarray ? @parms : $parms[0];
166 }
167
168 Notice how this (unprototyped) function doesn't care whether it was
169 passed real scalars or arrays. Perl sees all arguments as one big,
170 long, flat parameter list in @_. This is one area where Perl's simple
171 argument-passing style shines. The "upcase()" function would work
172 perfectly well without changing the "upcase()" definition even if we
173 fed it things like this:
174
175 @newlist = upcase(@list1, @list2);
176 @newlist = upcase( split /:/, $var );
177
178 Do not, however, be tempted to do this:
179
180 (@a, @b) = upcase(@list1, @list2);
181
182 Like the flattened incoming parameter list, the return list is also
183 flattened on return. So all you have managed to do here is stored
184 everything in @a and made @b empty. See "Pass by Reference" for
185 alternatives.
186
187 A subroutine may be called using an explicit "&" prefix. The "&" is
188 optional in modern Perl, as are parentheses if the subroutine has been
189 predeclared. The "&" is not optional when just naming the subroutine,
190 such as when it's used as an argument to defined() or undef(). Nor is
191 it optional when you want to do an indirect subroutine call with a
192 subroutine name or reference using the "&$subref()" or "&{$subref}()"
193 constructs, although the "$subref->()" notation solves that problem.
194 See perlref for more about all that.
195
196 Subroutines may be called recursively. If a subroutine is called using
197 the "&" form, the argument list is optional, and if omitted, no @_
198 array is set up for the subroutine: the @_ array at the time of the
199 call is visible to subroutine instead. This is an efficiency mechanism
200 that new users may wish to avoid.
201
202 &foo(1,2,3); # pass three arguments
203 foo(1,2,3); # the same
204
205 foo(); # pass a null list
206 &foo(); # the same
207
208 &foo; # foo() get current args, like foo(@_) !!
209 foo; # like foo() IFF sub foo predeclared, else "foo"
210
211 Not only does the "&" form make the argument list optional, it also
212 disables any prototype checking on arguments you do provide. This is
213 partly for historical reasons, and partly for having a convenient way
214 to cheat if you know what you're doing. See "Prototypes" below.
215
216 Since Perl 5.16.0, the "__SUB__" token is available under "use feature
217 'current_sub'" and "use 5.16.0". It will evaluate to a reference to
218 the currently-running sub, which allows for recursive calls without
219 knowing your subroutine's name.
220
221 use 5.16.0;
222 my $factorial = sub {
223 my ($x) = @_;
224 return 1 if $x == 1;
225 return($x * __SUB__->( $x - 1 ) );
226 };
227
228 The behavior of "__SUB__" within a regex code block (such as
229 "/(?{...})/") is subject to change.
230
231 Subroutines whose names are in all upper case are reserved to the Perl
232 core, as are modules whose names are in all lower case. A subroutine
233 in all capitals is a loosely-held convention meaning it will be called
234 indirectly by the run-time system itself, usually due to a triggered
235 event. Subroutines whose name start with a left parenthesis are also
236 reserved the same way. The following is a list of some subroutines
237 that currently do special, pre-defined things.
238
239 documented later in this document
240 "AUTOLOAD"
241
242 documented in perlmod
243 "CLONE", "CLONE_SKIP"
244
245 documented in perlobj
246 "DESTROY", "DOES"
247
248 documented in perltie
249 "BINMODE", "CLEAR", "CLOSE", "DELETE", "DESTROY", "EOF", "EXISTS",
250 "EXTEND", "FETCH", "FETCHSIZE", "FILENO", "FIRSTKEY", "GETC",
251 "NEXTKEY", "OPEN", "POP", "PRINT", "PRINTF", "PUSH", "READ",
252 "READLINE", "SCALAR", "SEEK", "SHIFT", "SPLICE", "STORE",
253 "STORESIZE", "TELL", "TIEARRAY", "TIEHANDLE", "TIEHASH",
254 "TIESCALAR", "UNSHIFT", "UNTIE", "WRITE"
255
256 documented in PerlIO::via
257 "BINMODE", "CLEARERR", "CLOSE", "EOF", "ERROR", "FDOPEN", "FILENO",
258 "FILL", "FLUSH", "OPEN", "POPPED", "PUSHED", "READ", "SEEK",
259 "SETLINEBUF", "SYSOPEN", "TELL", "UNREAD", "UTF8", "WRITE"
260
261 documented in perlfunc
262 "import" , "unimport" , "INC"
263
264 documented in UNIVERSAL
265 "VERSION"
266
267 documented in perldebguts
268 "DB::DB", "DB::sub", "DB::lsub", "DB::goto", "DB::postponed"
269
270 undocumented, used internally by the overload feature
271 any starting with "("
272
273 The "BEGIN", "UNITCHECK", "CHECK", "INIT" and "END" subroutines are not
274 so much subroutines as named special code blocks, of which you can have
275 more than one in a package, and which you can not call explicitly. See
276 "BEGIN, UNITCHECK, CHECK, INIT and END" in perlmod
277
278 Signatures
279 WARNING: Subroutine signatures are experimental. The feature may be
280 modified or removed in future versions of Perl.
281
282 Perl has an experimental facility to allow a subroutine's formal
283 parameters to be introduced by special syntax, separate from the
284 procedural code of the subroutine body. The formal parameter list is
285 known as a signature. The facility must be enabled first by a
286 pragmatic declaration, "use feature 'signatures'", and it will produce
287 a warning unless the "experimental::signatures" warnings category is
288 disabled.
289
290 The signature is part of a subroutine's body. Normally the body of a
291 subroutine is simply a braced block of code, but when using a
292 signature, the signature is a parenthesised list that goes immediately
293 before the block, after any name or attributes.
294
295 For example,
296
297 sub foo :lvalue ($a, $b = 1, @c) { .... }
298
299 The signature declares lexical variables that are in scope for the
300 block. When the subroutine is called, the signature takes control
301 first. It populates the signature variables from the list of arguments
302 that were passed. If the argument list doesn't meet the requirements
303 of the signature, then it will throw an exception. When the signature
304 processing is complete, control passes to the block.
305
306 Positional parameters are handled by simply naming scalar variables in
307 the signature. For example,
308
309 sub foo ($left, $right) {
310 return $left + $right;
311 }
312
313 takes two positional parameters, which must be filled at runtime by two
314 arguments. By default the parameters are mandatory, and it is not
315 permitted to pass more arguments than expected. So the above is
316 equivalent to
317
318 sub foo {
319 die "Too many arguments for subroutine" unless @_ <= 2;
320 die "Too few arguments for subroutine" unless @_ >= 2;
321 my $left = $_[0];
322 my $right = $_[1];
323 return $left + $right;
324 }
325
326 An argument can be ignored by omitting the main part of the name from a
327 parameter declaration, leaving just a bare "$" sigil. For example,
328
329 sub foo ($first, $, $third) {
330 return "first=$first, third=$third";
331 }
332
333 Although the ignored argument doesn't go into a variable, it is still
334 mandatory for the caller to pass it.
335
336 A positional parameter is made optional by giving a default value,
337 separated from the parameter name by "=":
338
339 sub foo ($left, $right = 0) {
340 return $left + $right;
341 }
342
343 The above subroutine may be called with either one or two arguments.
344 The default value expression is evaluated when the subroutine is
345 called, so it may provide different default values for different calls.
346 It is only evaluated if the argument was actually omitted from the
347 call. For example,
348
349 my $auto_id = 0;
350 sub foo ($thing, $id = $auto_id++) {
351 print "$thing has ID $id";
352 }
353
354 automatically assigns distinct sequential IDs to things for which no ID
355 was supplied by the caller. A default value expression may also refer
356 to parameters earlier in the signature, making the default for one
357 parameter vary according to the earlier parameters. For example,
358
359 sub foo ($first_name, $surname, $nickname = $first_name) {
360 print "$first_name $surname is known as \"$nickname\"";
361 }
362
363 An optional parameter can be nameless just like a mandatory parameter.
364 For example,
365
366 sub foo ($thing, $ = 1) {
367 print $thing;
368 }
369
370 The parameter's default value will still be evaluated if the
371 corresponding argument isn't supplied, even though the value won't be
372 stored anywhere. This is in case evaluating it has important side
373 effects. However, it will be evaluated in void context, so if it
374 doesn't have side effects and is not trivial it will generate a warning
375 if the "void" warning category is enabled. If a nameless optional
376 parameter's default value is not important, it may be omitted just as
377 the parameter's name was:
378
379 sub foo ($thing, $=) {
380 print $thing;
381 }
382
383 Optional positional parameters must come after all mandatory positional
384 parameters. (If there are no mandatory positional parameters then an
385 optional positional parameters can be the first thing in the
386 signature.) If there are multiple optional positional parameters and
387 not enough arguments are supplied to fill them all, they will be filled
388 from left to right.
389
390 After positional parameters, additional arguments may be captured in a
391 slurpy parameter. The simplest form of this is just an array variable:
392
393 sub foo ($filter, @inputs) {
394 print $filter->($_) foreach @inputs;
395 }
396
397 With a slurpy parameter in the signature, there is no upper limit on
398 how many arguments may be passed. A slurpy array parameter may be
399 nameless just like a positional parameter, in which case its only
400 effect is to turn off the argument limit that would otherwise apply:
401
402 sub foo ($thing, @) {
403 print $thing;
404 }
405
406 A slurpy parameter may instead be a hash, in which case the arguments
407 available to it are interpreted as alternating keys and values. There
408 must be as many keys as values: if there is an odd argument then an
409 exception will be thrown. Keys will be stringified, and if there are
410 duplicates then the later instance takes precedence over the earlier,
411 as with standard hash construction.
412
413 sub foo ($filter, %inputs) {
414 print $filter->($_, $inputs{$_}) foreach sort keys %inputs;
415 }
416
417 A slurpy hash parameter may be nameless just like other kinds of
418 parameter. It still insists that the number of arguments available to
419 it be even, even though they're not being put into a variable.
420
421 sub foo ($thing, %) {
422 print $thing;
423 }
424
425 A slurpy parameter, either array or hash, must be the last thing in the
426 signature. It may follow mandatory and optional positional parameters;
427 it may also be the only thing in the signature. Slurpy parameters
428 cannot have default values: if no arguments are supplied for them then
429 you get an empty array or empty hash.
430
431 A signature may be entirely empty, in which case all it does is check
432 that the caller passed no arguments:
433
434 sub foo () {
435 return 123;
436 }
437
438 When using a signature, the arguments are still available in the
439 special array variable @_, in addition to the lexical variables of the
440 signature. There is a difference between the two ways of accessing the
441 arguments: @_ aliases the arguments, but the signature variables get
442 copies of the arguments. So writing to a signature variable only
443 changes that variable, and has no effect on the caller's variables, but
444 writing to an element of @_ modifies whatever the caller used to supply
445 that argument.
446
447 There is a potential syntactic ambiguity between signatures and
448 prototypes (see "Prototypes"), because both start with an opening
449 parenthesis and both can appear in some of the same places, such as
450 just after the name in a subroutine declaration. For historical
451 reasons, when signatures are not enabled, any opening parenthesis in
452 such a context will trigger very forgiving prototype parsing. Most
453 signatures will be interpreted as prototypes in those circumstances,
454 but won't be valid prototypes. (A valid prototype cannot contain any
455 alphabetic character.) This will lead to somewhat confusing error
456 messages.
457
458 To avoid ambiguity, when signatures are enabled the special syntax for
459 prototypes is disabled. There is no attempt to guess whether a
460 parenthesised group was intended to be a prototype or a signature. To
461 give a subroutine a prototype under these circumstances, use a
462 prototype attribute. For example,
463
464 sub foo :prototype($) { $_[0] }
465
466 It is entirely possible for a subroutine to have both a prototype and a
467 signature. They do different jobs: the prototype affects compilation
468 of calls to the subroutine, and the signature puts argument values into
469 lexical variables at runtime. You can therefore write
470
471 sub foo :prototype($$) ($left, $right) {
472 return $left + $right;
473 }
474
475 The prototype attribute, and any other attributes, must come before the
476 signature. The signature always immediately precedes the block of the
477 subroutine's body.
478
479 Private Variables via my()
480 Synopsis:
481
482 my $foo; # declare $foo lexically local
483 my (@wid, %get); # declare list of variables local
484 my $foo = "flurp"; # declare $foo lexical, and init it
485 my @oof = @bar; # declare @oof lexical, and init it
486 my $x : Foo = $y; # similar, with an attribute applied
487
488 WARNING: The use of attribute lists on "my" declarations is still
489 evolving. The current semantics and interface are subject to change.
490 See attributes and Attribute::Handlers.
491
492 The "my" operator declares the listed variables to be lexically
493 confined to the enclosing block, conditional
494 ("if"/"unless"/"elsif"/"else"), loop
495 ("for"/"foreach"/"while"/"until"/"continue"), subroutine, "eval", or
496 "do"/"require"/"use"'d file. If more than one value is listed, the
497 list must be placed in parentheses. All listed elements must be legal
498 lvalues. Only alphanumeric identifiers may be lexically
499 scoped--magical built-ins like $/ must currently be "local"ized with
500 "local" instead.
501
502 Unlike dynamic variables created by the "local" operator, lexical
503 variables declared with "my" are totally hidden from the outside world,
504 including any called subroutines. This is true if it's the same
505 subroutine called from itself or elsewhere--every call gets its own
506 copy.
507
508 This doesn't mean that a "my" variable declared in a statically
509 enclosing lexical scope would be invisible. Only dynamic scopes are
510 cut off. For example, the "bumpx()" function below has access to the
511 lexical $x variable because both the "my" and the "sub" occurred at the
512 same scope, presumably file scope.
513
514 my $x = 10;
515 sub bumpx { $x++ }
516
517 An "eval()", however, can see lexical variables of the scope it is
518 being evaluated in, so long as the names aren't hidden by declarations
519 within the "eval()" itself. See perlref.
520
521 The parameter list to my() may be assigned to if desired, which allows
522 you to initialize your variables. (If no initializer is given for a
523 particular variable, it is created with the undefined value.) Commonly
524 this is used to name input parameters to a subroutine. Examples:
525
526 $arg = "fred"; # "global" variable
527 $n = cube_root(27);
528 print "$arg thinks the root is $n\n";
529 fred thinks the root is 3
530
531 sub cube_root {
532 my $arg = shift; # name doesn't matter
533 $arg **= 1/3;
534 return $arg;
535 }
536
537 The "my" is simply a modifier on something you might assign to. So
538 when you do assign to variables in its argument list, "my" doesn't
539 change whether those variables are viewed as a scalar or an array. So
540
541 my ($foo) = <STDIN>; # WRONG?
542 my @FOO = <STDIN>;
543
544 both supply a list context to the right-hand side, while
545
546 my $foo = <STDIN>;
547
548 supplies a scalar context. But the following declares only one
549 variable:
550
551 my $foo, $bar = 1; # WRONG
552
553 That has the same effect as
554
555 my $foo;
556 $bar = 1;
557
558 The declared variable is not introduced (is not visible) until after
559 the current statement. Thus,
560
561 my $x = $x;
562
563 can be used to initialize a new $x with the value of the old $x, and
564 the expression
565
566 my $x = 123 and $x == 123
567
568 is false unless the old $x happened to have the value 123.
569
570 Lexical scopes of control structures are not bounded precisely by the
571 braces that delimit their controlled blocks; control expressions are
572 part of that scope, too. Thus in the loop
573
574 while (my $line = <>) {
575 $line = lc $line;
576 } continue {
577 print $line;
578 }
579
580 the scope of $line extends from its declaration throughout the rest of
581 the loop construct (including the "continue" clause), but not beyond
582 it. Similarly, in the conditional
583
584 if ((my $answer = <STDIN>) =~ /^yes$/i) {
585 user_agrees();
586 } elsif ($answer =~ /^no$/i) {
587 user_disagrees();
588 } else {
589 chomp $answer;
590 die "'$answer' is neither 'yes' nor 'no'";
591 }
592
593 the scope of $answer extends from its declaration through the rest of
594 that conditional, including any "elsif" and "else" clauses, but not
595 beyond it. See "Simple Statements" in perlsyn for information on the
596 scope of variables in statements with modifiers.
597
598 The "foreach" loop defaults to scoping its index variable dynamically
599 in the manner of "local". However, if the index variable is prefixed
600 with the keyword "my", or if there is already a lexical by that name in
601 scope, then a new lexical is created instead. Thus in the loop
602
603 for my $i (1, 2, 3) {
604 some_function();
605 }
606
607 the scope of $i extends to the end of the loop, but not beyond it,
608 rendering the value of $i inaccessible within "some_function()".
609
610 Some users may wish to encourage the use of lexically scoped variables.
611 As an aid to catching implicit uses to package variables, which are
612 always global, if you say
613
614 use strict 'vars';
615
616 then any variable mentioned from there to the end of the enclosing
617 block must either refer to a lexical variable, be predeclared via "our"
618 or "use vars", or else must be fully qualified with the package name.
619 A compilation error results otherwise. An inner block may countermand
620 this with "no strict 'vars'".
621
622 A "my" has both a compile-time and a run-time effect. At compile time,
623 the compiler takes notice of it. The principal usefulness of this is
624 to quiet "use strict 'vars'", but it is also essential for generation
625 of closures as detailed in perlref. Actual initialization is delayed
626 until run time, though, so it gets executed at the appropriate time,
627 such as each time through a loop, for example.
628
629 Variables declared with "my" are not part of any package and are
630 therefore never fully qualified with the package name. In particular,
631 you're not allowed to try to make a package variable (or other global)
632 lexical:
633
634 my $pack::var; # ERROR! Illegal syntax
635
636 In fact, a dynamic variable (also known as package or global variables)
637 are still accessible using the fully qualified "::" notation even while
638 a lexical of the same name is also visible:
639
640 package main;
641 local $x = 10;
642 my $x = 20;
643 print "$x and $::x\n";
644
645 That will print out 20 and 10.
646
647 You may declare "my" variables at the outermost scope of a file to hide
648 any such identifiers from the world outside that file. This is similar
649 in spirit to C's static variables when they are used at the file level.
650 To do this with a subroutine requires the use of a closure (an
651 anonymous function that accesses enclosing lexicals). If you want to
652 create a private subroutine that cannot be called from outside that
653 block, it can declare a lexical variable containing an anonymous sub
654 reference:
655
656 my $secret_version = '1.001-beta';
657 my $secret_sub = sub { print $secret_version };
658 &$secret_sub();
659
660 As long as the reference is never returned by any function within the
661 module, no outside module can see the subroutine, because its name is
662 not in any package's symbol table. Remember that it's not REALLY
663 called $some_pack::secret_version or anything; it's just
664 $secret_version, unqualified and unqualifiable.
665
666 This does not work with object methods, however; all object methods
667 have to be in the symbol table of some package to be found. See
668 "Function Templates" in perlref for something of a work-around to this.
669
670 Persistent Private Variables
671 There are two ways to build persistent private variables in Perl 5.10.
672 First, you can simply use the "state" feature. Or, you can use
673 closures, if you want to stay compatible with releases older than 5.10.
674
675 Persistent variables via state()
676
677 Beginning with Perl 5.10.0, you can declare variables with the "state"
678 keyword in place of "my". For that to work, though, you must have
679 enabled that feature beforehand, either by using the "feature" pragma,
680 or by using "-E" on one-liners (see feature). Beginning with Perl
681 5.16, the "CORE::state" form does not require the "feature" pragma.
682
683 The "state" keyword creates a lexical variable (following the same
684 scoping rules as "my") that persists from one subroutine call to the
685 next. If a state variable resides inside an anonymous subroutine, then
686 each copy of the subroutine has its own copy of the state variable.
687 However, the value of the state variable will still persist between
688 calls to the same copy of the anonymous subroutine. (Don't forget that
689 "sub { ... }" creates a new subroutine each time it is executed.)
690
691 For example, the following code maintains a private counter,
692 incremented each time the gimme_another() function is called:
693
694 use feature 'state';
695 sub gimme_another { state $x; return ++$x }
696
697 And this example uses anonymous subroutines to create separate
698 counters:
699
700 use feature 'state';
701 sub create_counter {
702 return sub { state $x; return ++$x }
703 }
704
705 Also, since $x is lexical, it can't be reached or modified by any Perl
706 code outside.
707
708 When combined with variable declaration, simple assignment to "state"
709 variables (as in "state $x = 42") is executed only the first time.
710 When such statements are evaluated subsequent times, the assignment is
711 ignored. The behavior of assignment to "state" declarations where the
712 left hand side of the assignment involves any parentheses is currently
713 undefined.
714
715 Persistent variables with closures
716
717 Just because a lexical variable is lexically (also called statically)
718 scoped to its enclosing block, "eval", or "do" FILE, this doesn't mean
719 that within a function it works like a C static. It normally works
720 more like a C auto, but with implicit garbage collection.
721
722 Unlike local variables in C or C++, Perl's lexical variables don't
723 necessarily get recycled just because their scope has exited. If
724 something more permanent is still aware of the lexical, it will stick
725 around. So long as something else references a lexical, that lexical
726 won't be freed--which is as it should be. You wouldn't want memory
727 being free until you were done using it, or kept around once you were
728 done. Automatic garbage collection takes care of this for you.
729
730 This means that you can pass back or save away references to lexical
731 variables, whereas to return a pointer to a C auto is a grave error.
732 It also gives us a way to simulate C's function statics. Here's a
733 mechanism for giving a function private variables with both lexical
734 scoping and a static lifetime. If you do want to create something like
735 C's static variables, just enclose the whole function in an extra
736 block, and put the static variable outside the function but in the
737 block.
738
739 {
740 my $secret_val = 0;
741 sub gimme_another {
742 return ++$secret_val;
743 }
744 }
745 # $secret_val now becomes unreachable by the outside
746 # world, but retains its value between calls to gimme_another
747
748 If this function is being sourced in from a separate file via "require"
749 or "use", then this is probably just fine. If it's all in the main
750 program, you'll need to arrange for the "my" to be executed early,
751 either by putting the whole block above your main program, or more
752 likely, placing merely a "BEGIN" code block around it to make sure it
753 gets executed before your program starts to run:
754
755 BEGIN {
756 my $secret_val = 0;
757 sub gimme_another {
758 return ++$secret_val;
759 }
760 }
761
762 See "BEGIN, UNITCHECK, CHECK, INIT and END" in perlmod about the
763 special triggered code blocks, "BEGIN", "UNITCHECK", "CHECK", "INIT"
764 and "END".
765
766 If declared at the outermost scope (the file scope), then lexicals work
767 somewhat like C's file statics. They are available to all functions in
768 that same file declared below them, but are inaccessible from outside
769 that file. This strategy is sometimes used in modules to create
770 private variables that the whole module can see.
771
772 Temporary Values via local()
773 WARNING: In general, you should be using "my" instead of "local",
774 because it's faster and safer. Exceptions to this include the global
775 punctuation variables, global filehandles and formats, and direct
776 manipulation of the Perl symbol table itself. "local" is mostly used
777 when the current value of a variable must be visible to called
778 subroutines.
779
780 Synopsis:
781
782 # localization of values
783
784 local $foo; # make $foo dynamically local
785 local (@wid, %get); # make list of variables local
786 local $foo = "flurp"; # make $foo dynamic, and init it
787 local @oof = @bar; # make @oof dynamic, and init it
788
789 local $hash{key} = "val"; # sets a local value for this hash entry
790 delete local $hash{key}; # delete this entry for the current block
791 local ($cond ? $v1 : $v2); # several types of lvalues support
792 # localization
793
794 # localization of symbols
795
796 local *FH; # localize $FH, @FH, %FH, &FH ...
797 local *merlyn = *randal; # now $merlyn is really $randal, plus
798 # @merlyn is really @randal, etc
799 local *merlyn = 'randal'; # SAME THING: promote 'randal' to *randal
800 local *merlyn = \$randal; # just alias $merlyn, not @merlyn etc
801
802 A "local" modifies its listed variables to be "local" to the enclosing
803 block, "eval", or "do FILE"--and to any subroutine called from within
804 that block. A "local" just gives temporary values to global (meaning
805 package) variables. It does not create a local variable. This is
806 known as dynamic scoping. Lexical scoping is done with "my", which
807 works more like C's auto declarations.
808
809 Some types of lvalues can be localized as well: hash and array elements
810 and slices, conditionals (provided that their result is always
811 localizable), and symbolic references. As for simple variables, this
812 creates new, dynamically scoped values.
813
814 If more than one variable or expression is given to "local", they must
815 be placed in parentheses. This operator works by saving the current
816 values of those variables in its argument list on a hidden stack and
817 restoring them upon exiting the block, subroutine, or eval. This means
818 that called subroutines can also reference the local variable, but not
819 the global one. The argument list may be assigned to if desired, which
820 allows you to initialize your local variables. (If no initializer is
821 given for a particular variable, it is created with an undefined
822 value.)
823
824 Because "local" is a run-time operator, it gets executed each time
825 through a loop. Consequently, it's more efficient to localize your
826 variables outside the loop.
827
828 Grammatical note on local()
829
830 A "local" is simply a modifier on an lvalue expression. When you
831 assign to a "local"ized variable, the "local" doesn't change whether
832 its list is viewed as a scalar or an array. So
833
834 local($foo) = <STDIN>;
835 local @FOO = <STDIN>;
836
837 both supply a list context to the right-hand side, while
838
839 local $foo = <STDIN>;
840
841 supplies a scalar context.
842
843 Localization of special variables
844
845 If you localize a special variable, you'll be giving a new value to it,
846 but its magic won't go away. That means that all side-effects related
847 to this magic still work with the localized value.
848
849 This feature allows code like this to work :
850
851 # Read the whole contents of FILE in $slurp
852 { local $/ = undef; $slurp = <FILE>; }
853
854 Note, however, that this restricts localization of some values ; for
855 example, the following statement dies, as of perl 5.10.0, with an error
856 Modification of a read-only value attempted, because the $1 variable is
857 magical and read-only :
858
859 local $1 = 2;
860
861 One exception is the default scalar variable: starting with perl 5.14
862 "local($_)" will always strip all magic from $_, to make it possible to
863 safely reuse $_ in a subroutine.
864
865 WARNING: Localization of tied arrays and hashes does not currently work
866 as described. This will be fixed in a future release of Perl; in the
867 meantime, avoid code that relies on any particular behavior of
868 localising tied arrays or hashes (localising individual elements is
869 still okay). See "Localising Tied Arrays and Hashes Is Broken" in
870 perl58delta for more details.
871
872 Localization of globs
873
874 The construct
875
876 local *name;
877
878 creates a whole new symbol table entry for the glob "name" in the
879 current package. That means that all variables in its glob slot
880 ($name, @name, %name, &name, and the "name" filehandle) are dynamically
881 reset.
882
883 This implies, among other things, that any magic eventually carried by
884 those variables is locally lost. In other words, saying "local */"
885 will not have any effect on the internal value of the input record
886 separator.
887
888 Localization of elements of composite types
889
890 It's also worth taking a moment to explain what happens when you
891 "local"ize a member of a composite type (i.e. an array or hash
892 element). In this case, the element is "local"ized by name. This
893 means that when the scope of the "local()" ends, the saved value will
894 be restored to the hash element whose key was named in the "local()",
895 or the array element whose index was named in the "local()". If that
896 element was deleted while the "local()" was in effect (e.g. by a
897 "delete()" from a hash or a "shift()" of an array), it will spring back
898 into existence, possibly extending an array and filling in the skipped
899 elements with "undef". For instance, if you say
900
901 %hash = ( 'This' => 'is', 'a' => 'test' );
902 @ary = ( 0..5 );
903 {
904 local($ary[5]) = 6;
905 local($hash{'a'}) = 'drill';
906 while (my $e = pop(@ary)) {
907 print "$e . . .\n";
908 last unless $e > 3;
909 }
910 if (@ary) {
911 $hash{'only a'} = 'test';
912 delete $hash{'a'};
913 }
914 }
915 print join(' ', map { "$_ $hash{$_}" } sort keys %hash),".\n";
916 print "The array has ",scalar(@ary)," elements: ",
917 join(', ', map { defined $_ ? $_ : 'undef' } @ary),"\n";
918
919 Perl will print
920
921 6 . . .
922 4 . . .
923 3 . . .
924 This is a test only a test.
925 The array has 6 elements: 0, 1, 2, undef, undef, 5
926
927 The behavior of local() on non-existent members of composite types is
928 subject to change in future. The behavior of local() on array elements
929 specified using negative indexes is particularly surprising, and is
930 very likely to change.
931
932 Localized deletion of elements of composite types
933
934 You can use the "delete local $array[$idx]" and "delete local
935 $hash{key}" constructs to delete a composite type entry for the current
936 block and restore it when it ends. They return the array/hash value
937 before the localization, which means that they are respectively
938 equivalent to
939
940 do {
941 my $val = $array[$idx];
942 local $array[$idx];
943 delete $array[$idx];
944 $val
945 }
946
947 and
948
949 do {
950 my $val = $hash{key};
951 local $hash{key};
952 delete $hash{key};
953 $val
954 }
955
956 except that for those the "local" is scoped to the "do" block. Slices
957 are also accepted.
958
959 my %hash = (
960 a => [ 7, 8, 9 ],
961 b => 1,
962 )
963
964 {
965 my $a = delete local $hash{a};
966 # $a is [ 7, 8, 9 ]
967 # %hash is (b => 1)
968
969 {
970 my @nums = delete local @$a[0, 2]
971 # @nums is (7, 9)
972 # $a is [ undef, 8 ]
973
974 $a[0] = 999; # will be erased when the scope ends
975 }
976 # $a is back to [ 7, 8, 9 ]
977
978 }
979 # %hash is back to its original state
980
981 Lvalue subroutines
982 It is possible to return a modifiable value from a subroutine. To do
983 this, you have to declare the subroutine to return an lvalue.
984
985 my $val;
986 sub canmod : lvalue {
987 $val; # or: return $val;
988 }
989 sub nomod {
990 $val;
991 }
992
993 canmod() = 5; # assigns to $val
994 nomod() = 5; # ERROR
995
996 The scalar/list context for the subroutine and for the right-hand side
997 of assignment is determined as if the subroutine call is replaced by a
998 scalar. For example, consider:
999
1000 data(2,3) = get_data(3,4);
1001
1002 Both subroutines here are called in a scalar context, while in:
1003
1004 (data(2,3)) = get_data(3,4);
1005
1006 and in:
1007
1008 (data(2),data(3)) = get_data(3,4);
1009
1010 all the subroutines are called in a list context.
1011
1012 Lvalue subroutines are convenient, but you have to keep in mind that,
1013 when used with objects, they may violate encapsulation. A normal
1014 mutator can check the supplied argument before setting the attribute it
1015 is protecting, an lvalue subroutine cannot. If you require any special
1016 processing when storing and retrieving the values, consider using the
1017 CPAN module Sentinel or something similar.
1018
1019 Lexical Subroutines
1020 Beginning with Perl 5.18, you can declare a private subroutine with
1021 "my" or "state". As with state variables, the "state" keyword is only
1022 available under "use feature 'state'" or "use 5.010" or higher.
1023
1024 Prior to Perl 5.26, lexical subroutines were deemed experimental and
1025 were available only under the "use feature 'lexical_subs'" pragma.
1026 They also produced a warning unless the "experimental::lexical_subs"
1027 warnings category was disabled.
1028
1029 These subroutines are only visible within the block in which they are
1030 declared, and only after that declaration:
1031
1032 # Include these two lines if your code is intended to run under Perl
1033 # versions earlier than 5.26.
1034 no warnings "experimental::lexical_subs";
1035 use feature 'lexical_subs';
1036
1037 foo(); # calls the package/global subroutine
1038 state sub foo {
1039 foo(); # also calls the package subroutine
1040 }
1041 foo(); # calls "state" sub
1042 my $ref = \&foo; # take a reference to "state" sub
1043
1044 my sub bar { ... }
1045 bar(); # calls "my" sub
1046
1047 You can't (directly) write a recursive lexical subroutine:
1048
1049 # WRONG
1050 my sub baz {
1051 baz();
1052 }
1053
1054 This example fails because "baz()" refers to the package/global
1055 subroutine "baz", not the lexical subroutine currently being defined.
1056
1057 The solution is to use "__SUB__":
1058
1059 my sub baz {
1060 __SUB__->(); # calls itself
1061 }
1062
1063 It is possible to predeclare a lexical subroutine. The "sub foo {...}"
1064 subroutine definition syntax respects any previous "my sub;" or "state
1065 sub;" declaration. Using this to define recursive subroutines is a bad
1066 idea, however:
1067
1068 my sub baz; # predeclaration
1069 sub baz { # define the "my" sub
1070 baz(); # WRONG: calls itself, but leaks memory
1071 }
1072
1073 Just like "my $f; $f = sub { $f->() }", this example leaks memory. The
1074 name "baz" is a reference to the subroutine, and the subroutine uses
1075 the name "baz"; they keep each other alive (see "Circular References"
1076 in perlref).
1077
1078 "state sub" vs "my sub"
1079
1080 What is the difference between "state" subs and "my" subs? Each time
1081 that execution enters a block when "my" subs are declared, a new copy
1082 of each sub is created. "State" subroutines persist from one execution
1083 of the containing block to the next.
1084
1085 So, in general, "state" subroutines are faster. But "my" subs are
1086 necessary if you want to create closures:
1087
1088 sub whatever {
1089 my $x = shift;
1090 my sub inner {
1091 ... do something with $x ...
1092 }
1093 inner();
1094 }
1095
1096 In this example, a new $x is created when "whatever" is called, and
1097 also a new "inner", which can see the new $x. A "state" sub will only
1098 see the $x from the first call to "whatever".
1099
1100 "our" subroutines
1101
1102 Like "our $variable", "our sub" creates a lexical alias to the package
1103 subroutine of the same name.
1104
1105 The two main uses for this are to switch back to using the package sub
1106 inside an inner scope:
1107
1108 sub foo { ... }
1109
1110 sub bar {
1111 my sub foo { ... }
1112 {
1113 # need to use the outer foo here
1114 our sub foo;
1115 foo();
1116 }
1117 }
1118
1119 and to make a subroutine visible to other packages in the same scope:
1120
1121 package MySneakyModule;
1122
1123 our sub do_something { ... }
1124
1125 sub do_something_with_caller {
1126 package DB;
1127 () = caller 1; # sets @DB::args
1128 do_something(@args); # uses MySneakyModule::do_something
1129 }
1130
1131 Passing Symbol Table Entries (typeglobs)
1132 WARNING: The mechanism described in this section was originally the
1133 only way to simulate pass-by-reference in older versions of Perl.
1134 While it still works fine in modern versions, the new reference
1135 mechanism is generally easier to work with. See below.
1136
1137 Sometimes you don't want to pass the value of an array to a subroutine
1138 but rather the name of it, so that the subroutine can modify the global
1139 copy of it rather than working with a local copy. In perl you can
1140 refer to all objects of a particular name by prefixing the name with a
1141 star: *foo. This is often known as a "typeglob", because the star on
1142 the front can be thought of as a wildcard match for all the funny
1143 prefix characters on variables and subroutines and such.
1144
1145 When evaluated, the typeglob produces a scalar value that represents
1146 all the objects of that name, including any filehandle, format, or
1147 subroutine. When assigned to, it causes the name mentioned to refer to
1148 whatever "*" value was assigned to it. Example:
1149
1150 sub doubleary {
1151 local(*someary) = @_;
1152 foreach $elem (@someary) {
1153 $elem *= 2;
1154 }
1155 }
1156 doubleary(*foo);
1157 doubleary(*bar);
1158
1159 Scalars are already passed by reference, so you can modify scalar
1160 arguments without using this mechanism by referring explicitly to $_[0]
1161 etc. You can modify all the elements of an array by passing all the
1162 elements as scalars, but you have to use the "*" mechanism (or the
1163 equivalent reference mechanism) to "push", "pop", or change the size of
1164 an array. It will certainly be faster to pass the typeglob (or
1165 reference).
1166
1167 Even if you don't want to modify an array, this mechanism is useful for
1168 passing multiple arrays in a single LIST, because normally the LIST
1169 mechanism will merge all the array values so that you can't extract out
1170 the individual arrays. For more on typeglobs, see "Typeglobs and
1171 Filehandles" in perldata.
1172
1173 When to Still Use local()
1174 Despite the existence of "my", there are still three places where the
1175 "local" operator still shines. In fact, in these three places, you
1176 must use "local" instead of "my".
1177
1178 1. You need to give a global variable a temporary value, especially
1179 $_.
1180
1181 The global variables, like @ARGV or the punctuation variables, must
1182 be "local"ized with "local()". This block reads in /etc/motd, and
1183 splits it up into chunks separated by lines of equal signs, which
1184 are placed in @Fields.
1185
1186 {
1187 local @ARGV = ("/etc/motd");
1188 local $/ = undef;
1189 local $_ = <>;
1190 @Fields = split /^\s*=+\s*$/;
1191 }
1192
1193 It particular, it's important to "local"ize $_ in any routine that
1194 assigns to it. Look out for implicit assignments in "while"
1195 conditionals.
1196
1197 2. You need to create a local file or directory handle or a local
1198 function.
1199
1200 A function that needs a filehandle of its own must use "local()" on
1201 a complete typeglob. This can be used to create new symbol table
1202 entries:
1203
1204 sub ioqueue {
1205 local (*READER, *WRITER); # not my!
1206 pipe (READER, WRITER) or die "pipe: $!";
1207 return (*READER, *WRITER);
1208 }
1209 ($head, $tail) = ioqueue();
1210
1211 See the Symbol module for a way to create anonymous symbol table
1212 entries.
1213
1214 Because assignment of a reference to a typeglob creates an alias,
1215 this can be used to create what is effectively a local function, or
1216 at least, a local alias.
1217
1218 {
1219 local *grow = \&shrink; # only until this block exits
1220 grow(); # really calls shrink()
1221 move(); # if move() grow()s, it shrink()s too
1222 }
1223 grow(); # get the real grow() again
1224
1225 See "Function Templates" in perlref for more about manipulating
1226 functions by name in this way.
1227
1228 3. You want to temporarily change just one element of an array or
1229 hash.
1230
1231 You can "local"ize just one element of an aggregate. Usually this
1232 is done on dynamics:
1233
1234 {
1235 local $SIG{INT} = 'IGNORE';
1236 funct(); # uninterruptible
1237 }
1238 # interruptibility automatically restored here
1239
1240 But it also works on lexically declared aggregates.
1241
1242 Pass by Reference
1243 If you want to pass more than one array or hash into a function--or
1244 return them from it--and have them maintain their integrity, then
1245 you're going to have to use an explicit pass-by-reference. Before you
1246 do that, you need to understand references as detailed in perlref.
1247 This section may not make much sense to you otherwise.
1248
1249 Here are a few simple examples. First, let's pass in several arrays to
1250 a function and have it "pop" all of then, returning a new list of all
1251 their former last elements:
1252
1253 @tailings = popmany ( \@a, \@b, \@c, \@d );
1254
1255 sub popmany {
1256 my $aref;
1257 my @retlist;
1258 foreach $aref ( @_ ) {
1259 push @retlist, pop @$aref;
1260 }
1261 return @retlist;
1262 }
1263
1264 Here's how you might write a function that returns a list of keys
1265 occurring in all the hashes passed to it:
1266
1267 @common = inter( \%foo, \%bar, \%joe );
1268 sub inter {
1269 my ($k, $href, %seen); # locals
1270 foreach $href (@_) {
1271 while ( $k = each %$href ) {
1272 $seen{$k}++;
1273 }
1274 }
1275 return grep { $seen{$_} == @_ } keys %seen;
1276 }
1277
1278 So far, we're using just the normal list return mechanism. What
1279 happens if you want to pass or return a hash? Well, if you're using
1280 only one of them, or you don't mind them concatenating, then the normal
1281 calling convention is ok, although a little expensive.
1282
1283 Where people get into trouble is here:
1284
1285 (@a, @b) = func(@c, @d);
1286 or
1287 (%a, %b) = func(%c, %d);
1288
1289 That syntax simply won't work. It sets just @a or %a and clears the @b
1290 or %b. Plus the function didn't get passed into two separate arrays or
1291 hashes: it got one long list in @_, as always.
1292
1293 If you can arrange for everyone to deal with this through references,
1294 it's cleaner code, although not so nice to look at. Here's a function
1295 that takes two array references as arguments, returning the two array
1296 elements in order of how many elements they have in them:
1297
1298 ($aref, $bref) = func(\@c, \@d);
1299 print "@$aref has more than @$bref\n";
1300 sub func {
1301 my ($cref, $dref) = @_;
1302 if (@$cref > @$dref) {
1303 return ($cref, $dref);
1304 } else {
1305 return ($dref, $cref);
1306 }
1307 }
1308
1309 It turns out that you can actually do this also:
1310
1311 (*a, *b) = func(\@c, \@d);
1312 print "@a has more than @b\n";
1313 sub func {
1314 local (*c, *d) = @_;
1315 if (@c > @d) {
1316 return (\@c, \@d);
1317 } else {
1318 return (\@d, \@c);
1319 }
1320 }
1321
1322 Here we're using the typeglobs to do symbol table aliasing. It's a tad
1323 subtle, though, and also won't work if you're using "my" variables,
1324 because only globals (even in disguise as "local"s) are in the symbol
1325 table.
1326
1327 If you're passing around filehandles, you could usually just use the
1328 bare typeglob, like *STDOUT, but typeglobs references work, too. For
1329 example:
1330
1331 splutter(\*STDOUT);
1332 sub splutter {
1333 my $fh = shift;
1334 print $fh "her um well a hmmm\n";
1335 }
1336
1337 $rec = get_rec(\*STDIN);
1338 sub get_rec {
1339 my $fh = shift;
1340 return scalar <$fh>;
1341 }
1342
1343 If you're planning on generating new filehandles, you could do this.
1344 Notice to pass back just the bare *FH, not its reference.
1345
1346 sub openit {
1347 my $path = shift;
1348 local *FH;
1349 return open (FH, $path) ? *FH : undef;
1350 }
1351
1352 Prototypes
1353 Perl supports a very limited kind of compile-time argument checking
1354 using function prototyping. This can be declared in either the PROTO
1355 section or with a prototype attribute. If you declare either of
1356
1357 sub mypush (\@@)
1358 sub mypush :prototype(\@@)
1359
1360 then "mypush()" takes arguments exactly like "push()" does.
1361
1362 If subroutine signatures are enabled (see "Signatures"), then the
1363 shorter PROTO syntax is unavailable, because it would clash with
1364 signatures. In that case, a prototype can only be declared in the form
1365 of an attribute.
1366
1367 The function declaration must be visible at compile time. The
1368 prototype affects only interpretation of new-style calls to the
1369 function, where new-style is defined as not using the "&" character.
1370 In other words, if you call it like a built-in function, then it
1371 behaves like a built-in function. If you call it like an old-fashioned
1372 subroutine, then it behaves like an old-fashioned subroutine. It
1373 naturally falls out from this rule that prototypes have no influence on
1374 subroutine references like "\&foo" or on indirect subroutine calls like
1375 "&{$subref}" or "$subref->()".
1376
1377 Method calls are not influenced by prototypes either, because the
1378 function to be called is indeterminate at compile time, since the exact
1379 code called depends on inheritance.
1380
1381 Because the intent of this feature is primarily to let you define
1382 subroutines that work like built-in functions, here are prototypes for
1383 some other functions that parse almost exactly like the corresponding
1384 built-in.
1385
1386 Declared as Called as
1387
1388 sub mylink ($$) mylink $old, $new
1389 sub myvec ($$$) myvec $var, $offset, 1
1390 sub myindex ($$;$) myindex &getstring, "substr"
1391 sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off
1392 sub myreverse (@) myreverse $a, $b, $c
1393 sub myjoin ($@) myjoin ":", $a, $b, $c
1394 sub mypop (\@) mypop @array
1395 sub mysplice (\@$$@) mysplice @array, 0, 2, @pushme
1396 sub mykeys (\[%@]) mykeys %{$hashref}
1397 sub myopen (*;$) myopen HANDLE, $name
1398 sub mypipe (**) mypipe READHANDLE, WRITEHANDLE
1399 sub mygrep (&@) mygrep { /foo/ } $a, $b, $c
1400 sub myrand (;$) myrand 42
1401 sub mytime () mytime
1402
1403 Any backslashed prototype character represents an actual argument that
1404 must start with that character (optionally preceded by "my", "our" or
1405 "local"), with the exception of "$", which will accept any scalar
1406 lvalue expression, such as "$foo = 7" or "my_function()->[0]". The
1407 value passed as part of @_ will be a reference to the actual argument
1408 given in the subroutine call, obtained by applying "\" to that
1409 argument.
1410
1411 You can use the "\[]" backslash group notation to specify more than one
1412 allowed argument type. For example:
1413
1414 sub myref (\[$@%&*])
1415
1416 will allow calling myref() as
1417
1418 myref $var
1419 myref @array
1420 myref %hash
1421 myref &sub
1422 myref *glob
1423
1424 and the first argument of myref() will be a reference to a scalar, an
1425 array, a hash, a code, or a glob.
1426
1427 Unbackslashed prototype characters have special meanings. Any
1428 unbackslashed "@" or "%" eats all remaining arguments, and forces list
1429 context. An argument represented by "$" forces scalar context. An "&"
1430 requires an anonymous subroutine, which, if passed as the first
1431 argument, does not require the "sub" keyword or a subsequent comma.
1432
1433 A "*" allows the subroutine to accept a bareword, constant, scalar
1434 expression, typeglob, or a reference to a typeglob in that slot. The
1435 value will be available to the subroutine either as a simple scalar, or
1436 (in the latter two cases) as a reference to the typeglob. If you wish
1437 to always convert such arguments to a typeglob reference, use
1438 Symbol::qualify_to_ref() as follows:
1439
1440 use Symbol 'qualify_to_ref';
1441
1442 sub foo (*) {
1443 my $fh = qualify_to_ref(shift, caller);
1444 ...
1445 }
1446
1447 The "+" prototype is a special alternative to "$" that will act like
1448 "\[@%]" when given a literal array or hash variable, but will otherwise
1449 force scalar context on the argument. This is useful for functions
1450 which should accept either a literal array or an array reference as the
1451 argument:
1452
1453 sub mypush (+@) {
1454 my $aref = shift;
1455 die "Not an array or arrayref" unless ref $aref eq 'ARRAY';
1456 push @$aref, @_;
1457 }
1458
1459 When using the "+" prototype, your function must check that the
1460 argument is of an acceptable type.
1461
1462 A semicolon (";") separates mandatory arguments from optional
1463 arguments. It is redundant before "@" or "%", which gobble up
1464 everything else.
1465
1466 As the last character of a prototype, or just before a semicolon, a "@"
1467 or a "%", you can use "_" in place of "$": if this argument is not
1468 provided, $_ will be used instead.
1469
1470 Note how the last three examples in the table above are treated
1471 specially by the parser. "mygrep()" is parsed as a true list operator,
1472 "myrand()" is parsed as a true unary operator with unary precedence the
1473 same as "rand()", and "mytime()" is truly without arguments, just like
1474 "time()". That is, if you say
1475
1476 mytime +2;
1477
1478 you'll get "mytime() + 2", not mytime(2), which is how it would be
1479 parsed without a prototype. If you want to force a unary function to
1480 have the same precedence as a list operator, add ";" to the end of the
1481 prototype:
1482
1483 sub mygetprotobynumber($;);
1484 mygetprotobynumber $a > $b; # parsed as mygetprotobynumber($a > $b)
1485
1486 The interesting thing about "&" is that you can generate new syntax
1487 with it, provided it's in the initial position:
1488
1489 sub try (&@) {
1490 my($try,$catch) = @_;
1491 eval { &$try };
1492 if ($@) {
1493 local $_ = $@;
1494 &$catch;
1495 }
1496 }
1497 sub catch (&) { $_[0] }
1498
1499 try {
1500 die "phooey";
1501 } catch {
1502 /phooey/ and print "unphooey\n";
1503 };
1504
1505 That prints "unphooey". (Yes, there are still unresolved issues having
1506 to do with visibility of @_. I'm ignoring that question for the
1507 moment. (But note that if we make @_ lexically scoped, those anonymous
1508 subroutines can act like closures... (Gee, is this sounding a little
1509 Lispish? (Never mind.))))
1510
1511 And here's a reimplementation of the Perl "grep" operator:
1512
1513 sub mygrep (&@) {
1514 my $code = shift;
1515 my @result;
1516 foreach $_ (@_) {
1517 push(@result, $_) if &$code;
1518 }
1519 @result;
1520 }
1521
1522 Some folks would prefer full alphanumeric prototypes. Alphanumerics
1523 have been intentionally left out of prototypes for the express purpose
1524 of someday in the future adding named, formal parameters. The current
1525 mechanism's main goal is to let module writers provide better
1526 diagnostics for module users. Larry feels the notation quite
1527 understandable to Perl programmers, and that it will not intrude
1528 greatly upon the meat of the module, nor make it harder to read. The
1529 line noise is visually encapsulated into a small pill that's easy to
1530 swallow.
1531
1532 If you try to use an alphanumeric sequence in a prototype you will
1533 generate an optional warning - "Illegal character in prototype...".
1534 Unfortunately earlier versions of Perl allowed the prototype to be used
1535 as long as its prefix was a valid prototype. The warning may be
1536 upgraded to a fatal error in a future version of Perl once the majority
1537 of offending code is fixed.
1538
1539 It's probably best to prototype new functions, not retrofit prototyping
1540 into older ones. That's because you must be especially careful about
1541 silent impositions of differing list versus scalar contexts. For
1542 example, if you decide that a function should take just one parameter,
1543 like this:
1544
1545 sub func ($) {
1546 my $n = shift;
1547 print "you gave me $n\n";
1548 }
1549
1550 and someone has been calling it with an array or expression returning a
1551 list:
1552
1553 func(@foo);
1554 func( $text =~ /\w+/g );
1555
1556 Then you've just supplied an automatic "scalar" in front of their
1557 argument, which can be more than a bit surprising. The old @foo which
1558 used to hold one thing doesn't get passed in. Instead, "func()" now
1559 gets passed in a 1; that is, the number of elements in @foo. And the
1560 "m//g" gets called in scalar context so instead of a list of words it
1561 returns a boolean result and advances "pos($text)". Ouch!
1562
1563 If a sub has both a PROTO and a BLOCK, the prototype is not applied
1564 until after the BLOCK is completely defined. This means that a
1565 recursive function with a prototype has to be predeclared for the
1566 prototype to take effect, like so:
1567
1568 sub foo($$);
1569 sub foo($$) {
1570 foo 1, 2;
1571 }
1572
1573 This is all very powerful, of course, and should be used only in
1574 moderation to make the world a better place.
1575
1576 Constant Functions
1577 Functions with a prototype of "()" are potential candidates for
1578 inlining. If the result after optimization and constant folding is
1579 either a constant or a lexically-scoped scalar which has no other
1580 references, then it will be used in place of function calls made
1581 without "&". Calls made using "&" are never inlined. (See constant.pm
1582 for an easy way to declare most constants.)
1583
1584 The following functions would all be inlined:
1585
1586 sub pi () { 3.14159 } # Not exact, but close.
1587 sub PI () { 4 * atan2 1, 1 } # As good as it gets,
1588 # and it's inlined, too!
1589 sub ST_DEV () { 0 }
1590 sub ST_INO () { 1 }
1591
1592 sub FLAG_FOO () { 1 << 8 }
1593 sub FLAG_BAR () { 1 << 9 }
1594 sub FLAG_MASK () { FLAG_FOO | FLAG_BAR }
1595
1596 sub OPT_BAZ () { not (0x1B58 & FLAG_MASK) }
1597
1598 sub N () { int(OPT_BAZ) / 3 }
1599
1600 sub FOO_SET () { 1 if FLAG_MASK & FLAG_FOO }
1601 sub FOO_SET2 () { if (FLAG_MASK & FLAG_FOO) { 1 } }
1602
1603 (Be aware that the last example was not always inlined in Perl 5.20 and
1604 earlier, which did not behave consistently with subroutines containing
1605 inner scopes.) You can countermand inlining by using an explicit
1606 "return":
1607
1608 sub baz_val () {
1609 if (OPT_BAZ) {
1610 return 23;
1611 }
1612 else {
1613 return 42;
1614 }
1615 }
1616 sub bonk_val () { return 12345 }
1617
1618 As alluded to earlier you can also declare inlined subs dynamically at
1619 BEGIN time if their body consists of a lexically-scoped scalar which
1620 has no other references. Only the first example here will be inlined:
1621
1622 BEGIN {
1623 my $var = 1;
1624 no strict 'refs';
1625 *INLINED = sub () { $var };
1626 }
1627
1628 BEGIN {
1629 my $var = 1;
1630 my $ref = \$var;
1631 no strict 'refs';
1632 *NOT_INLINED = sub () { $var };
1633 }
1634
1635 A not so obvious caveat with this (see [RT #79908]) is that the
1636 variable will be immediately inlined, and will stop behaving like a
1637 normal lexical variable, e.g. this will print 79907, not 79908:
1638
1639 BEGIN {
1640 my $x = 79907;
1641 *RT_79908 = sub () { $x };
1642 $x++;
1643 }
1644 print RT_79908(); # prints 79907
1645
1646 As of Perl 5.22, this buggy behavior, while preserved for backward
1647 compatibility, is detected and emits a deprecation warning. If you
1648 want the subroutine to be inlined (with no warning), make sure the
1649 variable is not used in a context where it could be modified aside from
1650 where it is declared.
1651
1652 # Fine, no warning
1653 BEGIN {
1654 my $x = 54321;
1655 *INLINED = sub () { $x };
1656 }
1657 # Warns. Future Perl versions will stop inlining it.
1658 BEGIN {
1659 my $x;
1660 $x = 54321;
1661 *ALSO_INLINED = sub () { $x };
1662 }
1663
1664 Perl 5.22 also introduces the experimental "const" attribute as an
1665 alternative. (Disable the "experimental::const_attr" warnings if you
1666 want to use it.) When applied to an anonymous subroutine, it forces
1667 the sub to be called when the "sub" expression is evaluated. The
1668 return value is captured and turned into a constant subroutine:
1669
1670 my $x = 54321;
1671 *INLINED = sub : const { $x };
1672 $x++;
1673
1674 The return value of "INLINED" in this example will always be 54321,
1675 regardless of later modifications to $x. You can also put any
1676 arbitrary code inside the sub, at it will be executed immediately and
1677 its return value captured the same way.
1678
1679 If you really want a subroutine with a "()" prototype that returns a
1680 lexical variable you can easily force it to not be inlined by adding an
1681 explicit "return":
1682
1683 BEGIN {
1684 my $x = 79907;
1685 *RT_79908 = sub () { return $x };
1686 $x++;
1687 }
1688 print RT_79908(); # prints 79908
1689
1690 The easiest way to tell if a subroutine was inlined is by using
1691 B::Deparse. Consider this example of two subroutines returning 1, one
1692 with a "()" prototype causing it to be inlined, and one without (with
1693 deparse output truncated for clarity):
1694
1695 $ perl -MO=Deparse -le 'sub ONE { 1 } if (ONE) { print ONE if ONE }'
1696 sub ONE {
1697 1;
1698 }
1699 if (ONE ) {
1700 print ONE() if ONE ;
1701 }
1702 $ perl -MO=Deparse -le 'sub ONE () { 1 } if (ONE) { print ONE if ONE }'
1703 sub ONE () { 1 }
1704 do {
1705 print 1
1706 };
1707
1708 If you redefine a subroutine that was eligible for inlining, you'll get
1709 a warning by default. You can use this warning to tell whether or not
1710 a particular subroutine is considered inlinable, since it's different
1711 than the warning for overriding non-inlined subroutines:
1712
1713 $ perl -e 'sub one () {1} sub one () {2}'
1714 Constant subroutine one redefined at -e line 1.
1715 $ perl -we 'sub one {1} sub one {2}'
1716 Subroutine one redefined at -e line 1.
1717
1718 The warning is considered severe enough not to be affected by the -w
1719 switch (or its absence) because previously compiled invocations of the
1720 function will still be using the old value of the function. If you
1721 need to be able to redefine the subroutine, you need to ensure that it
1722 isn't inlined, either by dropping the "()" prototype (which changes
1723 calling semantics, so beware) or by thwarting the inlining mechanism in
1724 some other way, e.g. by adding an explicit "return", as mentioned
1725 above:
1726
1727 sub not_inlined () { return 23 }
1728
1729 Overriding Built-in Functions
1730 Many built-in functions may be overridden, though this should be tried
1731 only occasionally and for good reason. Typically this might be done by
1732 a package attempting to emulate missing built-in functionality on a
1733 non-Unix system.
1734
1735 Overriding may be done only by importing the name from a module at
1736 compile time--ordinary predeclaration isn't good enough. However, the
1737 "use subs" pragma lets you, in effect, predeclare subs via the import
1738 syntax, and these names may then override built-in ones:
1739
1740 use subs 'chdir', 'chroot', 'chmod', 'chown';
1741 chdir $somewhere;
1742 sub chdir { ... }
1743
1744 To unambiguously refer to the built-in form, precede the built-in name
1745 with the special package qualifier "CORE::". For example, saying
1746 "CORE::open()" always refers to the built-in "open()", even if the
1747 current package has imported some other subroutine called "&open()"
1748 from elsewhere. Even though it looks like a regular function call, it
1749 isn't: the CORE:: prefix in that case is part of Perl's syntax, and
1750 works for any keyword, regardless of what is in the CORE package.
1751 Taking a reference to it, that is, "\&CORE::open", only works for some
1752 keywords. See CORE.
1753
1754 Library modules should not in general export built-in names like "open"
1755 or "chdir" as part of their default @EXPORT list, because these may
1756 sneak into someone else's namespace and change the semantics
1757 unexpectedly. Instead, if the module adds that name to @EXPORT_OK,
1758 then it's possible for a user to import the name explicitly, but not
1759 implicitly. That is, they could say
1760
1761 use Module 'open';
1762
1763 and it would import the "open" override. But if they said
1764
1765 use Module;
1766
1767 they would get the default imports without overrides.
1768
1769 The foregoing mechanism for overriding built-in is restricted, quite
1770 deliberately, to the package that requests the import. There is a
1771 second method that is sometimes applicable when you wish to override a
1772 built-in everywhere, without regard to namespace boundaries. This is
1773 achieved by importing a sub into the special namespace
1774 "CORE::GLOBAL::". Here is an example that quite brazenly replaces the
1775 "glob" operator with something that understands regular expressions.
1776
1777 package REGlob;
1778 require Exporter;
1779 @ISA = 'Exporter';
1780 @EXPORT_OK = 'glob';
1781
1782 sub import {
1783 my $pkg = shift;
1784 return unless @_;
1785 my $sym = shift;
1786 my $where = ($sym =~ s/^GLOBAL_// ? 'CORE::GLOBAL' : caller(0));
1787 $pkg->export($where, $sym, @_);
1788 }
1789
1790 sub glob {
1791 my $pat = shift;
1792 my @got;
1793 if (opendir my $d, '.') {
1794 @got = grep /$pat/, readdir $d;
1795 closedir $d;
1796 }
1797 return @got;
1798 }
1799 1;
1800
1801 And here's how it could be (ab)used:
1802
1803 #use REGlob 'GLOBAL_glob'; # override glob() in ALL namespaces
1804 package Foo;
1805 use REGlob 'glob'; # override glob() in Foo:: only
1806 print for <^[a-z_]+\.pm\$>; # show all pragmatic modules
1807
1808 The initial comment shows a contrived, even dangerous example. By
1809 overriding "glob" globally, you would be forcing the new (and
1810 subversive) behavior for the "glob" operator for every namespace,
1811 without the complete cognizance or cooperation of the modules that own
1812 those namespaces. Naturally, this should be done with extreme
1813 caution--if it must be done at all.
1814
1815 The "REGlob" example above does not implement all the support needed to
1816 cleanly override perl's "glob" operator. The built-in "glob" has
1817 different behaviors depending on whether it appears in a scalar or list
1818 context, but our "REGlob" doesn't. Indeed, many perl built-in have
1819 such context sensitive behaviors, and these must be adequately
1820 supported by a properly written override. For a fully functional
1821 example of overriding "glob", study the implementation of
1822 "File::DosGlob" in the standard library.
1823
1824 When you override a built-in, your replacement should be consistent (if
1825 possible) with the built-in native syntax. You can achieve this by
1826 using a suitable prototype. To get the prototype of an overridable
1827 built-in, use the "prototype" function with an argument of
1828 "CORE::builtin_name" (see "prototype" in perlfunc).
1829
1830 Note however that some built-ins can't have their syntax expressed by a
1831 prototype (such as "system" or "chomp"). If you override them you
1832 won't be able to fully mimic their original syntax.
1833
1834 The built-ins "do", "require" and "glob" can also be overridden, but
1835 due to special magic, their original syntax is preserved, and you don't
1836 have to define a prototype for their replacements. (You can't override
1837 the "do BLOCK" syntax, though).
1838
1839 "require" has special additional dark magic: if you invoke your
1840 "require" replacement as "require Foo::Bar", it will actually receive
1841 the argument "Foo/Bar.pm" in @_. See "require" in perlfunc.
1842
1843 And, as you'll have noticed from the previous example, if you override
1844 "glob", the "<*>" glob operator is overridden as well.
1845
1846 In a similar fashion, overriding the "readline" function also overrides
1847 the equivalent I/O operator "<FILEHANDLE>". Also, overriding
1848 "readpipe" also overrides the operators "``" and "qx//".
1849
1850 Finally, some built-ins (e.g. "exists" or "grep") can't be overridden.
1851
1852 Autoloading
1853 If you call a subroutine that is undefined, you would ordinarily get an
1854 immediate, fatal error complaining that the subroutine doesn't exist.
1855 (Likewise for subroutines being used as methods, when the method
1856 doesn't exist in any base class of the class's package.) However, if
1857 an "AUTOLOAD" subroutine is defined in the package or packages used to
1858 locate the original subroutine, then that "AUTOLOAD" subroutine is
1859 called with the arguments that would have been passed to the original
1860 subroutine. The fully qualified name of the original subroutine
1861 magically appears in the global $AUTOLOAD variable of the same package
1862 as the "AUTOLOAD" routine. The name is not passed as an ordinary
1863 argument because, er, well, just because, that's why. (As an
1864 exception, a method call to a nonexistent "import" or "unimport" method
1865 is just skipped instead. Also, if the AUTOLOAD subroutine is an XSUB,
1866 there are other ways to retrieve the subroutine name. See "Autoloading
1867 with XSUBs" in perlguts for details.)
1868
1869 Many "AUTOLOAD" routines load in a definition for the requested
1870 subroutine using eval(), then execute that subroutine using a special
1871 form of goto() that erases the stack frame of the "AUTOLOAD" routine
1872 without a trace. (See the source to the standard module documented in
1873 AutoLoader, for example.) But an "AUTOLOAD" routine can also just
1874 emulate the routine and never define it. For example, let's pretend
1875 that a function that wasn't defined should just invoke "system" with
1876 those arguments. All you'd do is:
1877
1878 sub AUTOLOAD {
1879 our $AUTOLOAD; # keep 'use strict' happy
1880 my $program = $AUTOLOAD;
1881 $program =~ s/.*:://;
1882 system($program, @_);
1883 }
1884 date();
1885 who();
1886 ls('-l');
1887
1888 In fact, if you predeclare functions you want to call that way, you
1889 don't even need parentheses:
1890
1891 use subs qw(date who ls);
1892 date;
1893 who;
1894 ls '-l';
1895
1896 A more complete example of this is the Shell module on CPAN, which can
1897 treat undefined subroutine calls as calls to external programs.
1898
1899 Mechanisms are available to help modules writers split their modules
1900 into autoloadable files. See the standard AutoLoader module described
1901 in AutoLoader and in AutoSplit, the standard SelfLoader modules in
1902 SelfLoader, and the document on adding C functions to Perl code in
1903 perlxs.
1904
1905 Subroutine Attributes
1906 A subroutine declaration or definition may have a list of attributes
1907 associated with it. If such an attribute list is present, it is broken
1908 up at space or colon boundaries and treated as though a "use
1909 attributes" had been seen. See attributes for details about what
1910 attributes are currently supported. Unlike the limitation with the
1911 obsolescent "use attrs", the "sub : ATTRLIST" syntax works to associate
1912 the attributes with a pre-declaration, and not just with a subroutine
1913 definition.
1914
1915 The attributes must be valid as simple identifier names (without any
1916 punctuation other than the '_' character). They may have a parameter
1917 list appended, which is only checked for whether its parentheses
1918 ('(',')') nest properly.
1919
1920 Examples of valid syntax (even though the attributes are unknown):
1921
1922 sub fnord (&\%) : switch(10,foo(7,3)) : expensive;
1923 sub plugh () : Ugly('\(") :Bad;
1924 sub xyzzy : _5x5 { ... }
1925
1926 Examples of invalid syntax:
1927
1928 sub fnord : switch(10,foo(); # ()-string not balanced
1929 sub snoid : Ugly('('); # ()-string not balanced
1930 sub xyzzy : 5x5; # "5x5" not a valid identifier
1931 sub plugh : Y2::north; # "Y2::north" not a simple identifier
1932 sub snurt : foo + bar; # "+" not a colon or space
1933
1934 The attribute list is passed as a list of constant strings to the code
1935 which associates them with the subroutine. In particular, the second
1936 example of valid syntax above currently looks like this in terms of how
1937 it's parsed and invoked:
1938
1939 use attributes __PACKAGE__, \&plugh, q[Ugly('\(")], 'Bad';
1940
1941 For further details on attribute lists and their manipulation, see
1942 attributes and Attribute::Handlers.
1943
1945 See "Function Templates" in perlref for more about references and
1946 closures. See perlxs if you'd like to learn about calling C
1947 subroutines from Perl. See perlembed if you'd like to learn about
1948 calling Perl subroutines from C. See perlmod to learn about bundling
1949 up your functions in separate files. See perlmodlib to learn what
1950 library modules come standard on your system. See perlootut to learn
1951 how to make object method calls.
1952
1953
1954
1955perl v5.30.2 2020-03-27 PERLSUB(1)