This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[inseparable changes from match from perl-5.003_97g to perl-5.003_97h]
[perl5.git] / pod / perlfaq7.pod
1 =head1 NAME
2
3 perlfaq7 - Perl Language Issues ($Revision: 1.16 $, $Date: 1997/03/19 17:25:23 $)
4
5 =head1 DESCRIPTION
6
7 This section deals with general Perl language issues that don't
8 clearly fit into any of the other sections.
9
10 =head2 Can I get a BNF/yacc/RE for the Perl language?
11
12 No, in the words of Chaim Frenkel: "Perl's grammar can not be reduced
13 to BNF.  The work of parsing perl is distributed between yacc, the
14 lexer, smoke and mirrors."
15
16 =head2 What are all these $@%* punctuation signs, and how do I know when to use them?
17
18 They are type specifiers, as detailed in L<perldata>:
19
20     $ for scalar values (number, string or reference)
21     @ for arrays
22     % for hashes (associative arrays)
23     * for all types of that symbol name.  In version 4 you used them like
24       pointers, but in modern perls you can just use references.
25
26 While there are a few places where you don't actually need these type
27 specifiers, you should always use them.
28
29 A couple of others that you're likely to encounter that aren't
30 really type specifiers are:
31
32     <> are used for inputting a record from a filehandle.
33     \  takes a reference to something.
34
35 Note that E<lt>FILEE<gt> is I<neither> the type specifier for files
36 nor the name of the handle.  It is the C<E<lt>E<gt>> operator applied
37 to the handle FILE.  It reads one line (well, record - see
38 L<perlvar/$/>) from the handle FILE in scalar context, or I<all> lines
39 in list context.  When performing open, close, or any other operation
40 besides C<E<lt>E<gt>> on files, or even talking about the handle, do
41 I<not> use the brackets.  These are correct: C<eof(FH)>, C<seek(FH, 0,
42 2)> and "copying from STDIN to FILE".
43
44 =head2 Do I always/never have to quote my strings or use semicolons and commas?
45
46 Normally, a bareword doesn't need to be quoted, but in most cases
47 probably should be (and must be under C<use strict>).  But a hash key
48 consisting of a simple word (that isn't the name of a defined
49 subroutine) and the left-hand operand to the C<=E<gt>> operator both
50 count as though they were quoted:
51
52     This                    is like this
53     ------------            ---------------
54     $foo{line}              $foo{"line"}
55     bar => stuff            "bar" => stuff
56
57 The final semicolon in a block is optional, as is the final comma in a
58 list.  Good style (see L<perlstyle>) says to put them in except for
59 one-liners:
60
61     if ($whoops) { exit 1 }
62     @nums = (1, 2, 3);
63
64     if ($whoops) {
65         exit 1;
66     }
67     @lines = (
68         "There Beren came from mountains cold",
69         "And lost he wandered under leaves",
70     );
71
72 =head2 How do I skip some return values?
73
74 One way is to treat the return values as a list and index into it:
75
76         $dir = (getpwnam($user))[7];
77
78 Another way is to use undef as an element on the left-hand-side:
79
80     ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
81
82 =head2 How do I temporarily block warnings?
83
84 The C<$^W> variable (documented in L<perlvar>) controls
85 runtime warnings for a block:
86
87     {
88         local $^W = 0;        # temporarily turn off warnings
89         $a = $b + $c;         # I know these might be undef
90     }
91
92 Note that like all the punctuation variables, you cannot currently
93 use my() on C<$^W>, only local().
94
95 A new C<use warnings> pragma is in the works to provide finer control
96 over all this.  The curious should check the perl5-porters mailing list
97 archives for details.
98
99 =head2 What's an extension?
100
101 A way of calling compiled C code from Perl.  Reading L<perlxstut>
102 is a good place to learn more about extensions.
103
104 =head2 Why do Perl operators have different precedence than C operators?
105
106 Actually, they don't.  All C operators that Perl copies have the same
107 precedence in Perl as they do in C.  The problem is with operators that C
108 doesn't have, especially functions that give a list context to everything
109 on their right, eg print, chmod, exec, and so on.  Such functions are
110 called "list operators" and appear as such in the precedence table in
111 L<perlop>.
112
113 A common mistake is to write:
114
115     unlink $file || die "snafu";
116
117 This gets interpreted as:
118
119     unlink ($file || die "snafu");
120
121 To avoid this problem, either put in extra parentheses or use the
122 super low precedence C<or> operator:
123
124     (unlink $file) || die "snafu";
125     unlink $file or die "snafu";
126
127 The "English" operators (C<and>, C<or>, C<xor>, and C<not>)
128 deliberately have precedence lower than that of list operators for
129 just such situations as the one above.
130
131 Another operator with surprising precedence is exponentiation.  It
132 binds more tightly even than unary minus, making C<-2**2> product a
133 negative not a positive four.  It is also right-associating, meaning
134 that C<2**3**2> is two raised to the ninth power, not eight squared.
135
136 =head2 How do I declare/create a structure?
137
138 In general, you don't "declare" a structure.  Just use a (probably
139 anonymous) hash reference.  See L<perlref> and L<perldsc> for details.
140 Here's an example:
141
142     $person = {};                   # new anonymous hash
143     $person->{AGE}  = 24;           # set field AGE to 24
144     $person->{NAME} = "Nat";        # set field NAME to "Nat"
145
146 If you're looking for something a bit more rigorous, try L<perltoot>.
147
148 =head2 How do I create a module?
149
150 A module is a package that lives in a file of the same name.  For
151 example, the Hello::There module would live in Hello/There.pm.  For
152 details, read L<perlmod>.  You'll also find L<Exporter> helpful.  If
153 you're writing a C or mixed-language module with both C and Perl, then
154 you should study L<perlxstut>.
155
156 Here's a convenient template you might wish you use when starting your
157 own module.  Make sure to change the names appropriately.
158
159     package Some::Module;  # assumes Some/Module.pm
160
161     use strict;
162
163     BEGIN {
164         use Exporter   ();
165         use vars       qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
166
167         ## set the version for version checking; uncomment to use
168         ## $VERSION     = 1.00;
169
170         # if using RCS/CVS, this next line may be preferred,
171         # but beware two-digit versions.
172         $VERSION = do{my@r=q$Revision: 1.16 $=~/\d+/g;sprintf '%d.'.'%02d'x$#r,@r};
173
174         @ISA         = qw(Exporter);
175         @EXPORT      = qw(&func1 &func2 &func3);
176         %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
177
178         # your exported package globals go here,
179         # as well as any optionally exported functions
180         @EXPORT_OK   = qw($Var1 %Hashit);
181     }
182     use vars      @EXPORT_OK;
183
184     # non-exported package globals go here
185     use vars      qw( @more $stuff );
186
187     # initialize package globals, first exported ones
188     $Var1   = '';
189     %Hashit = ();
190
191     # then the others (which are still accessible as $Some::Module::stuff)
192     $stuff  = '';
193     @more   = ();
194
195     # all file-scoped lexicals must be created before
196     # the functions below that use them.
197
198     # file-private lexicals go here
199     my $priv_var    = '';
200     my %secret_hash = ();
201
202     # here's a file-private function as a closure,
203     # callable as &$priv_func;  it cannot be prototyped.
204     my $priv_func = sub {
205         # stuff goes here.
206     };
207
208     # make all your functions, whether exported or not;
209     # remember to put something interesting in the {} stubs
210     sub func1      {}    # no prototype
211     sub func2()    {}    # proto'd void
212     sub func3($$)  {}    # proto'd to 2 scalars
213
214     # this one isn't exported, but could be called!
215     sub func4(\%)  {}    # proto'd to 1 hash ref
216
217     END { }       # module clean-up code here (global destructor)
218
219     1;            # modules must return true
220
221 =head2 How do I create a class?
222
223 See L<perltoot> for an introduction to classes and objects, as well as
224 L<perlobj> and L<perlbot>.
225
226 =head2 How can I tell if a variable is tainted?
227
228 See L<perlsec/"Laundering and Detecting Tainted Data">.  Here's an
229 example (which doesn't use any system calls, because the kill()
230 is given no processes to signal):
231
232     sub is_tainted {
233         return ! eval { join('',@_), kill 0; 1; };
234     }
235
236 This is not C<-w> clean, however.  There is no C<-w> clean way to
237 detect taintedness - take this as a hint that you should untaint
238 all possibly-tainted data.
239
240 =head2 What's a closure?
241
242 Closures are documented in L<perlref>.
243
244 I<Closure> is a computer science term with a precise but
245 hard-to-explain meaning. Closures are implemented in Perl as anonymous
246 subroutines with lasting references to lexical variables outside their
247 own scopes.  These lexicals magically refer to the variables that were
248 around when the subroutine was defined (deep binding).
249
250 Closures make sense in any programming language where you can have the
251 return value of a function be itself a function, as you can in Perl.
252 Note that some languages provide anonymous functions but are not
253 capable of providing proper closures; the Python language, for
254 example.  For more information on closures, check out any textbook on
255 functional programming.  Scheme is a language that not only supports
256 but encourages closures.
257
258 Here's a classic function-generating function:
259
260     sub add_function_generator {
261       return sub { shift + shift };
262     }
263
264     $add_sub = add_function_generator();
265     $sum = &$add_sub(4,5);                # $sum is 9 now.
266
267 The closure works as a I<function template> with some customization
268 slots left out to be filled later.  The anonymous subroutine returned
269 by add_function_generator() isn't technically a closure because it
270 refers to no lexicals outside its own scope.
271
272 Contrast this with the following make_adder() function, in which the
273 returned anonymous function contains a reference to a lexical variable
274 outside the scope of that function itself.  Such a reference requires
275 that Perl return a proper closure, thus locking in for all time the
276 value that the lexical had when the function was created.
277
278     sub make_adder {
279         my $addpiece = shift;
280         return sub { shift + $addpiece };
281     }
282
283     $f1 = make_adder(20);
284     $f2 = make_adder(555);
285
286 Now C<&$f1($n)> is always 20 plus whatever $n you pass in, whereas
287 C<&$f2($n)> is always 555 plus whatever $n you pass in.  The $addpiece
288 in the closure sticks around.
289
290 Closures are often used for less esoteric purposes.  For example, when
291 you want to pass in a bit of code into a function:
292
293     my $line;
294     timeout( 30, sub { $line = <STDIN> } );
295
296 If the code to execute had been passed in as a string, C<'$line =
297 E<lt>STDINE<gt>'>, there would have been no way for the hypothetical
298 timeout() function to access the lexical variable $line back in its
299 caller's scope.
300
301 =head2 What is variable suicide and how can I prevent it?
302
303 Variable suicide is when you (temporarily or permanently) lose the
304 value of a variable.  It is caused by scoping through my() and local()
305 interacting with either closures or aliased foreach() interator
306 variables and subroutine arguments.  It used to be easy to
307 inadvertently lose a variable's value this way, but now it's much
308 harder.  Take this code:
309
310     my $f = "foo";
311     sub T {
312       while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" }
313     }
314     T;
315     print "Finally $f\n";
316
317 The $f that has "bar" added to it three times should be a new C<$f>
318 (C<my $f> should create a new local variable each time through the
319 loop).  It isn't, however.  This is a bug, and will be fixed.
320
321 =head2 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regexp}?
322
323 With the exception of regexps, you need to pass references to these
324 objects.  See L<perlsub/"Pass by Reference"> for this particular
325 question, and L<perlref> for information on references.
326
327 =over 4
328
329 =item Passing Variables and Functions
330
331 Regular variables and functions are quite easy: just pass in a
332 reference to an existing or anonymous variable or function:
333
334     func( \$some_scalar );
335
336     func( \$some_array );
337     func( [ 1 .. 10 ]   );
338
339     func( \%some_hash   );
340     func( { this => 10, that => 20 }   );
341
342     func( \&some_func   );
343     func( sub { $_[0] ** $_[1] }   );
344
345 =item Passing Filehandles
346
347 To create filehandles you can pass to subroutines, you can use C<*FH>
348 or C<\*FH> notation ("typeglobs" - see L<perldata> for more information),
349 or create filehandles dynamically using the old FileHandle or the new
350 IO::File modules, both part of the standard Perl distribution.
351
352     use Fcntl;
353     use IO::File;
354     my $fh = new IO::File $filename, O_WRONLY|O_APPEND;
355                 or die "Can't append to $filename: $!";
356     func($fh);
357
358 =item Passing Regexps
359
360 To pass regexps around, you'll need to either use one of the highly
361 experimental regular expression modules from CPAN (Nick Ing-Simmons's
362 Regexp or Ilya Zakharevich's Devel::Regexp), pass around strings
363 and use an exception-trapping eval, or else be be very, very clever.
364 Here's an example of how to pass in a string to be regexp compared:
365
366     sub compare($$) {
367         my ($val1, $regexp) = @_;
368         my $retval = eval { $val =~ /$regexp/ };
369         die if $@;
370         return $retval;
371     }
372
373     $match = compare("old McDonald", q/d.*D/);
374
375 Make sure you never say something like this:
376
377     return eval "\$val =~ /$regexp/";   # WRONG
378
379 or someone can sneak shell escapes into the regexp due to the double
380 interpolation of the eval and the double-quoted string.  For example:
381
382     $pattern_of_evil = 'danger ${ system("rm -rf * &") } danger';
383
384     eval "\$string =~ /$pattern_of_evil/";
385
386 Those preferring to be very, very clever might see the O'Reilly book,
387 I<Mastering Regular Expressions>, by Jeffrey Friedl.  Page 273's
388 Build_MatchMany_Function() is particularly interesting.  A complete
389 citation of this book is given in L<perlfaq2>.
390
391 =item Passing Methods
392
393 To pass an object method into a subroutine, you can do this:
394
395     call_a_lot(10, $some_obj, "methname")
396     sub call_a_lot {
397         my ($count, $widget, $trick) = @_;
398         for (my $i = 0; $i < $count; $i++) {
399             $widget->$trick();
400         }
401     }
402
403 or you can use a closure to bundle up the object and its method call
404 and arguments:
405
406     my $whatnot =  sub { $some_obj->obfuscate(@args) };
407     func($whatnot);
408     sub func {
409         my $code = shift;
410         &$code();
411     }
412
413 You could also investigate the can() method in the UNIVERSAL class
414 (part of the standard perl distribution).
415
416 =back
417
418 =head2 How do I create a static variable?
419
420 As with most things in Perl, TMTOWTDI.  What is a "static variable" in
421 other languages could be either a function-private variable (visible
422 only within a single function, retaining its value between calls to
423 that function), or a file-private variable (visible only to functions
424 within the file it was declared in) in Perl.
425
426 Here's code to implement a function-private variable:
427
428     BEGIN {
429         my $counter = 42;
430         sub prev_counter { return --$counter }
431         sub next_counter { return $counter++ }
432     }
433
434 Now prev_counter() and next_counter() share a private variable $counter
435 that was initialized at compile time.
436
437 To declare a file-private variable, you'll still use a my(), putting
438 it at the outer scope level at the top of the file.  Assume this is in
439 file Pax.pm:
440
441     package Pax;
442     my $started = scalar(localtime(time()));
443
444     sub begun { return $started }
445
446 When C<use Pax> or C<require Pax> loads this module, the variable will
447 be initialized.  It won't get garbage-collected the way most variables
448 going out of scope do, because the begun() function cares about it,
449 but no one else can get it.  It is not called $Pax::started because
450 its scope is unrelated to the package.  It's scoped to the file.  You
451 could conceivably have several packages in that same file all
452 accessing the same private variable, but another file with the same
453 package couldn't get to it.
454
455 =head2 What's the difference between dynamic and lexical (static) scoping?  Between local() and my()?
456
457 C<local($x)> saves away the old value of the global variable C<$x>,
458 and assigns a new value for the duration of the subroutine, I<which is
459 visible in other functions called from that subroutine>.  This is done
460 at run-time, so is called dynamic scoping.  local() always affects global
461 variables, also called package variables or dynamic variables.
462
463 C<my($x)> creates a new variable that is only visible in the current
464 subroutine.  This is done at compile-time, so is called lexical or
465 static scoping.  my() always affects private variables, also called
466 lexical variables or (improperly) static(ly scoped) variables.
467
468 For instance:
469
470     sub visible {
471         print "var has value $var\n";
472     }
473
474     sub dynamic {
475         local $var = 'local';   # new temporary value for the still-global
476         visible();              #   variable called $var
477     }
478
479     sub lexical {
480         my $var = 'private';    # new private variable, $var
481         visible();              # (invisible outside of sub scope)
482     }
483
484     $var = 'global';
485
486     visible();                  # prints global
487     dynamic();                  # prints local
488     lexical();                  # prints global
489
490 Notice how at no point does the value "private" get printed.  That's
491 because $var only has that value within the block of the lexical()
492 function, and it is hidden from called subroutine.
493
494 In summary, local() doesn't make what you think of as private, local
495 variables.  It gives a global variable a temporary value.  my() is
496 what you're looking for if you want private variables.
497
498 See also L<perlsub>, which explains this all in more detail.
499
500 =head2 How can I access a dynamic variable while a similarly named lexical is in scope?
501
502 You can do this via symbolic references, provided you haven't set
503 C<use strict "refs">.  So instead of $var, use C<${'var'}>.
504
505     local $var = "global";
506     my    $var = "lexical";
507
508     print "lexical is $var\n";
509
510     no strict 'refs';
511     print "global  is ${'var'}\n";
512
513 If you know your package, you can just mention it explicitly, as in
514 $Some_Pack::var.  Note that the notation $::var is I<not> the dynamic
515 $var in the current package, but rather the one in the C<main>
516 package, as though you had written $main::var.  Specifying the package
517 directly makes you hard-code its name, but it executes faster and
518 avoids running afoul of C<use strict "refs">.
519
520 =head2 What's the difference between deep and shallow binding?
521
522 In deep binding, lexical variables mentioned in anonymous subroutines
523 are the same ones that were in scope when the subroutine was created.
524 In shallow binding, they are whichever variables with the same names
525 happen to be in scope when the subroutine is called.  Perl always uses
526 deep binding of lexical variables (i.e., those created with my()).
527 However, dynamic variables (aka global, local, or package variables)
528 are effectively shallowly bound.  Consider this just one more reason
529 not to use them.  See the answer to L<"What's a closure?">.
530
531 =head2 Why doesn't "local($foo) = <FILE>;" work right?
532
533 C<local()> gives list context to the right hand side of C<=>.  The
534 E<lt>FHE<gt> read operation, like so many of Perl's functions and
535 operators, can tell which context it was called in and behaves
536 appropriately.  In general, the scalar() function can help.  This
537 function does nothing to the data itself (contrary to popular myth)
538 but rather tells its argument to behave in whatever its scalar fashion
539 is.  If that function doesn't have a defined scalar behavior, this of
540 course doesn't help you (such as with sort()).
541
542 To enforce scalar context in this particular case, however, you need
543 merely omit the parentheses:
544
545     local($foo) = <FILE>;           # WRONG
546     local($foo) = scalar(<FILE>);   # ok
547     local $foo  = <FILE>;           # right
548
549 You should probably be using lexical variables anyway, although the
550 issue is the same here:
551
552     my($foo) = <FILE>;  # WRONG
553     my $foo  = <FILE>;  # right
554
555 =head2 How do I redefine a builtin function, operator, or method?
556
557 Why do you want to do that? :-)
558
559 If you want to override a predefined function, such as open(),
560 then you'll have to import the new definition from a different
561 module.  See L<perlsub/"Overriding Builtin Functions">.  There's
562 also an example in L<perltoot/"Class::Template">.
563
564 If you want to overload a Perl operator, such as C<+> or C<**>,
565 then you'll want to use the C<use overload> pragma, documented
566 in L<overload>.
567
568 If you're talking about obscuring method calls in parent classes,
569 see L<perltoot/"Overridden Methods">.
570
571 =head2 What's the difference between calling a function as &foo and foo()?
572
573 When you call a function as C<&foo>, you allow that function access to
574 your current @_ values, and you by-pass prototypes.  That means that
575 the function doesn't get an empty @_, it gets yours!  While not
576 strictly speaking a bug (it's documented that way in L<perlsub>), it
577 would be hard to consider this a feature in most cases.
578
579 When you call your function as C<&foo()>, then you do get a new @_,
580 but prototyping is still circumvented.
581
582 Normally, you want to call a function using C<foo()>.  You may only
583 omit the parentheses if the function is already known to the compiler
584 because it already saw the definition (C<use> but not C<require>),
585 or via a forward reference or C<use subs> declaration.  Even in this
586 case, you get a clean @_ without any of the old values leaking through
587 where they don't belong.
588
589 =head2 How do I create a switch or case statement?
590
591 This is explained in more depth in the L<perlsyn>.  Briefly, there's
592 no official case statement, because of the variety of tests possible
593 in Perl (numeric comparison, string comparison, glob comparison,
594 regexp matching, overloaded comparisons, ...).  Larry couldn't decide
595 how best to do this, so he left it out, even though it's been on the
596 wish list since perl1.
597
598 Here's a simple example of a switch based on pattern matching.  We'll
599 do a multi-way conditional based on the type of reference stored in
600 $whatchamacallit:
601
602     SWITCH:
603       for (ref $whatchamacallit) {
604
605         /^$/            && die "not a reference";
606
607         /SCALAR/        && do {
608                                 print_scalar($$ref);
609                                 last SWITCH;
610                         };
611
612         /ARRAY/         && do {
613                                 print_array(@$ref);
614                                 last SWITCH;
615                         };
616
617         /HASH/          && do {
618                                 print_hash(%$ref);
619                                 last SWITCH;
620                         };
621
622         /CODE/          && do {
623                                 warn "can't print function ref";
624                                 last SWITCH;
625                         };
626
627         # DEFAULT
628
629         warn "User defined type skipped";
630
631     }
632
633 =head2 How can I catch accesses to undefined variables/functions/methods?
634
635 The AUTOLOAD method, discussed in L<perlsub/"Autoloading"> and
636 L<perltoot/"AUTOLOAD: Proxy Methods">, lets you capture calls to
637 undefined functions and methods.
638
639 When it comes to undefined variables that would trigger a warning
640 under C<-w>, you can use a handler to trap the pseudo-signal
641 C<__WARN__> like this:
642
643     $SIG{__WARN__} = sub {
644
645         for ( $_[0] ) {
646
647             /Use of uninitialized value/  && do {
648                 # promote warning to a fatal
649                 die $_;
650             };
651
652             # other warning cases to catch could go here;
653
654             warn $_;
655         }
656
657     };
658
659 =head2 Why can't a method included in this same file be found?
660
661 Some possible reasons: your inheritance is getting confused, you've
662 misspelled the method name, or the object is of the wrong type.  Check
663 out L<perltoot> for details on these.  You may also use C<print
664 ref($object)> to find out the class C<$object> was blessed into.
665
666 Another possible reason for problems is because you've used the
667 indirect object syntax (eg, C<find Guru "Samy">) on a class name
668 before Perl has seen that such a package exists.  It's wisest to make
669 sure your packages are all defined before you start using them, which
670 will be taken care of if you use the C<use> statement instead of
671 C<require>.  If not, make sure to use arrow notation (eg,
672 C<Guru->find("Samy")>) instead.  Object notation is explained in
673 L<perlobj>.
674
675 =head2 How can I find out my current package?
676
677 If you're just a random program, you can do this to find
678 out what the currently compiled package is:
679
680     my $packname = ref bless [];
681
682 But if you're a method and you want to print an error message
683 that includes the kind of object you were called on (which is
684 not necessarily the same as the one in which you were compiled):
685
686     sub amethod {
687         my $self = shift;
688         my $class = ref($self) || $self;
689         warn "called me from a $class object";
690     }
691
692 =head2 How can I comment out a large block of perl code?
693
694 Use embedded POD to discard it:
695
696     # program is here
697
698     =for nobody
699     This paragraph is commented out
700
701     # program continues
702
703     =begin comment text
704
705     all of this stuff
706
707     here will be ignored
708     by everyone
709
710     =end comment text
711
712 =head1 AUTHOR AND COPYRIGHT
713
714 Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
715 All rights reserved.  See L<perlfaq> for distribution information.