Updated.
[p5sagit/p5-mst-13.2.git] / pod / perlsub.pod
CommitLineData
a0d0e21e 1=head1 NAME
2
3perlsub - Perl subroutines
4
5=head1 SYNOPSIS
6
7To declare subroutines:
8
cb1a09d0 9 sub NAME; # A "forward" declaration.
10 sub NAME(PROTO); # ditto, but with prototypes
11
12 sub NAME BLOCK # A declaration and a definition.
13 sub NAME(PROTO) BLOCK # ditto, but with prototypes
a0d0e21e 14
748a9306 15To define an anonymous subroutine at runtime:
16
17 $subref = sub BLOCK;
18
a0d0e21e 19To import subroutines:
20
21 use PACKAGE qw(NAME1 NAME2 NAME3);
22
23To call subroutines:
24
a0d0e21e 25 NAME(LIST); # & is optional with parens.
26 NAME LIST; # Parens optional if predeclared/imported.
cb1a09d0 27 &NAME; # Passes current @_ to subroutine.
a0d0e21e 28
29=head1 DESCRIPTION
30
cb1a09d0 31Like many languages, Perl provides for user-defined subroutines. These
32may be located anywhere in the main program, loaded in from other files
33via the C<do>, C<require>, or C<use> keywords, or even generated on the
34fly using C<eval> or anonymous subroutines (closures). You can even call
35a function indirectly using a variable containing its name or a CODE reference.
36
37The Perl model for function call and return values is simple: all
38functions are passed as parameters one single flat list of scalars, and
39all functions likewise return to their caller one single flat list of
40scalars. Any arrays or hashes in these call and return lists will
41collapse, losing their identities--but you may always use
42pass-by-reference instead to avoid this. Both call and return lists may
43contain as many or as few scalar elements as you'd like. (Often a
44function without an explicit return statement is called a subroutine, but
45there's really no difference from the language's perspective.)
46
47Any arguments passed to the routine come in as the array @_. Thus if you
48called a function with two arguments, those would be stored in C<$_[0]>
49and C<$_[1]>. The array @_ is a local array, but its values are implicit
50references (predating L<perlref>) to the actual scalar parameters. The
51return value of the subroutine is the value of the last expression
52evaluated. Alternatively, a return statement may be used to specify the
53returned value and exit the subroutine. If you return one or more arrays
54and/or hashes, these will be flattened together into one large
55indistinguishable list.
56
57Perl does not have named formal parameters, but in practice all you do is
58assign to a my() list of these. Any variables you use in the function
59that aren't declared private are global variables. For the gory details
60on creating private variables, see the sections below on L<"Private
61Variables via my()"> and L</"Temporary Values via local()">. To create
62protected environments for a set of functions in a separate package (and
63probably a separate file), see L<perlmod/"Packages">.
a0d0e21e 64
65Example:
66
cb1a09d0 67 sub max {
68 my $max = shift(@_);
a0d0e21e 69 foreach $foo (@_) {
70 $max = $foo if $max < $foo;
71 }
cb1a09d0 72 return $max;
a0d0e21e 73 }
cb1a09d0 74 $bestday = max($mon,$tue,$wed,$thu,$fri);
a0d0e21e 75
76Example:
77
78 # get a line, combining continuation lines
79 # that start with whitespace
80
81 sub get_line {
cb1a09d0 82 $thisline = $lookahead; # GLOBAL VARIABLES!!
a0d0e21e 83 LINE: while ($lookahead = <STDIN>) {
84 if ($lookahead =~ /^[ \t]/) {
85 $thisline .= $lookahead;
86 }
87 else {
88 last LINE;
89 }
90 }
91 $thisline;
92 }
93
94 $lookahead = <STDIN>; # get first line
95 while ($_ = get_line()) {
96 ...
97 }
98
99Use array assignment to a local list to name your formal arguments:
100
101 sub maybeset {
102 my($key, $value) = @_;
cb1a09d0 103 $Foo{$key} = $value unless $Foo{$key};
a0d0e21e 104 }
105
cb1a09d0 106This also has the effect of turning call-by-reference into call-by-value,
107since the assignment copies the values. Otherwise a function is free to
108do in-place modifications of @_ and change its callers values.
109
110 upcase_in($v1, $v2); # this changes $v1 and $v2
111 sub upcase_in {
112 for (@_) { tr/a-z/A-Z/ }
113 }
114
115You aren't allowed to modify constants in this way, of course. If an
116argument were actually literal and you tried to change it, you'd take a
117(presumably fatal) exception. For example, this won't work:
118
119 upcase_in("frederick");
120
121It would be much safer if the upcase_in() function
122were written to return a copy of its parameters instead
123of changing them in place:
124
125 ($v3, $v4) = upcase($v1, $v2); # this doesn't
126 sub upcase {
127 my @parms = @_;
128 for (@parms) { tr/a-z/A-Z/ }
129 return @parms;
130 }
131
132Notice how this (unprototyped) function doesn't care whether it was passed
133real scalars or arrays. Perl will see everything as one big long flat @_
134parameter list. This is one of the ways where Perl's simple
135argument-passing style shines. The upcase() function would work perfectly
136well without changing the upcase() definition even if we fed it things
137like this:
138
139 @newlist = upcase(@list1, @list2);
140 @newlist = upcase( split /:/, $var );
141
142Do not, however, be tempted to do this:
143
144 (@a, @b) = upcase(@list1, @list2);
145
146Because like its flat incoming parameter list, the return list is also
147flat. So all you have managed to do here is stored everything in @a and
148made @b an empty list. See L</"Pass by Reference"> for alternatives.
149
150A subroutine may be called using the "&" prefix. The "&" is optional in
151Perl 5, and so are the parens if the subroutine has been predeclared.
152(Note, however, that the "&" is I<NOT> optional when you're just naming
153the subroutine, such as when it's used as an argument to defined() or
154undef(). Nor is it optional when you want to do an indirect subroutine
155call with a subroutine name or reference using the C<&$subref()> or
156C<&{$subref}()> constructs. See L<perlref> for more on that.)
a0d0e21e 157
158Subroutines may be called recursively. If a subroutine is called using
cb1a09d0 159the "&" form, the argument list is optional, and if omitted, no @_ array is
160set up for the subroutine: the @_ array at the time of the call is
161visible to subroutine instead. This is an efficiency mechanism that
162new users may wish to avoid.
a0d0e21e 163
164 &foo(1,2,3); # pass three arguments
165 foo(1,2,3); # the same
166
167 foo(); # pass a null list
168 &foo(); # the same
a0d0e21e 169
cb1a09d0 170 &foo; # foo() get current args, like foo(@_) !!
171 foo; # like foo() IFF sub foo pre-declared, else "foo"
172
173=head2 Private Variables via my()
174
175Synopsis:
176
177 my $foo; # declare $foo lexically local
178 my (@wid, %get); # declare list of variables local
179 my $foo = "flurp"; # declare $foo lexical, and init it
180 my @oof = @bar; # declare @oof lexical, and init it
181
182A "my" declares the listed variables to be confined (lexically) to the
183enclosing block, subroutine, C<eval>, or C<do/require/use>'d file. If
184more than one value is listed, the list must be placed in parens. All
185listed elements must be legal lvalues. Only alphanumeric identifiers may
186be lexically scoped--magical builtins like $/ must currently be localized with
187"local" instead.
188
189Unlike dynamic variables created by the "local" statement, lexical
190variables declared with "my" are totally hidden from the outside world,
191including any called subroutines (even if it's the same subroutine called
192from itself or elsewhere--every call gets its own copy).
193
194(An eval(), however, can see the lexical variables of the scope it is
195being evaluated in so long as the names aren't hidden by declarations within
196the eval() itself. See L<perlref>.)
197
198The parameter list to my() may be assigned to if desired, which allows you
199to initialize your variables. (If no initializer is given for a
200particular variable, it is created with the undefined value.) Commonly
201this is used to name the parameters to a subroutine. Examples:
202
203 $arg = "fred"; # "global" variable
204 $n = cube_root(27);
205 print "$arg thinks the root is $n\n";
206 fred thinks the root is 3
207
208 sub cube_root {
209 my $arg = shift; # name doesn't matter
210 $arg **= 1/3;
211 return $arg;
212 }
213
214The "my" is simply a modifier on something you might assign to. So when
215you do assign to the variables in its argument list, the "my" doesn't
216change whether those variables is viewed as a scalar or an array. So
217
218 my ($foo) = <STDIN>;
219 my @FOO = <STDIN>;
220
221both supply a list context to the righthand side, while
222
223 my $foo = <STDIN>;
224
225supplies a scalar context. But the following only declares one variable:
748a9306 226
cb1a09d0 227 my $foo, $bar = 1;
748a9306 228
cb1a09d0 229That has the same effect as
748a9306 230
cb1a09d0 231 my $foo;
232 $bar = 1;
a0d0e21e 233
cb1a09d0 234The declared variable is not introduced (is not visible) until after
235the current statement. Thus,
236
237 my $x = $x;
238
239can be used to initialize the new $x with the value of the old $x, and
240the expression
241
242 my $x = 123 and $x == 123
243
244is false unless the old $x happened to have the value 123.
245
246Some users may wish to encourage the use of lexically scoped variables.
247As an aid to catching implicit references to package variables,
248if you say
249
250 use strict 'vars';
251
252then any variable reference from there to the end of the enclosing
253block must either refer to a lexical variable, or must be fully
254qualified with the package name. A compilation error results
255otherwise. An inner block may countermand this with S<"no strict 'vars'">.
256
257A my() has both a compile-time and a run-time effect. At compile time,
258the compiler takes notice of it; the principle usefulness of this is to
259quiet C<use strict 'vars'>. The actual initialization doesn't happen
260until run time, so gets executed every time through a loop.
261
262Variables declared with "my" are not part of any package and are therefore
263never fully qualified with the package name. In particular, you're not
264allowed to try to make a package variable (or other global) lexical:
265
266 my $pack::var; # ERROR! Illegal syntax
267 my $_; # also illegal (currently)
268
269In fact, a dynamic variable (also known as package or global variables)
270are still accessible using the fully qualified :: notation even while a
271lexical of the same name is also visible:
272
273 package main;
274 local $x = 10;
275 my $x = 20;
276 print "$x and $::x\n";
277
278That will print out 20 and 10.
279
280You may declare "my" variables at the outer most scope of a file to
281totally hide any such identifiers from the outside world. This is similar
282to a C's static variables at the file level. To do this with a subroutine
283requires the use of a closure (anonymous function). If a block (such as
284an eval(), function, or C<package>) wants to create a private subroutine
285that cannot be called from outside that block, it can declare a lexical
286variable containing an anonymous sub reference:
287
288 my $secret_version = '1.001-beta';
289 my $secret_sub = sub { print $secret_version };
290 &$secret_sub();
291
292As long as the reference is never returned by any function within the
293module, no outside module can see the subroutine, since its name is not in
294any package's symbol table. Remember that it's not I<REALLY> called
295$some_pack::secret_version or anything; it's just $secret_version,
296unqualified and unqualifiable.
297
298This does not work with object methods, however; all object methods have
299to be in the symbol table of some package to be found.
300
301Just because the lexical variable is lexically (also called statically)
302scoped doesn't mean that within a function it works like a C static. It
303normally works more like a C auto. But here's a mechanism for giving a
304function private variables with both lexical scoping and a static
305lifetime. If you do want to create something like C's static variables,
306just enclose the whole function in an extra block, and put the
307static variable outside the function but in the block.
308
309 {
310 my $secret_val = 0;
311 sub gimme_another {
312 return ++$secret_val;
313 }
314 }
315 # $secret_val now becomes unreachable by the outside
316 # world, but retains its value between calls to gimme_another
317
318If this function is being sourced in from a separate file
319via C<require> or C<use>, then this is probably just fine. If it's
320all in the main program, you'll need to arrange for the my()
321to be executed early, either by putting the whole block above
322your pain program, or more likely, merely placing a BEGIN
323sub around it to make sure it gets executed before your program
324starts to run:
325
326 sub BEGIN {
327 my $secret_val = 0;
328 sub gimme_another {
329 return ++$secret_val;
330 }
331 }
332
333See L<perlrun> about the BEGIN function.
334
335=head2 Temporary Values via local()
336
337B<NOTE>: In general, you should be using "my" instead of "local", because
338it's faster and safer. Execeptions to this include the global punctuation
339variables, filehandles and formats, and direct manipulation of the Perl
340symbol table itself. Format variables often use "local" though, as do
341other variables whose current value must be visible to called
342subroutines.
343
344Synopsis:
345
346 local $foo; # declare $foo dynamically local
347 local (@wid, %get); # declare list of variables local
348 local $foo = "flurp"; # declare $foo dynamic, and init it
349 local @oof = @bar; # declare @oof dynamic, and init it
350
351 local *FH; # localize $FH, @FH, %FH, &FH ...
352 local *merlyn = *randal; # now $merlyn is really $randal, plus
353 # @merlyn is really @randal, etc
354 local *merlyn = 'randal'; # SAME THING: promote 'randal' to *randal
355 local *merlyn = \$randal; # just alias $merlyn, not @merlyn etc
356
357A local() modifies its listed variables to be local to the enclosing
358block, (or subroutine, C<eval{}> or C<do>) and I<the any called from
359within that block>. A local() just gives temporary values to global
360(meaning package) variables. This is known as dynamic scoping. Lexical
361scoping is done with "my", which works more like C's auto declarations.
362
363If more than one variable is given to local(), they must be placed in
364parens. All listed elements must be legal lvalues. This operator works
365by saving the current values of those variables in its argument list on a
366hidden stack and restoring them upon exiting the block, subroutine or
367eval. This means that called subroutines can also reference the local
368variable, but not the global one. The argument list may be assigned to if
369desired, which allows you to initialize your local variables. (If no
370initializer is given for a particular variable, it is created with an
371undefined value.) Commonly this is used to name the parameters to a
372subroutine. Examples:
373
374 for $i ( 0 .. 9 ) {
375 $digits{$i} = $i;
376 }
377 # assume this function uses global %digits hash
378 parse_num();
379
380 # now temporarily add to %digits hash
381 if ($base12) {
382 # (NOTE: not claiming this is efficient!)
383 local %digits = (%digits, 't' => 10, 'e' => 11);
384 parse_num(); # parse_num gets this new %digits!
385 }
386 # old %digits restored here
387
388Because local() is a run-time command, and so gets executed every time
389through a loop. In releases of Perl previous to 5.0, this used more stack
390storage each time until the loop was exited. Perl now reclaims the space
391each time through, but it's still more efficient to declare your variables
392outside the loop.
393
394A local is simply a modifier on an lvalue expression. When you assign to
395a localized variable, the local doesn't change whether its list is viewed
396as a scalar or an array. So
397
398 local($foo) = <STDIN>;
399 local @FOO = <STDIN>;
400
401both supply a list context to the righthand side, while
402
403 local $foo = <STDIN>;
404
405supplies a scalar context.
406
407=head2 Passing Symbol Table Entries (typeglobs)
408
409[Note: The mechanism described in this section was originally the only
410way to simulate pass-by-reference in older versions of Perl. While it
411still works fine in modern versions, the new reference mechanism is
412generally easier to work with. See below.]
a0d0e21e 413
414Sometimes you don't want to pass the value of an array to a subroutine
415but rather the name of it, so that the subroutine can modify the global
416copy of it rather than working with a local copy. In perl you can
cb1a09d0 417refer to all objects of a particular name by prefixing the name
a0d0e21e 418with a star: C<*foo>. This is often known as a "type glob", since the
419star on the front can be thought of as a wildcard match for all the
420funny prefix characters on variables and subroutines and such.
421
422When evaluated, the type glob produces a scalar value that represents
423all the objects of that name, including any filehandle, format or
424subroutine. When assigned to, it causes the name mentioned to refer to
425whatever "*" value was assigned to it. Example:
426
427 sub doubleary {
428 local(*someary) = @_;
429 foreach $elem (@someary) {
430 $elem *= 2;
431 }
432 }
433 doubleary(*foo);
434 doubleary(*bar);
435
436Note that scalars are already passed by reference, so you can modify
437scalar arguments without using this mechanism by referring explicitly
438to $_[0] etc. You can modify all the elements of an array by passing
439all the elements as scalars, but you have to use the * mechanism (or
440the equivalent reference mechanism) to push, pop or change the size of
441an array. It will certainly be faster to pass the typeglob (or reference).
442
443Even if you don't want to modify an array, this mechanism is useful for
444passing multiple arrays in a single LIST, since normally the LIST
445mechanism will merge all the array values so that you can't extract out
cb1a09d0 446the individual arrays. For more on typeglobs, see L<perldata/"Typeglobs">.
447
448=head2 Pass by Reference
449
450If you want to pass more than one array or hash into a function--or
451return them from it--and have them maintain their integrity,
452then you're going to have to use an explicit pass-by-reference.
453Before you do that, you need to understand references; see L<perlref>.
454
455Here are a few simple examples. First, let's pass in several
456arrays to a function and have it pop all of then, return a new
457list of all their former last elements:
458
459 @tailings = popmany ( \@a, \@b, \@c, \@d );
460
461 sub popmany {
462 my $aref;
463 my @retlist = ();
464 foreach $aref ( @_ ) {
465 push @retlist, pop @$aref;
466 }
467 return @retlist;
468 }
469
470Here's how you might write a function that returns a
471list of keys occurring in all the hashes passed to it:
472
473 @common = inter( \%foo, \%bar, \%joe );
474 sub inter {
475 my ($k, $href, %seen); # locals
476 foreach $href (@_) {
477 while ( $k = each %$href ) {
478 $seen{$k}++;
479 }
480 }
481 return grep { $seen{$_} == @_ } keys %seen;
482 }
483
484So far, we're just using the normal list return mechanism.
485What happens if you want to pass or return a hash? Well,
486if you're only using one of them, or you don't mind them
487concatenating, then the normal calling convention is ok, although
488a little expensive.
489
490Where people get into trouble is here:
491
492 (@a, @b) = func(@c, @d);
493or
494 (%a, %b) = func(%c, %d);
495
496That syntax simply won't work. It just sets @a or %a and clears the @b or
497%b. Plus the function didn't get passed into two separate arrays or
498hashes: it got one long list in @_, as always.
499
500If you can arrange for everyone to deal with this through references, it's
501cleaner code, although not so nice to look at. Here's a function that
502takes two array references as arguments, returning the two array elements
503in order of how many elements they have in them:
504
505 ($aref, $bref) = func(\@c, \@d);
506 print "@$aref has more than @$bref\n";
507 sub func {
508 my ($cref, $dref) = @_;
509 if (@$cref > @$dref) {
510 return ($cref, $dref);
511 } else {
512 return ($cref, $cref);
513 }
514 }
515
516It turns out that you can actually do this also:
517
518 (*a, *b) = func(\@c, \@d);
519 print "@a has more than @b\n";
520 sub func {
521 local (*c, *d) = @_;
522 if (@c > @d) {
523 return (\@c, \@d);
524 } else {
525 return (\@d, \@c);
526 }
527 }
528
529Here we're using the typeglobs to do symbol table aliasing. It's
530a tad subtle, though, and also won't work if you're using my()
531variables, since only globals (well, and local()s) are in the symbol table.
532
533If you're passing around filehandles, you could usually just use the bare
534typeglob, like *STDOUT, but typeglobs references would be better because
535they'll still work properly under C<use strict 'refs'>. For example:
536
537 splutter(\*STDOUT);
538 sub splutter {
539 my $fh = shift;
540 print $fh "her um well a hmmm\n";
541 }
542
543 $rec = get_rec(\*STDIN);
544 sub get_rec {
545 my $fh = shift;
546 return scalar <$fh>;
547 }
548
549If you're planning on generating new filehandles, you could do this:
550
551 sub openit {
552 my $name = shift;
553 local *FH;
554 return open (FH, $path) ? \*FH : undef;
555 }
556
557Although that will actually produce a small memory leak. See the bottom
558of L<perlfunc/open()> for a somewhat cleaner way using the FileHandle
559functions supplied with the POSIX package.
560
561=head2 Prototypes
562
563As of the 5.002 release of perl, if you declare
564
565 sub mypush (\@@)
566
567then mypush() takes arguments exactly like push() does. (This only works
568for function calls that are visible at compile time, not indirect function
569calls through a C<&$func> reference nor for method calls as described in
570L<perlobj>.)
571
572Here are the prototypes for some other functions that parse almost exactly
573like the corresponding builtins.
574
575 Declared as Called as
576
577 sub mylink ($$) mylink $old, $new
578 sub myvec ($$$) myvec $var, $offset, 1
579 sub myindex ($$;$) myindex &getstring, "substr"
580 sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off
581 sub myreverse (@) myreverse $a,$b,$c
582 sub myjoin ($@) myjoin ":",$a,$b,$c
583 sub mypop (\@) mypop @array
584 sub mysplice (\@$$@) mysplice @array,@array,0,@pushme
585 sub mykeys (\%) mykeys %{$hashref}
586 sub myopen (*;$) myopen HANDLE, $name
587 sub mypipe (**) mypipe READHANDLE, WRITEHANDLE
588 sub mygrep (&@) mygrep { /foo/ } $a,$b,$c
589 sub myrand ($) myrand 42
590 sub mytime () mytime
591
592Any backslashed prototype character must be passed something starting
593with that character. Any unbackslashed @ or % eats all the rest of the
594arguments, and forces list context. An argument represented by $
595forces scalar context. An & requires an anonymous subroutine, and *
596does whatever it has to do to turn the argument into a reference to a
597symbol table entry. A semicolon separates mandatory arguments from
598optional arguments.
599
600Note that the last three are syntactically distinguished by the lexer.
601mygrep() is parsed as a true list operator, myrand() is parsed as a
602true unary operator with unary precedence the same as rand(), and
603mytime() is truly argumentless, just like time(). That is, if you
604say
605
606 mytime +2;
607
608you'll get mytime() + 2, not mytime(2), which is how it would be parsed
609without the prototype.
610
611The interesting thing about & is that you can generate new syntax with it:
612
613 sub try (&$) {
614 my($try,$catch) = @_;
615 eval { &$try };
616 if ($@) {
617 local $_ = $@;
618 &$catch;
619 }
620 }
621 sub catch (&) { @_ }
622
623 try {
624 die "phooey";
625 } catch {
626 /phooey/ and print "unphooey\n";
627 };
628
629That prints "unphooey". (Yes, there are still unresolved
630issues having to do with the visibility of @_. I'm ignoring that
631question for the moment. (But note that if we make @_ lexically
632scoped, those anonymous subroutines can act like closures... (Gee,
633is this sounding a little Lispish? (Nevermind.))))
634
635And here's a reimplementation of grep:
636
637 sub mygrep (&@) {
638 my $code = shift;
639 my @result;
640 foreach $_ (@_) {
641 push(@result, $_) if &$ref;
642 }
643 @result;
644 }
a0d0e21e 645
cb1a09d0 646Some folks would prefer full alphanumeric prototypes. Alphanumerics have
647been intentionally left out of prototypes for the express purpose of
648someday in the future adding named, formal parameters. The current
649mechanism's main goal is to let module writers provide better diagnostics
650for module users. Larry feels the notation quite understandable to Perl
651programmers, and that it will not intrude greatly upon the meat of the
652module, nor make it harder to read. The line noise is visually
653encapsulated into a small pill that's easy to swallow.
654
655It's probably best to prototype new functions, not retrofit prototyping
656into older ones. That's because you must be especially careful about
657silent impositions of differing list versus scalar contexts. For example,
658if you decide that a function should take just one parameter, like this:
659
660 sub func ($) {
661 my $n = shift;
662 print "you gave me $n\n";
663 }
664
665and someone has been calling it with an array or expression
666returning a list:
667
668 func(@foo);
669 func( split /:/ );
670
671Then you've just supplied an automatic scalar() in front of their
672argument, which can be more than a bit surprising. The old @foo
673which used to hold one thing doesn't get passed in. Instead,
674the func() now gets passed in 1, that is, the number of elments
675in @foo. And the split() gets called in a scalar context and
676starts scribbling on your @_ parameter list.
677
678This is all very powerful, of course, and should only be used in moderation
679to make the world a better place.
680
681=head2 Overriding Builtin Functions
a0d0e21e 682
683Many builtin functions may be overridden, though this should only be
684tried occasionally and for good reason. Typically this might be
685done by a package attempting to emulate missing builtin functionality
686on a non-Unix system.
687
688Overriding may only be done by importing the name from a
689module--ordinary predeclaration isn't good enough. However, the
690C<subs> pragma (compiler directive) lets you, in effect, predeclare subs
691via the import syntax, and these names may then override the builtin ones:
692
693 use subs 'chdir', 'chroot', 'chmod', 'chown';
694 chdir $somewhere;
695 sub chdir { ... }
696
697Library modules should not in general export builtin names like "open"
698or "chdir" as part of their default @EXPORT list, since these may
699sneak into someone else's namespace and change the semantics unexpectedly.
700Instead, if the module adds the name to the @EXPORT_OK list, then it's
701possible for a user to import the name explicitly, but not implicitly.
702That is, they could say
703
704 use Module 'open';
705
706and it would import the open override, but if they said
707
708 use Module;
709
710they would get the default imports without the overrides.
711
712=head2 Autoloading
713
714If you call a subroutine that is undefined, you would ordinarily get an
715immediate fatal error complaining that the subroutine doesn't exist.
716(Likewise for subroutines being used as methods, when the method
717doesn't exist in any of the base classes of the class package.) If,
718however, there is an C<AUTOLOAD> subroutine defined in the package or
719packages that were searched for the original subroutine, then that
720C<AUTOLOAD> subroutine is called with the arguments that would have been
721passed to the original subroutine. The fully qualified name of the
722original subroutine magically appears in the $AUTOLOAD variable in the
723same package as the C<AUTOLOAD> routine. The name is not passed as an
724ordinary argument because, er, well, just because, that's why...
725
726Most C<AUTOLOAD> routines will load in a definition for the subroutine in
727question using eval, and then execute that subroutine using a special
728form of "goto" that erases the stack frame of the C<AUTOLOAD> routine
729without a trace. (See the standard C<AutoLoader> module, for example.)
730But an C<AUTOLOAD> routine can also just emulate the routine and never
cb1a09d0 731define it. For example, let's pretend that a function that wasn't defined
732should just call system() with those arguments. All you'd do is this:
733
734 sub AUTOLOAD {
735 my $program = $AUTOLOAD;
736 $program =~ s/.*:://;
737 system($program, @_);
738 }
739 date();
740 who('am', i');
741 ls('-l');
742
743In fact, if you preclare the functions you want to call that way, you don't
744even need the parentheses:
745
746 use subs qw(date who ls);
747 date;
748 who "am", "i";
749 ls -l;
750
751A more complete example of this is the standard Shell module, which
a0d0e21e 752can treat undefined subroutine calls as calls to Unix programs.
753
cb1a09d0 754Mechanisms are available for modules writers to help split the modules
755up into autoloadable files. See the standard AutoLoader module described
756in L<Autoloader>, the standard SelfLoader modules in L<SelfLoader>, and
757the document on adding C functions to perl code in L<perlxs>.
758
759=head1 SEE ALSO
a0d0e21e 760
cb1a09d0 761See L<perlref> for more on references. See L<perlxs> if you'd
762like to learn about calling C subroutines from perl. See
763L<perlmod> to learn about bundling up your functions in
764separate files.