This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Unused 'cv'
[perl5.git] / pod / perlfaq7.pod
CommitLineData
68dc0745 1=head1 NAME
2
c195e131 3perlfaq7 - General Perl Language Issues ($Revision: 10100 $)
68dc0745 4
5=head1 DESCRIPTION
6
7This section deals with general Perl language issues that don't
8clearly fit into any of the other sections.
9
10=head2 Can I get a BNF/yacc/RE for the Perl language?
11
c8db1d39
TC
12There is no BNF, but you can paw your way through the yacc grammar in
13perly.y in the source distribution if you're particularly brave. The
14grammar relies on very smart tokenizing code, so be prepared to
15venture into toke.c as well.
16
17In the words of Chaim Frenkel: "Perl's grammar can not be reduced to BNF.
18The work of parsing perl is distributed between yacc, the lexer, smoke
19and mirrors."
68dc0745 20
d92eb7b0 21=head2 What are all these $@%&* punctuation signs, and how do I know when to use them?
68dc0745 22
23They are type specifiers, as detailed in L<perldata>:
24
ac003c96
RGS
25 $ for scalar values (number, string or reference)
26 @ for arrays
27 % for hashes (associative arrays)
28 & for subroutines (aka functions, procedures, methods)
29 * for all types of that symbol name. In version 4 you used them like
30 pointers, but in modern perls you can just use references.
68dc0745 31
a6dd486b
JB
32There are couple of other symbols that you're likely to encounter that aren't
33really type specifiers:
68dc0745 34
ac003c96
RGS
35 <> are used for inputting a record from a filehandle.
36 \ takes a reference to something.
68dc0745 37
c47ff5f1
GS
38Note that <FILE> is I<neither> the type specifier for files
39nor the name of the handle. It is the C<< <> >> operator applied
a6dd486b 40to the handle FILE. It reads one line (well, record--see
197aec24 41L<perlvar/$E<sol>>) from the handle FILE in scalar context, or I<all> lines
68dc0745 42in list context. When performing open, close, or any other operation
a6dd486b 43besides C<< <> >> on files, or even when talking about the handle, do
68dc0745 44I<not> use the brackets. These are correct: C<eof(FH)>, C<seek(FH, 0,
452)> and "copying from STDIN to FILE".
46
47=head2 Do I always/never have to quote my strings or use semicolons and commas?
48
49Normally, a bareword doesn't need to be quoted, but in most cases
50probably should be (and must be under C<use strict>). But a hash key
51consisting of a simple word (that isn't the name of a defined
c47ff5f1 52subroutine) and the left-hand operand to the C<< => >> operator both
68dc0745 53count as though they were quoted:
54
ac003c96
RGS
55 This is like this
56 ------------ ---------------
57 $foo{line} $foo{'line'}
58 bar => stuff 'bar' => stuff
68dc0745 59
60The final semicolon in a block is optional, as is the final comma in a
61list. Good style (see L<perlstyle>) says to put them in except for
62one-liners:
63
ac003c96
RGS
64 if ($whoops) { exit 1 }
65 @nums = (1, 2, 3);
66
67 if ($whoops) {
68 exit 1;
69 }
68dc0745 70
ac003c96 71 @lines = (
68dc0745 72 "There Beren came from mountains cold",
73 "And lost he wandered under leaves",
ac003c96 74 );
68dc0745 75
76=head2 How do I skip some return values?
77
78One way is to treat the return values as a list and index into it:
79
ac003c96 80 $dir = (getpwnam($user))[7];
68dc0745 81
82Another way is to use undef as an element on the left-hand-side:
83
ac003c96 84 ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
197aec24 85
49d635f9
RGS
86You can also use a list slice to select only the elements that
87you need:
88
89 ($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5];
68dc0745 90
91=head2 How do I temporarily block warnings?
92
9f1b1f2d
GS
93If you are running Perl 5.6.0 or better, the C<use warnings> pragma
94allows fine control of what warning are produced.
95See L<perllexwarn> for more details.
96
ac003c96 97 {
9f1b1f2d
GS
98 no warnings; # temporarily turn off warnings
99 $a = $b + $c; # I know these might be undef
ac003c96 100 }
6670e5e7 101
28b41a80
RGS
102Additionally, you can enable and disable categories of warnings.
103You turn off the categories you want to ignore and you can still
104get other categories of warnings. See L<perllexwarn> for the
105complete details, including the category names and hierarchy.
106
107 {
108 no warnings 'uninitialized';
109 $a = $b + $c;
110 }
9f1b1f2d
GS
111
112If you have an older version of Perl, the C<$^W> variable (documented
113in L<perlvar>) controls runtime warnings for a block:
68dc0745 114
ac003c96 115 {
68dc0745 116 local $^W = 0; # temporarily turn off warnings
117 $a = $b + $c; # I know these might be undef
ac003c96 118 }
68dc0745 119
120Note that like all the punctuation variables, you cannot currently
121use my() on C<$^W>, only local().
122
68dc0745 123=head2 What's an extension?
124
a6dd486b
JB
125An extension is a way of calling compiled C code from Perl. Reading
126L<perlxstut> is a good place to learn more about extensions.
68dc0745 127
128=head2 Why do Perl operators have different precedence than C operators?
129
130Actually, they don't. All C operators that Perl copies have the same
131precedence in Perl as they do in C. The problem is with operators that C
132doesn't have, especially functions that give a list context to everything
a6dd486b 133on their right, eg. print, chmod, exec, and so on. Such functions are
68dc0745 134called "list operators" and appear as such in the precedence table in
135L<perlop>.
136
137A common mistake is to write:
138
ac003c96 139 unlink $file || die "snafu";
68dc0745 140
141This gets interpreted as:
142
ac003c96 143 unlink ($file || die "snafu");
68dc0745 144
145To avoid this problem, either put in extra parentheses or use the
146super low precedence C<or> operator:
147
ac003c96
RGS
148 (unlink $file) || die "snafu";
149 unlink $file or die "snafu";
68dc0745 150
151The "English" operators (C<and>, C<or>, C<xor>, and C<not>)
152deliberately have precedence lower than that of list operators for
153just such situations as the one above.
154
155Another operator with surprising precedence is exponentiation. It
156binds more tightly even than unary minus, making C<-2**2> product a
157negative not a positive four. It is also right-associating, meaning
158that C<2**3**2> is two raised to the ninth power, not eight squared.
159
c8db1d39
TC
160Although it has the same precedence as in C, Perl's C<?:> operator
161produces an lvalue. This assigns $x to either $a or $b, depending
162on the trueness of $maybe:
163
ac003c96 164 ($maybe ? $a : $b) = $x;
c8db1d39 165
68dc0745 166=head2 How do I declare/create a structure?
167
168In general, you don't "declare" a structure. Just use a (probably
169anonymous) hash reference. See L<perlref> and L<perldsc> for details.
170Here's an example:
171
ac003c96
RGS
172 $person = {}; # new anonymous hash
173 $person->{AGE} = 24; # set field AGE to 24
174 $person->{NAME} = "Nat"; # set field NAME to "Nat"
175
68dc0745 176If you're looking for something a bit more rigorous, try L<perltoot>.
177
178=head2 How do I create a module?
179
7678cced 180(contributed by brian d foy)
68dc0745 181
7678cced 182L<perlmod>, L<perlmodlib>, L<perlmodstyle> explain modules
cf525c36 183in all the gory details. L<perlnewmod> gives a brief
7678cced
RGS
184overview of the process along with a couple of suggestions
185about style.
65acb1b1 186
7678cced
RGS
187If you need to include C code or C library interfaces in
188your module, you'll need h2xs. h2xs will create the module
189distribution structure and the initial interface files
190you'll need. L<perlxs> and L<perlxstut> explain the details.
7207e29d 191
7678cced
RGS
192If you don't need to use C code, other tools such as
193ExtUtils::ModuleMaker and Module::Starter, can help you
194create a skeleton module distribution.
195
196You may also want to see Sam Tregar's "Writing Perl Modules
197for CPAN" ( http://apress.com/book/bookDisplay.html?bID=14 )
198which is the best hands-on guide to creating module
199distributions.
65acb1b1 200
c195e131
RGS
201=head2 How do I adopt or take over a module already on CPAN?
202
203(contributed by brian d foy)
204
205The easiest way to take over a module is to have the current
206module maintainer either make you a co-maintainer or transfer
207the module to you.
208
209If you can't reach the author for some reason (e.g. email bounces),
210the PAUSE admins at modules@perl.org can help. The PAUSE admins
211treat each case individually.
212
213=over 4
214
215=item
216
217Get a login for the Perl Authors Upload Server (PAUSE) if you don't
218already have one: http://pause.perl.org
219
220=item
221
222Write to modules@perl.org explaining what you did to contact the
223current maintainer. The PAUSE admins will also try to reach the
224maintainer.
225
226=item
227
228Post a public message in a heavily trafficked site announcing your
229intention to take over the module.
230
231=item
232
233Wait a bit. The PAUSE admins don't want to act too quickly in case
234the current maintainer is on holiday. If there's no response to
235private communication or the public post, a PAUSE admin can transfer
236it to you.
237
238=back
239
68dc0745 240=head2 How do I create a class?
241
242See L<perltoot> for an introduction to classes and objects, as well as
243L<perlobj> and L<perlbot>.
244
245=head2 How can I tell if a variable is tainted?
246
213329dd
JH
247You can use the tainted() function of the Scalar::Util module, available
248from CPAN (or included with Perl since release 5.8.0).
249See also L<perlsec/"Laundering and Detecting Tainted Data">.
68dc0745 250
251=head2 What's a closure?
252
253Closures are documented in L<perlref>.
254
255I<Closure> is a computer science term with a precise but
322be77c
RGS
256hard-to-explain meaning. Usually, closures are implemented in Perl as
257anonymous subroutines with lasting references to lexical variables
258outside their own scopes. These lexicals magically refer to the
259variables that were around when the subroutine was defined (deep
260binding).
261
262Closures are most often used in programming languages where you can
263have the return value of a function be itself a function, as you can
264in Perl. Note that some languages provide anonymous functions but are
265not capable of providing proper closures: the Python language, for
68dc0745 266example. For more information on closures, check out any textbook on
267functional programming. Scheme is a language that not only supports
268but encourages closures.
269
322be77c 270Here's a classic non-closure function-generating function:
68dc0745 271
ac003c96
RGS
272 sub add_function_generator {
273 return sub { shift() + shift() };
274 }
68dc0745 275
ac003c96
RGS
276 $add_sub = add_function_generator();
277 $sum = $add_sub->(4,5); # $sum is 9 now.
68dc0745 278
322be77c
RGS
279The anonymous subroutine returned by add_function_generator() isn't
280technically a closure because it refers to no lexicals outside its own
281scope. Using a closure gives you a I<function template> with some
282customization slots left out to be filled later.
68dc0745 283
284Contrast this with the following make_adder() function, in which the
285returned anonymous function contains a reference to a lexical variable
286outside the scope of that function itself. Such a reference requires
287that Perl return a proper closure, thus locking in for all time the
288value that the lexical had when the function was created.
289
ac003c96
RGS
290 sub make_adder {
291 my $addpiece = shift;
292 return sub { shift() + $addpiece };
293 }
294
295 $f1 = make_adder(20);
296 $f2 = make_adder(555);
68dc0745 297
298Now C<&$f1($n)> is always 20 plus whatever $n you pass in, whereas
299C<&$f2($n)> is always 555 plus whatever $n you pass in. The $addpiece
300in the closure sticks around.
301
302Closures are often used for less esoteric purposes. For example, when
303you want to pass in a bit of code into a function:
304
ac003c96
RGS
305 my $line;
306 timeout( 30, sub { $line = <STDIN> } );
68dc0745 307
c47ff5f1
GS
308If the code to execute had been passed in as a string,
309C<< '$line = <STDIN>' >>, there would have been no way for the
310hypothetical timeout() function to access the lexical variable
311$line back in its caller's scope.
68dc0745 312
322be77c
RGS
313Another use for a closure is to make a variable I<private> to a
314named subroutine, e.g. a counter that gets initialized at creation
315time of the sub and can only be modified from within the sub.
316This is sometimes used with a BEGIN block in package files to make
317sure a variable doesn't get meddled with during the lifetime of the
318package:
319
ac003c96
RGS
320 BEGIN {
321 my $id = 0;
322 sub next_id { ++$id }
323 }
322be77c
RGS
324
325This is discussed in more detail in L<perlsub>, see the entry on
326I<Persistent Private Variables>.
327
46fc3d4c 328=head2 What is variable suicide and how can I prevent it?
329
9e72e4c6
RGS
330This problem was fixed in perl 5.004_05, so preventing it means upgrading
331your version of perl. ;)
46fc3d4c 332
9e72e4c6
RGS
333Variable suicide is when you (temporarily or permanently) lose the value
334of a variable. It is caused by scoping through my() and local()
335interacting with either closures or aliased foreach() iterator variables
336and subroutine arguments. It used to be easy to inadvertently lose a
337variable's value this way, but now it's much harder. Take this code:
338
ac003c96
RGS
339 my $f = 'foo';
340 sub T {
341 while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" }
342 }
343
344 T;
345 print "Finally $f\n";
46fc3d4c 346
9e72e4c6
RGS
347If you are experiencing variable suicide, that C<my $f> in the subroutine
348doesn't pick up a fresh copy of the C<$f> whose value is <foo>. The output
349shows that inside the subroutine the value of C<$f> leaks through when it
350shouldn't, as in this output:
351
352 foobar
353 foobarbar
354 foobarbarbar
355 Finally foo
356
46fc3d4c 357The $f that has "bar" added to it three times should be a new C<$f>
9e72e4c6
RGS
358C<my $f> should create a new lexical variable each time through the loop.
359The expected output is:
360
361 foobar
362 foobar
363 foobar
364 Finally foo
46fc3d4c 365
d92eb7b0 366=head2 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?
68dc0745 367
d92eb7b0 368With the exception of regexes, you need to pass references to these
68dc0745 369objects. See L<perlsub/"Pass by Reference"> for this particular
370question, and L<perlref> for information on references.
371
ac9dac7f
RGS
372See "Passing Regexes", later in L<perlfaq7>, for information on
373passing regular expressions.
a6dd486b 374
68dc0745 375=over 4
376
377=item Passing Variables and Functions
378
a6dd486b 379Regular variables and functions are quite easy to pass: just pass in a
68dc0745 380reference to an existing or anonymous variable or function:
381
ac003c96 382 func( \$some_scalar );
68dc0745 383
ac003c96
RGS
384 func( \@some_array );
385 func( [ 1 .. 10 ] );
68dc0745 386
ac003c96
RGS
387 func( \%some_hash );
388 func( { this => 10, that => 20 } );
68dc0745 389
ac003c96
RGS
390 func( \&some_func );
391 func( sub { $_[0] ** $_[1] } );
68dc0745 392
393=item Passing Filehandles
394
49d635f9
RGS
395As of Perl 5.6, you can represent filehandles with scalar variables
396which you treat as any other scalar.
397
398 open my $fh, $filename or die "Cannot open $filename! $!";
399 func( $fh );
197aec24 400
49d635f9
RGS
401 sub func {
402 my $passed_fh = shift;
197aec24 403
ac003c96 404 my $line = <$passed_fh>;
49d635f9 405 }
197aec24 406
49d635f9 407Before Perl 5.6, you had to use the C<*FH> or C<\*FH> notations.
a6dd486b 408These are "typeglobs"--see L<perldata/"Typeglobs and Filehandles">
c8db1d39
TC
409and especially L<perlsub/"Pass by Reference"> for more information.
410
d92eb7b0
GS
411=item Passing Regexes
412
413To pass regexes around, you'll need to be using a release of Perl
414sufficiently recent as to support the C<qr//> construct, pass around
415strings and use an exception-trapping eval, or else be very, very clever.
68dc0745 416
d92eb7b0
GS
417Here's an example of how to pass in a string to be regex compared
418using C<qr//>:
68dc0745 419
ac003c96
RGS
420 sub compare($$) {
421 my ($val1, $regex) = @_;
422 my $retval = $val1 =~ /$regex/;
d92eb7b0 423 return $retval;
ac003c96
RGS
424 }
425 $match = compare("old McDonald", qr/d.*D/i);
d92eb7b0
GS
426
427Notice how C<qr//> allows flags at the end. That pattern was compiled
428at compile time, although it was executed later. The nifty C<qr//>
429notation wasn't introduced until the 5.005 release. Before that, you
430had to approach this problem much less intuitively. For example, here
431it is again if you don't have C<qr//>:
432
ac003c96
RGS
433 sub compare($$) {
434 my ($val1, $regex) = @_;
435 my $retval = eval { $val1 =~ /$regex/ };
68dc0745 436 die if $@;
437 return $retval;
ac003c96 438 }
68dc0745 439
ac003c96 440 $match = compare("old McDonald", q/($?i)d.*D/);
68dc0745 441
442Make sure you never say something like this:
443
ac003c96 444 return eval "\$val =~ /$regex/"; # WRONG
68dc0745 445
d92eb7b0 446or someone can sneak shell escapes into the regex due to the double
68dc0745 447interpolation of the eval and the double-quoted string. For example:
448
ac003c96 449 $pattern_of_evil = 'danger ${ system("rm -rf * &") } danger';
68dc0745 450
ac003c96 451 eval "\$string =~ /$pattern_of_evil/";
68dc0745 452
453Those preferring to be very, very clever might see the O'Reilly book,
454I<Mastering Regular Expressions>, by Jeffrey Friedl. Page 273's
455Build_MatchMany_Function() is particularly interesting. A complete
456citation of this book is given in L<perlfaq2>.
457
458=item Passing Methods
459
460To pass an object method into a subroutine, you can do this:
461
ac003c96
RGS
462 call_a_lot(10, $some_obj, "methname")
463 sub call_a_lot {
464 my ($count, $widget, $trick) = @_;
465 for (my $i = 0; $i < $count; $i++) {
466 $widget->$trick();
467 }
468 }
68dc0745 469
a6dd486b
JB
470Or, you can use a closure to bundle up the object, its
471method call, and arguments:
68dc0745 472
ac003c96
RGS
473 my $whatnot = sub { $some_obj->obfuscate(@args) };
474 func($whatnot);
475 sub func {
476 my $code = shift;
477 &$code();
478 }
68dc0745 479
480You could also investigate the can() method in the UNIVERSAL class
481(part of the standard perl distribution).
482
483=back
484
485=head2 How do I create a static variable?
486
6670e5e7 487(contributed by brian d foy)
68dc0745 488
6670e5e7
RGS
489Perl doesn't have "static" variables, which can only be accessed from
490the function in which they are declared. You can get the same effect
491with lexical variables, though.
492
493You can fake a static variable by using a lexical variable which goes
a05e4845 494out of scope. In this example, you define the subroutine C<counter>, and
6670e5e7
RGS
495it uses the lexical variable C<$count>. Since you wrap this in a BEGIN
496block, C<$count> is defined at compile-time, but also goes out of
497scope at the end of the BEGIN block. The BEGIN block also ensures that
498the subroutine and the value it uses is defined at compile-time so the
499subroutine is ready to use just like any other subroutine, and you can
500put this code in the same place as other subroutines in the program
501text (i.e. at the end of the code, typically). The subroutine
502C<counter> still has a reference to the data, and is the only way you
503can access the value (and each time you do, you increment the value).
504The data in chunk of memory defined by C<$count> is private to
505C<counter>.
506
ac003c96
RGS
507 BEGIN {
508 my $count = 1;
509 sub counter { $count++ }
510 }
511
512 my $start = counter();
513
514 .... # code that calls counter();
515
516 my $end = counter();
68dc0745 517
6670e5e7
RGS
518In the previous example, you created a function-private variable
519because only one function remembered its reference. You could define
520multiple functions while the variable is in scope, and each function
521can share the "private" variable. It's not really "static" because you
522can access it outside the function while the lexical variable is in
523scope, and even create references to it. In this example,
524C<increment_count> and C<return_count> share the variable. One
525function adds to the value and the other simply returns the value.
526They can both access C<$count>, and since it has gone out of scope,
527there is no other way to access it.
68dc0745 528
ac003c96
RGS
529 BEGIN {
530 my $count = 1;
531 sub increment_count { $count++ }
532 sub return_count { $count }
533 }
68dc0745 534
6670e5e7
RGS
535To declare a file-private variable, you still use a lexical variable.
536A file is also a scope, so a lexical variable defined in the file
537cannot be seen from any other file.
68dc0745 538
6670e5e7
RGS
539See L<perlsub/"Persistent Private Variables"> for more information.
540The discussion of closures in L<perlref> may help you even though we
541did not use anonymous subroutines in this answer. See
542L<perlsub/"Persistent Private Variables"> for details.
c8db1d39 543
68dc0745 544=head2 What's the difference between dynamic and lexical (static) scoping? Between local() and my()?
545
a6dd486b
JB
546C<local($x)> saves away the old value of the global variable C<$x>
547and assigns a new value for the duration of the subroutine I<which is
68dc0745 548visible in other functions called from that subroutine>. This is done
549at run-time, so is called dynamic scoping. local() always affects global
550variables, also called package variables or dynamic variables.
551
552C<my($x)> creates a new variable that is only visible in the current
a6dd486b 553subroutine. This is done at compile-time, so it is called lexical or
68dc0745 554static scoping. my() always affects private variables, also called
555lexical variables or (improperly) static(ly scoped) variables.
556
557For instance:
558
ac003c96
RGS
559 sub visible {
560 print "var has value $var\n";
561 }
562
563 sub dynamic {
564 local $var = 'local'; # new temporary value for the still-global
565 visible(); # variable called $var
566 }
567
568 sub lexical {
569 my $var = 'private'; # new private variable, $var
570 visible(); # (invisible outside of sub scope)
571 }
572
573 $var = 'global';
574
575 visible(); # prints global
576 dynamic(); # prints local
577 lexical(); # prints global
68dc0745 578
579Notice how at no point does the value "private" get printed. That's
580because $var only has that value within the block of the lexical()
581function, and it is hidden from called subroutine.
582
583In summary, local() doesn't make what you think of as private, local
584variables. It gives a global variable a temporary value. my() is
585what you're looking for if you want private variables.
586
197aec24 587See L<perlsub/"Private Variables via my()"> and
13a2d996 588L<perlsub/"Temporary Values via local()"> for excruciating details.
68dc0745 589
590=head2 How can I access a dynamic variable while a similarly named lexical is in scope?
591
49d635f9
RGS
592If you know your package, you can just mention it explicitly, as in
593$Some_Pack::var. Note that the notation $::var is B<not> the dynamic $var
594in the current package, but rather the one in the "main" package, as
595though you had written $main::var.
596
597 use vars '$var';
598 local $var = "global";
599 my $var = "lexical";
68dc0745 600
49d635f9
RGS
601 print "lexical is $var\n";
602 print "global is $main::var\n";
68dc0745 603
49d635f9
RGS
604Alternatively you can use the compiler directive our() to bring a
605dynamic variable into the current lexical scope.
68dc0745 606
49d635f9
RGS
607 require 5.006; # our() did not exist before 5.6
608 use vars '$var';
68dc0745 609
49d635f9
RGS
610 local $var = "global";
611 my $var = "lexical";
612
613 print "lexical is $var\n";
614
615 {
ac003c96
RGS
616 our $var;
617 print "global is $var\n";
49d635f9 618 }
68dc0745 619
620=head2 What's the difference between deep and shallow binding?
621
622In deep binding, lexical variables mentioned in anonymous subroutines
623are the same ones that were in scope when the subroutine was created.
624In shallow binding, they are whichever variables with the same names
625happen to be in scope when the subroutine is called. Perl always uses
626deep binding of lexical variables (i.e., those created with my()).
627However, dynamic variables (aka global, local, or package variables)
628are effectively shallowly bound. Consider this just one more reason
629not to use them. See the answer to L<"What's a closure?">.
630
04d666b1 631=head2 Why doesn't "my($foo) = E<lt>FILEE<gt>;" work right?
68dc0745 632
c8db1d39 633C<my()> and C<local()> give list context to the right hand side
c47ff5f1 634of C<=>. The <FH> read operation, like so many of Perl's
c8db1d39
TC
635functions and operators, can tell which context it was called in and
636behaves appropriately. In general, the scalar() function can help.
637This function does nothing to the data itself (contrary to popular myth)
638but rather tells its argument to behave in whatever its scalar fashion is.
639If that function doesn't have a defined scalar behavior, this of course
640doesn't help you (such as with sort()).
68dc0745 641
642To enforce scalar context in this particular case, however, you need
643merely omit the parentheses:
644
ac003c96
RGS
645 local($foo) = <FILE>; # WRONG
646 local($foo) = scalar(<FILE>); # ok
647 local $foo = <FILE>; # right
68dc0745 648
649You should probably be using lexical variables anyway, although the
650issue is the same here:
651
ac003c96
RGS
652 my($foo) = <FILE>; # WRONG
653 my $foo = <FILE>; # right
68dc0745 654
54310121 655=head2 How do I redefine a builtin function, operator, or method?
68dc0745 656
657Why do you want to do that? :-)
658
659If you want to override a predefined function, such as open(),
660then you'll have to import the new definition from a different
4a4eefd0 661module. See L<perlsub/"Overriding Built-in Functions">. There's
65acb1b1 662also an example in L<perltoot/"Class::Template">.
68dc0745 663
664If you want to overload a Perl operator, such as C<+> or C<**>,
665then you'll want to use the C<use overload> pragma, documented
666in L<overload>.
667
668If you're talking about obscuring method calls in parent classes,
669see L<perltoot/"Overridden Methods">.
670
671=head2 What's the difference between calling a function as &foo and foo()?
672
673When you call a function as C<&foo>, you allow that function access to
a6dd486b
JB
674your current @_ values, and you bypass prototypes.
675The function doesn't get an empty @_--it gets yours! While not
68dc0745 676strictly speaking a bug (it's documented that way in L<perlsub>), it
677would be hard to consider this a feature in most cases.
678
c8db1d39 679When you call your function as C<&foo()>, then you I<do> get a new @_,
68dc0745 680but prototyping is still circumvented.
681
682Normally, you want to call a function using C<foo()>. You may only
683omit the parentheses if the function is already known to the compiler
684because it already saw the definition (C<use> but not C<require>),
685or via a forward reference or C<use subs> declaration. Even in this
686case, you get a clean @_ without any of the old values leaking through
687where they don't belong.
688
689=head2 How do I create a switch or case statement?
690
f449fe8a
RGS
691If one wants to use pure Perl and to be compatible with Perl versions
692prior to 5.10, the general answer is to write a construct like this:
c8db1d39 693
ac003c96
RGS
694 for ($variable_to_test) {
695 if (/pat1/) { } # do something
696 elsif (/pat2/) { } # do something else
697 elsif (/pat3/) { } # do something else
698 else { } # default
699 }
68dc0745 700
f449fe8a
RGS
701Here's a simple example of a switch based on pattern matching,
702lined up in a way to make it look more like a switch statement.
8305e449 703We'll do a multiway conditional based on the type of reference stored
c8db1d39
TC
704in $whatchamacallit:
705
706 SWITCH: for (ref $whatchamacallit) {
68dc0745 707
708 /^$/ && die "not a reference";
709
710 /SCALAR/ && do {
711 print_scalar($$ref);
712 last SWITCH;
713 };
714
715 /ARRAY/ && do {
716 print_array(@$ref);
717 last SWITCH;
718 };
719
720 /HASH/ && do {
721 print_hash(%$ref);
722 last SWITCH;
723 };
724
725 /CODE/ && do {
726 warn "can't print function ref";
727 last SWITCH;
728 };
729
730 # DEFAULT
731
732 warn "User defined type skipped";
733
734 }
735
f449fe8a 736See L<perlsyn> for other examples in this style.
c8db1d39
TC
737
738Sometimes you should change the positions of the constant and the variable.
739For example, let's say you wanted to test which of many answers you were
740given, but in a case-insensitive way that also allows abbreviations.
741You can use the following technique if the strings all start with
a6dd486b 742different characters or if you want to arrange the matches so that
c8db1d39
TC
743one takes precedence over another, as C<"SEND"> has precedence over
744C<"STOP"> here:
745
ac003c96
RGS
746 chomp($answer = <>);
747 if ("SEND" =~ /^\Q$answer/i) { print "Action is send\n" }
748 elsif ("STOP" =~ /^\Q$answer/i) { print "Action is stop\n" }
749 elsif ("ABORT" =~ /^\Q$answer/i) { print "Action is abort\n" }
750 elsif ("LIST" =~ /^\Q$answer/i) { print "Action is list\n" }
751 elsif ("EDIT" =~ /^\Q$answer/i) { print "Action is edit\n" }
c8db1d39 752
197aec24 753A totally different approach is to create a hash of function references.
c8db1d39 754
ac003c96
RGS
755 my %commands = (
756 "happy" => \&joy,
757 "sad", => \&sullen,
758 "done" => sub { die "See ya!" },
759 "mad" => \&angry,
760 );
761
762 print "How are you? ";
763 chomp($string = <STDIN>);
764 if ($commands{$string}) {
765 $commands{$string}->();
766 } else {
767 print "No such command: $string\n";
768 }
c8db1d39 769
f449fe8a
RGS
770Note that starting from version 5.10, Perl has now a native switch
771statement. See L<perlsyn>.
772
773Starting from Perl 5.8, a source filter module, C<Switch>, can also be
774used to get switch and case. Its use is now discouraged, because it's
775not fully compatible with the native switch of Perl 5.10, and because,
776as it's implemented as a source filter, it doesn't always work as intended
777when complex syntax is involved.
778
49d635f9 779=head2 How can I catch accesses to undefined variables, functions, or methods?
68dc0745 780
781The AUTOLOAD method, discussed in L<perlsub/"Autoloading"> and
782L<perltoot/"AUTOLOAD: Proxy Methods">, lets you capture calls to
783undefined functions and methods.
784
785When it comes to undefined variables that would trigger a warning
49d635f9 786under C<use warnings>, you can promote the warning to an error.
68dc0745 787
49d635f9 788 use warnings FATAL => qw(uninitialized);
68dc0745 789
790=head2 Why can't a method included in this same file be found?
791
792Some possible reasons: your inheritance is getting confused, you've
793misspelled the method name, or the object is of the wrong type. Check
a6dd486b
JB
794out L<perltoot> for details about any of the above cases. You may
795also use C<print ref($object)> to find out the class C<$object> was
796blessed into.
68dc0745 797
798Another possible reason for problems is because you've used the
799indirect object syntax (eg, C<find Guru "Samy">) on a class name
800before Perl has seen that such a package exists. It's wisest to make
801sure your packages are all defined before you start using them, which
802will be taken care of if you use the C<use> statement instead of
a6dd486b 803C<require>. If not, make sure to use arrow notation (eg.,
c47ff5f1 804C<< Guru->find("Samy") >>) instead. Object notation is explained in
68dc0745 805L<perlobj>.
806
c8db1d39 807Make sure to read about creating modules in L<perlmod> and
ae93639c 808the perils of indirect objects in L<perlobj/"Method Invocation">.
c8db1d39 809
68dc0745 810=head2 How can I find out my current package?
811
812If you're just a random program, you can do this to find
813out what the currently compiled package is:
814
ac003c96 815 my $packname = __PACKAGE__;
68dc0745 816
a6dd486b 817But, if you're a method and you want to print an error message
68dc0745 818that includes the kind of object you were called on (which is
819not necessarily the same as the one in which you were compiled):
820
ac003c96
RGS
821 sub amethod {
822 my $self = shift;
823 my $class = ref($self) || $self;
824 warn "called me from a $class object";
825 }
68dc0745 826
46fc3d4c 827=head2 How can I comment out a large block of perl code?
828
659cfd94 829You can use embedded POD to discard it. Enclose the blocks you want
7678cced
RGS
830to comment out in POD markers. The <=begin> directive marks a section
831for a specific formatter. Use the C<comment> format, which no formatter
832should claim to understand (by policy). Mark the end of the block
833with <=end>.
46fc3d4c 834
ac003c96
RGS
835 # program is here
836
837 =begin comment
838
839 all of this stuff
840
841 here will be ignored
842 by everyone
843
7678cced 844 =end comment
ac003c96
RGS
845
846 =cut
847
848 # program continues
46fc3d4c 849
f05bbc40
JH
850The pod directives cannot go just anywhere. You must put a
851pod directive where the parser is expecting a new statement,
852not just in the middle of an expression or some other
659cfd94 853arbitrary grammar production.
fc36a67e 854
f05bbc40 855See L<perlpod> for more details.
c8db1d39 856
65acb1b1
TC
857=head2 How do I clear a package?
858
859Use this code, provided by Mark-Jason Dominus:
860
ac003c96
RGS
861 sub scrub_package {
862 no strict 'refs';
863 my $pack = shift;
864 die "Shouldn't delete main package"
865 if $pack eq "" || $pack eq "main";
866 my $stash = *{$pack . '::'}{HASH};
867 my $name;
868 foreach $name (keys %$stash) {
869 my $fullname = $pack . '::' . $name;
870 # Get rid of everything with that name.
871 undef $$fullname;
872 undef @$fullname;
873 undef %$fullname;
874 undef &$fullname;
875 undef *$fullname;
876 }
65acb1b1 877 }
65acb1b1 878
197aec24 879Or, if you're using a recent release of Perl, you can
65acb1b1
TC
880just use the Symbol::delete_package() function instead.
881
d92eb7b0
GS
882=head2 How can I use a variable as a variable name?
883
884Beginners often think they want to have a variable contain the name
885of a variable.
886
ac003c96
RGS
887 $fred = 23;
888 $varname = "fred";
889 ++$$varname; # $fred now 24
d92eb7b0
GS
890
891This works I<sometimes>, but it is a very bad idea for two reasons.
892
a6dd486b
JB
893The first reason is that this technique I<only works on global
894variables>. That means that if $fred is a lexical variable created
895with my() in the above example, the code wouldn't work at all: you'd
896accidentally access the global and skip right over the private lexical
897altogether. Global variables are bad because they can easily collide
898accidentally and in general make for non-scalable and confusing code.
d92eb7b0
GS
899
900Symbolic references are forbidden under the C<use strict> pragma.
901They are not true references and consequently are not reference counted
902or garbage collected.
903
904The other reason why using a variable to hold the name of another
a6dd486b 905variable is a bad idea is that the question often stems from a lack of
d92eb7b0
GS
906understanding of Perl data structures, particularly hashes. By using
907symbolic references, you are just using the package's symbol-table hash
908(like C<%main::>) instead of a user-defined hash. The solution is to
909use your own hash or a real reference instead.
910
ac003c96
RGS
911 $USER_VARS{"fred"} = 23;
912 $varname = "fred";
913 $USER_VARS{$varname}++; # not $$varname++
d92eb7b0
GS
914
915There we're using the %USER_VARS hash instead of symbolic references.
916Sometimes this comes up in reading strings from the user with variable
917references and wanting to expand them to the values of your perl
918program's variables. This is also a bad idea because it conflates the
919program-addressable namespace and the user-addressable one. Instead of
920reading a string and expanding it to the actual contents of your program's
921own variables:
922
ac003c96
RGS
923 $str = 'this has a $fred and $barney in it';
924 $str =~ s/(\$\w+)/$1/eeg; # need double eval
d92eb7b0 925
a6dd486b 926it would be better to keep a hash around like %USER_VARS and have
d92eb7b0
GS
927variable references actually refer to entries in that hash:
928
ac003c96 929 $str =~ s/\$(\w+)/$USER_VARS{$1}/g; # no /e here at all
d92eb7b0
GS
930
931That's faster, cleaner, and safer than the previous approach. Of course,
932you don't need to use a dollar sign. You could use your own scheme to
933make it less confusing, like bracketed percent symbols, etc.
934
ac003c96
RGS
935 $str = 'this has a %fred% and %barney% in it';
936 $str =~ s/%(\w+)%/$USER_VARS{$1}/g; # no /e here at all
d92eb7b0 937
a6dd486b
JB
938Another reason that folks sometimes think they want a variable to
939contain the name of a variable is because they don't know how to build
940proper data structures using hashes. For example, let's say they
941wanted two hashes in their program: %fred and %barney, and that they
942wanted to use another scalar variable to refer to those by name.
d92eb7b0 943
ac003c96
RGS
944 $name = "fred";
945 $$name{WIFE} = "wilma"; # set %fred
d92eb7b0 946
ac003c96
RGS
947 $name = "barney";
948 $$name{WIFE} = "betty"; # set %barney
d92eb7b0
GS
949
950This is still a symbolic reference, and is still saddled with the
951problems enumerated above. It would be far better to write:
952
ac003c96
RGS
953 $folks{"fred"}{WIFE} = "wilma";
954 $folks{"barney"}{WIFE} = "betty";
d92eb7b0
GS
955
956And just use a multilevel hash to start with.
957
958The only times that you absolutely I<must> use symbolic references are
959when you really must refer to the symbol table. This may be because it's
960something that can't take a real reference to, such as a format name.
961Doing so may also be important for method calls, since these always go
962through the symbol table for resolution.
963
964In those cases, you would turn off C<strict 'refs'> temporarily so you
965can play around with the symbol table. For example:
966
ac003c96
RGS
967 @colors = qw(red blue green yellow orange purple violet);
968 for my $name (@colors) {
969 no strict 'refs'; # renege for the block
970 *$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
971 }
d92eb7b0
GS
972
973All those functions (red(), blue(), green(), etc.) appear to be separate,
974but the real code in the closure actually was compiled only once.
975
976So, sometimes you might want to use symbolic references to directly
977manipulate the symbol table. This doesn't matter for formats, handles, and
a6dd486b
JB
978subroutines, because they are always global--you can't use my() on them.
979For scalars, arrays, and hashes, though--and usually for subroutines--
980you probably only want to use hard references.
d92eb7b0 981
5cd0b561
RGS
982=head2 What does "bad interpreter" mean?
983
571e049f
RGS
984(contributed by brian d foy)
985
5cd0b561
RGS
986The "bad interpreter" message comes from the shell, not perl. The
987actual message may vary depending on your platform, shell, and locale
988settings.
989
990If you see "bad interpreter - no such file or directory", the first
991line in your perl script (the "shebang" line) does not contain the
6670e5e7 992right path to perl (or any other program capable of running scripts).
5cd0b561 993Sometimes this happens when you move the script from one machine to
ac9dac7f 994another and each machine has a different path to perl--/usr/bin/perl
571e049f 995versus /usr/local/bin/perl for instance. It may also indicate
6670e5e7
RGS
996that the source machine has CRLF line terminators and the
997destination machine has LF only: the shell tries to find
571e049f 998/usr/bin/perl<CR>, but can't.
5cd0b561
RGS
999
1000If you see "bad interpreter: Permission denied", you need to make your
1001script executable.
1002
1003In either case, you should still be able to run the scripts with perl
1004explicitly:
1005
1006 % perl script.pl
1007
1008If you get a message like "perl: command not found", perl is not in
1009your PATH, which might also mean that the location of perl is not
1010where you expect it so you need to adjust your shebang line.
1011
500071f4
RGS
1012=head1 REVISION
1013
c195e131 1014Revision: $Revision: 10100 $
500071f4 1015
c195e131 1016Date: $Date: 2007-10-21 20:59:30 +0200 (Sun, 21 Oct 2007) $
500071f4
RGS
1017
1018See L<perlfaq> for source control details and availability.
1019
68dc0745 1020=head1 AUTHOR AND COPYRIGHT
1021
ee891a00 1022Copyright (c) 1997-2007 Tom Christiansen, Nathan Torkington, and
7678cced 1023other authors as noted. All rights reserved.
5a964f20 1024
5a7beb56
JH
1025This documentation is free; you can redistribute it and/or modify it
1026under the same terms as Perl itself.
5a964f20
TC
1027
1028Irrespective of its distribution, all code examples in this file
1029are hereby placed into the public domain. You are permitted and
1030encouraged to use this code in your own programs for fun
1031or for profit as you see fit. A simple comment in the code giving
1032credit would be courteous but is not required.
a6dd486b 1033