Commit | Line | Data |
---|---|---|
68dc0745 | 1 | =head1 NAME |
2 | ||
d92eb7b0 | 3 | perlfaq7 - Perl Language Issues ($Revision: 1.28 $, $Date: 1999/05/23 20:36:18 $) |
68dc0745 | 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 | ||
c8db1d39 TC |
12 | There is no BNF, but you can paw your way through the yacc grammar in |
13 | perly.y in the source distribution if you're particularly brave. The | |
14 | grammar relies on very smart tokenizing code, so be prepared to | |
15 | venture into toke.c as well. | |
16 | ||
17 | In the words of Chaim Frenkel: "Perl's grammar can not be reduced to BNF. | |
18 | The work of parsing perl is distributed between yacc, the lexer, smoke | |
19 | and mirrors." | |
68dc0745 | 20 | |
d92eb7b0 | 21 | =head2 What are all these $@%&* punctuation signs, and how do I know when to use them? |
68dc0745 | 22 | |
23 | They are type specifiers, as detailed in L<perldata>: | |
24 | ||
25 | $ for scalar values (number, string or reference) | |
26 | @ for arrays | |
27 | % for hashes (associative arrays) | |
d92eb7b0 | 28 | & for subroutines (aka functions, procedures, methods) |
68dc0745 | 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. | |
31 | ||
68dc0745 | 32 | A couple of others that you're likely to encounter that aren't |
33 | really type specifiers are: | |
34 | ||
35 | <> are used for inputting a record from a filehandle. | |
36 | \ takes a reference to something. | |
37 | ||
c47ff5f1 GS |
38 | Note that <FILE> is I<neither> the type specifier for files |
39 | nor the name of the handle. It is the C<< <> >> operator applied | |
68dc0745 | 40 | to the handle FILE. It reads one line (well, record - see |
41 | L<perlvar/$/>) from the handle FILE in scalar context, or I<all> lines | |
42 | in list context. When performing open, close, or any other operation | |
c47ff5f1 | 43 | besides C<< <> >> on files, or even talking about the handle, do |
68dc0745 | 44 | I<not> use the brackets. These are correct: C<eof(FH)>, C<seek(FH, 0, |
45 | 2)> and "copying from STDIN to FILE". | |
46 | ||
47 | =head2 Do I always/never have to quote my strings or use semicolons and commas? | |
48 | ||
49 | Normally, a bareword doesn't need to be quoted, but in most cases | |
50 | probably should be (and must be under C<use strict>). But a hash key | |
51 | consisting of a simple word (that isn't the name of a defined | |
c47ff5f1 | 52 | subroutine) and the left-hand operand to the C<< => >> operator both |
68dc0745 | 53 | count as though they were quoted: |
54 | ||
55 | This is like this | |
56 | ------------ --------------- | |
57 | $foo{line} $foo{"line"} | |
58 | bar => stuff "bar" => stuff | |
59 | ||
60 | The final semicolon in a block is optional, as is the final comma in a | |
61 | list. Good style (see L<perlstyle>) says to put them in except for | |
62 | one-liners: | |
63 | ||
64 | if ($whoops) { exit 1 } | |
65 | @nums = (1, 2, 3); | |
66 | ||
67 | if ($whoops) { | |
68 | exit 1; | |
69 | } | |
70 | @lines = ( | |
71 | "There Beren came from mountains cold", | |
72 | "And lost he wandered under leaves", | |
73 | ); | |
74 | ||
75 | =head2 How do I skip some return values? | |
76 | ||
77 | One way is to treat the return values as a list and index into it: | |
78 | ||
79 | $dir = (getpwnam($user))[7]; | |
80 | ||
81 | Another way is to use undef as an element on the left-hand-side: | |
82 | ||
83 | ($dev, $ino, undef, undef, $uid, $gid) = stat($file); | |
84 | ||
85 | =head2 How do I temporarily block warnings? | |
86 | ||
9f1b1f2d GS |
87 | If you are running Perl 5.6.0 or better, the C<use warnings> pragma |
88 | allows fine control of what warning are produced. | |
89 | See L<perllexwarn> for more details. | |
90 | ||
91 | { | |
92 | no warnings; # temporarily turn off warnings | |
93 | $a = $b + $c; # I know these might be undef | |
94 | } | |
95 | ||
96 | If you have an older version of Perl, the C<$^W> variable (documented | |
97 | in L<perlvar>) controls runtime warnings for a block: | |
68dc0745 | 98 | |
99 | { | |
100 | local $^W = 0; # temporarily turn off warnings | |
101 | $a = $b + $c; # I know these might be undef | |
102 | } | |
103 | ||
104 | Note that like all the punctuation variables, you cannot currently | |
105 | use my() on C<$^W>, only local(). | |
106 | ||
68dc0745 | 107 | =head2 What's an extension? |
108 | ||
109 | A way of calling compiled C code from Perl. Reading L<perlxstut> | |
110 | is a good place to learn more about extensions. | |
111 | ||
112 | =head2 Why do Perl operators have different precedence than C operators? | |
113 | ||
114 | Actually, they don't. All C operators that Perl copies have the same | |
115 | precedence in Perl as they do in C. The problem is with operators that C | |
116 | doesn't have, especially functions that give a list context to everything | |
117 | on their right, eg print, chmod, exec, and so on. Such functions are | |
118 | called "list operators" and appear as such in the precedence table in | |
119 | L<perlop>. | |
120 | ||
121 | A common mistake is to write: | |
122 | ||
123 | unlink $file || die "snafu"; | |
124 | ||
125 | This gets interpreted as: | |
126 | ||
127 | unlink ($file || die "snafu"); | |
128 | ||
129 | To avoid this problem, either put in extra parentheses or use the | |
130 | super low precedence C<or> operator: | |
131 | ||
132 | (unlink $file) || die "snafu"; | |
133 | unlink $file or die "snafu"; | |
134 | ||
135 | The "English" operators (C<and>, C<or>, C<xor>, and C<not>) | |
136 | deliberately have precedence lower than that of list operators for | |
137 | just such situations as the one above. | |
138 | ||
139 | Another operator with surprising precedence is exponentiation. It | |
140 | binds more tightly even than unary minus, making C<-2**2> product a | |
141 | negative not a positive four. It is also right-associating, meaning | |
142 | that C<2**3**2> is two raised to the ninth power, not eight squared. | |
143 | ||
c8db1d39 TC |
144 | Although it has the same precedence as in C, Perl's C<?:> operator |
145 | produces an lvalue. This assigns $x to either $a or $b, depending | |
146 | on the trueness of $maybe: | |
147 | ||
148 | ($maybe ? $a : $b) = $x; | |
149 | ||
68dc0745 | 150 | =head2 How do I declare/create a structure? |
151 | ||
152 | In general, you don't "declare" a structure. Just use a (probably | |
153 | anonymous) hash reference. See L<perlref> and L<perldsc> for details. | |
154 | Here's an example: | |
155 | ||
156 | $person = {}; # new anonymous hash | |
157 | $person->{AGE} = 24; # set field AGE to 24 | |
158 | $person->{NAME} = "Nat"; # set field NAME to "Nat" | |
159 | ||
160 | If you're looking for something a bit more rigorous, try L<perltoot>. | |
161 | ||
162 | =head2 How do I create a module? | |
163 | ||
164 | A module is a package that lives in a file of the same name. For | |
165 | example, the Hello::There module would live in Hello/There.pm. For | |
166 | details, read L<perlmod>. You'll also find L<Exporter> helpful. If | |
167 | you're writing a C or mixed-language module with both C and Perl, then | |
168 | you should study L<perlxstut>. | |
169 | ||
170 | Here's a convenient template you might wish you use when starting your | |
171 | own module. Make sure to change the names appropriately. | |
172 | ||
173 | package Some::Module; # assumes Some/Module.pm | |
174 | ||
175 | use strict; | |
9f1b1f2d | 176 | use warnings; |
68dc0745 | 177 | |
178 | BEGIN { | |
179 | use Exporter (); | |
77ca0c92 | 180 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); |
68dc0745 | 181 | |
182 | ## set the version for version checking; uncomment to use | |
183 | ## $VERSION = 1.00; | |
184 | ||
185 | # if using RCS/CVS, this next line may be preferred, | |
186 | # but beware two-digit versions. | |
d92eb7b0 | 187 | $VERSION = do{my@r=q$Revision: 1.28 $=~/\d+/g;sprintf '%d.'.'%02d'x$#r,@r}; |
68dc0745 | 188 | |
189 | @ISA = qw(Exporter); | |
190 | @EXPORT = qw(&func1 &func2 &func3); | |
191 | %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], | |
192 | ||
193 | # your exported package globals go here, | |
194 | # as well as any optionally exported functions | |
195 | @EXPORT_OK = qw($Var1 %Hashit); | |
196 | } | |
77ca0c92 | 197 | our @EXPORT_OK; |
68dc0745 | 198 | |
3da4c8f2 DB |
199 | # exported package globals go here |
200 | our $Var1; | |
201 | our %Hashit; | |
202 | ||
68dc0745 | 203 | # non-exported package globals go here |
77ca0c92 LW |
204 | our @more; |
205 | our $stuff; | |
68dc0745 | 206 | |
207 | # initialize package globals, first exported ones | |
208 | $Var1 = ''; | |
209 | %Hashit = (); | |
210 | ||
211 | # then the others (which are still accessible as $Some::Module::stuff) | |
212 | $stuff = ''; | |
213 | @more = (); | |
214 | ||
215 | # all file-scoped lexicals must be created before | |
216 | # the functions below that use them. | |
217 | ||
218 | # file-private lexicals go here | |
219 | my $priv_var = ''; | |
220 | my %secret_hash = (); | |
221 | ||
222 | # here's a file-private function as a closure, | |
223 | # callable as &$priv_func; it cannot be prototyped. | |
224 | my $priv_func = sub { | |
225 | # stuff goes here. | |
226 | }; | |
227 | ||
228 | # make all your functions, whether exported or not; | |
229 | # remember to put something interesting in the {} stubs | |
230 | sub func1 {} # no prototype | |
231 | sub func2() {} # proto'd void | |
232 | sub func3($$) {} # proto'd to 2 scalars | |
233 | ||
234 | # this one isn't exported, but could be called! | |
235 | sub func4(\%) {} # proto'd to 1 hash ref | |
236 | ||
237 | END { } # module clean-up code here (global destructor) | |
238 | ||
239 | 1; # modules must return true | |
240 | ||
65acb1b1 TC |
241 | The h2xs program will create stubs for all the important stuff for you: |
242 | ||
243 | % h2xs -XA -n My::Module | |
244 | ||
68dc0745 | 245 | =head2 How do I create a class? |
246 | ||
247 | See L<perltoot> for an introduction to classes and objects, as well as | |
248 | L<perlobj> and L<perlbot>. | |
249 | ||
250 | =head2 How can I tell if a variable is tainted? | |
251 | ||
252 | See L<perlsec/"Laundering and Detecting Tainted Data">. Here's an | |
253 | example (which doesn't use any system calls, because the kill() | |
254 | is given no processes to signal): | |
255 | ||
256 | sub is_tainted { | |
257 | return ! eval { join('',@_), kill 0; 1; }; | |
258 | } | |
259 | ||
260 | This is not C<-w> clean, however. There is no C<-w> clean way to | |
261 | detect taintedness - take this as a hint that you should untaint | |
262 | all possibly-tainted data. | |
263 | ||
264 | =head2 What's a closure? | |
265 | ||
266 | Closures are documented in L<perlref>. | |
267 | ||
268 | I<Closure> is a computer science term with a precise but | |
269 | hard-to-explain meaning. Closures are implemented in Perl as anonymous | |
270 | subroutines with lasting references to lexical variables outside their | |
271 | own scopes. These lexicals magically refer to the variables that were | |
272 | around when the subroutine was defined (deep binding). | |
273 | ||
274 | Closures make sense in any programming language where you can have the | |
275 | return value of a function be itself a function, as you can in Perl. | |
276 | Note that some languages provide anonymous functions but are not | |
277 | capable of providing proper closures; the Python language, for | |
278 | example. For more information on closures, check out any textbook on | |
279 | functional programming. Scheme is a language that not only supports | |
280 | but encourages closures. | |
281 | ||
282 | Here's a classic function-generating function: | |
283 | ||
284 | sub add_function_generator { | |
285 | return sub { shift + shift }; | |
286 | } | |
287 | ||
288 | $add_sub = add_function_generator(); | |
c8db1d39 | 289 | $sum = $add_sub->(4,5); # $sum is 9 now. |
68dc0745 | 290 | |
291 | The closure works as a I<function template> with some customization | |
292 | slots left out to be filled later. The anonymous subroutine returned | |
293 | by add_function_generator() isn't technically a closure because it | |
294 | refers to no lexicals outside its own scope. | |
295 | ||
296 | Contrast this with the following make_adder() function, in which the | |
297 | returned anonymous function contains a reference to a lexical variable | |
298 | outside the scope of that function itself. Such a reference requires | |
299 | that Perl return a proper closure, thus locking in for all time the | |
300 | value that the lexical had when the function was created. | |
301 | ||
302 | sub make_adder { | |
303 | my $addpiece = shift; | |
304 | return sub { shift + $addpiece }; | |
305 | } | |
306 | ||
307 | $f1 = make_adder(20); | |
308 | $f2 = make_adder(555); | |
309 | ||
310 | Now C<&$f1($n)> is always 20 plus whatever $n you pass in, whereas | |
311 | C<&$f2($n)> is always 555 plus whatever $n you pass in. The $addpiece | |
312 | in the closure sticks around. | |
313 | ||
314 | Closures are often used for less esoteric purposes. For example, when | |
315 | you want to pass in a bit of code into a function: | |
316 | ||
317 | my $line; | |
318 | timeout( 30, sub { $line = <STDIN> } ); | |
319 | ||
c47ff5f1 GS |
320 | If the code to execute had been passed in as a string, |
321 | C<< '$line = <STDIN>' >>, there would have been no way for the | |
322 | hypothetical timeout() function to access the lexical variable | |
323 | $line back in its caller's scope. | |
68dc0745 | 324 | |
46fc3d4c | 325 | =head2 What is variable suicide and how can I prevent it? |
326 | ||
327 | Variable suicide is when you (temporarily or permanently) lose the | |
328 | value of a variable. It is caused by scoping through my() and local() | |
368c9434 | 329 | interacting with either closures or aliased foreach() iterator |
46fc3d4c | 330 | variables and subroutine arguments. It used to be easy to |
331 | inadvertently lose a variable's value this way, but now it's much | |
332 | harder. Take this code: | |
333 | ||
334 | my $f = "foo"; | |
335 | sub T { | |
336 | while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" } | |
337 | } | |
338 | T; | |
339 | print "Finally $f\n"; | |
340 | ||
341 | The $f that has "bar" added to it three times should be a new C<$f> | |
d92eb7b0 GS |
342 | (C<my $f> should create a new local variable each time through the loop). |
343 | It isn't, however. This was a bug, now fixed in the latest releases | |
344 | (tested against 5.004_05, 5.005_03, and 5.005_56). | |
46fc3d4c | 345 | |
d92eb7b0 | 346 | =head2 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}? |
68dc0745 | 347 | |
d92eb7b0 | 348 | With the exception of regexes, you need to pass references to these |
68dc0745 | 349 | objects. See L<perlsub/"Pass by Reference"> for this particular |
350 | question, and L<perlref> for information on references. | |
351 | ||
352 | =over 4 | |
353 | ||
354 | =item Passing Variables and Functions | |
355 | ||
356 | Regular variables and functions are quite easy: just pass in a | |
357 | reference to an existing or anonymous variable or function: | |
358 | ||
359 | func( \$some_scalar ); | |
360 | ||
65acb1b1 | 361 | func( \@some_array ); |
68dc0745 | 362 | func( [ 1 .. 10 ] ); |
363 | ||
364 | func( \%some_hash ); | |
365 | func( { this => 10, that => 20 } ); | |
366 | ||
367 | func( \&some_func ); | |
368 | func( sub { $_[0] ** $_[1] } ); | |
369 | ||
370 | =item Passing Filehandles | |
371 | ||
c8db1d39 TC |
372 | To pass filehandles to subroutines, use the C<*FH> or C<\*FH> notations. |
373 | These are "typeglobs" - see L<perldata/"Typeglobs and Filehandles"> | |
374 | and especially L<perlsub/"Pass by Reference"> for more information. | |
375 | ||
376 | Here's an excerpt: | |
377 | ||
378 | If you're passing around filehandles, you could usually just use the bare | |
379 | typeglob, like *STDOUT, but typeglobs references would be better because | |
380 | they'll still work properly under C<use strict 'refs'>. For example: | |
68dc0745 | 381 | |
c8db1d39 TC |
382 | splutter(\*STDOUT); |
383 | sub splutter { | |
384 | my $fh = shift; | |
385 | print $fh "her um well a hmmm\n"; | |
386 | } | |
387 | ||
388 | $rec = get_rec(\*STDIN); | |
389 | sub get_rec { | |
390 | my $fh = shift; | |
391 | return scalar <$fh>; | |
392 | } | |
393 | ||
394 | If you're planning on generating new filehandles, you could do this: | |
395 | ||
396 | sub openit { | |
397 | my $name = shift; | |
398 | local *FH; | |
399 | return open (FH, $path) ? *FH : undef; | |
400 | } | |
401 | $fh = openit('< /etc/motd'); | |
402 | print <$fh>; | |
68dc0745 | 403 | |
d92eb7b0 GS |
404 | =item Passing Regexes |
405 | ||
406 | To pass regexes around, you'll need to be using a release of Perl | |
407 | sufficiently recent as to support the C<qr//> construct, pass around | |
408 | strings and use an exception-trapping eval, or else be very, very clever. | |
68dc0745 | 409 | |
d92eb7b0 GS |
410 | Here's an example of how to pass in a string to be regex compared |
411 | using C<qr//>: | |
68dc0745 | 412 | |
413 | sub compare($$) { | |
d92eb7b0 GS |
414 | my ($val1, $regex) = @_; |
415 | my $retval = $val1 =~ /$regex/; | |
416 | return $retval; | |
417 | } | |
418 | $match = compare("old McDonald", qr/d.*D/i); | |
419 | ||
420 | Notice how C<qr//> allows flags at the end. That pattern was compiled | |
421 | at compile time, although it was executed later. The nifty C<qr//> | |
422 | notation wasn't introduced until the 5.005 release. Before that, you | |
423 | had to approach this problem much less intuitively. For example, here | |
424 | it is again if you don't have C<qr//>: | |
425 | ||
426 | sub compare($$) { | |
427 | my ($val1, $regex) = @_; | |
428 | my $retval = eval { $val1 =~ /$regex/ }; | |
68dc0745 | 429 | die if $@; |
430 | return $retval; | |
431 | } | |
432 | ||
d92eb7b0 | 433 | $match = compare("old McDonald", q/($?i)d.*D/); |
68dc0745 | 434 | |
435 | Make sure you never say something like this: | |
436 | ||
d92eb7b0 | 437 | return eval "\$val =~ /$regex/"; # WRONG |
68dc0745 | 438 | |
d92eb7b0 | 439 | or someone can sneak shell escapes into the regex due to the double |
68dc0745 | 440 | interpolation of the eval and the double-quoted string. For example: |
441 | ||
442 | $pattern_of_evil = 'danger ${ system("rm -rf * &") } danger'; | |
443 | ||
444 | eval "\$string =~ /$pattern_of_evil/"; | |
445 | ||
446 | Those preferring to be very, very clever might see the O'Reilly book, | |
447 | I<Mastering Regular Expressions>, by Jeffrey Friedl. Page 273's | |
448 | Build_MatchMany_Function() is particularly interesting. A complete | |
449 | citation of this book is given in L<perlfaq2>. | |
450 | ||
451 | =item Passing Methods | |
452 | ||
453 | To pass an object method into a subroutine, you can do this: | |
454 | ||
455 | call_a_lot(10, $some_obj, "methname") | |
456 | sub call_a_lot { | |
457 | my ($count, $widget, $trick) = @_; | |
458 | for (my $i = 0; $i < $count; $i++) { | |
459 | $widget->$trick(); | |
460 | } | |
461 | } | |
462 | ||
c8db1d39 | 463 | Or you can use a closure to bundle up the object and its method call |
68dc0745 | 464 | and arguments: |
465 | ||
466 | my $whatnot = sub { $some_obj->obfuscate(@args) }; | |
467 | func($whatnot); | |
468 | sub func { | |
469 | my $code = shift; | |
470 | &$code(); | |
471 | } | |
472 | ||
473 | You could also investigate the can() method in the UNIVERSAL class | |
474 | (part of the standard perl distribution). | |
475 | ||
476 | =back | |
477 | ||
478 | =head2 How do I create a static variable? | |
479 | ||
480 | As with most things in Perl, TMTOWTDI. What is a "static variable" in | |
481 | other languages could be either a function-private variable (visible | |
482 | only within a single function, retaining its value between calls to | |
483 | that function), or a file-private variable (visible only to functions | |
484 | within the file it was declared in) in Perl. | |
485 | ||
486 | Here's code to implement a function-private variable: | |
487 | ||
488 | BEGIN { | |
489 | my $counter = 42; | |
490 | sub prev_counter { return --$counter } | |
491 | sub next_counter { return $counter++ } | |
492 | } | |
493 | ||
494 | Now prev_counter() and next_counter() share a private variable $counter | |
495 | that was initialized at compile time. | |
496 | ||
497 | To declare a file-private variable, you'll still use a my(), putting | |
498 | it at the outer scope level at the top of the file. Assume this is in | |
499 | file Pax.pm: | |
500 | ||
501 | package Pax; | |
502 | my $started = scalar(localtime(time())); | |
503 | ||
504 | sub begun { return $started } | |
505 | ||
506 | When C<use Pax> or C<require Pax> loads this module, the variable will | |
507 | be initialized. It won't get garbage-collected the way most variables | |
508 | going out of scope do, because the begun() function cares about it, | |
509 | but no one else can get it. It is not called $Pax::started because | |
510 | its scope is unrelated to the package. It's scoped to the file. You | |
511 | could conceivably have several packages in that same file all | |
512 | accessing the same private variable, but another file with the same | |
513 | package couldn't get to it. | |
514 | ||
c2611fb3 | 515 | See L<perlsub/"Persistent Private Variables"> for details. |
c8db1d39 | 516 | |
68dc0745 | 517 | =head2 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? |
518 | ||
519 | C<local($x)> saves away the old value of the global variable C<$x>, | |
520 | and assigns a new value for the duration of the subroutine, I<which is | |
521 | visible in other functions called from that subroutine>. This is done | |
522 | at run-time, so is called dynamic scoping. local() always affects global | |
523 | variables, also called package variables or dynamic variables. | |
524 | ||
525 | C<my($x)> creates a new variable that is only visible in the current | |
526 | subroutine. This is done at compile-time, so is called lexical or | |
527 | static scoping. my() always affects private variables, also called | |
528 | lexical variables or (improperly) static(ly scoped) variables. | |
529 | ||
530 | For instance: | |
531 | ||
532 | sub visible { | |
533 | print "var has value $var\n"; | |
534 | } | |
535 | ||
536 | sub dynamic { | |
537 | local $var = 'local'; # new temporary value for the still-global | |
538 | visible(); # variable called $var | |
539 | } | |
540 | ||
541 | sub lexical { | |
542 | my $var = 'private'; # new private variable, $var | |
543 | visible(); # (invisible outside of sub scope) | |
544 | } | |
545 | ||
546 | $var = 'global'; | |
547 | ||
548 | visible(); # prints global | |
549 | dynamic(); # prints local | |
550 | lexical(); # prints global | |
551 | ||
552 | Notice how at no point does the value "private" get printed. That's | |
553 | because $var only has that value within the block of the lexical() | |
554 | function, and it is hidden from called subroutine. | |
555 | ||
556 | In summary, local() doesn't make what you think of as private, local | |
557 | variables. It gives a global variable a temporary value. my() is | |
558 | what you're looking for if you want private variables. | |
559 | ||
c8db1d39 TC |
560 | See L<perlsub/"Private Variables via my()"> and L<perlsub/"Temporary |
561 | Values via local()"> for excruciating details. | |
68dc0745 | 562 | |
563 | =head2 How can I access a dynamic variable while a similarly named lexical is in scope? | |
564 | ||
565 | You can do this via symbolic references, provided you haven't set | |
566 | C<use strict "refs">. So instead of $var, use C<${'var'}>. | |
567 | ||
568 | local $var = "global"; | |
569 | my $var = "lexical"; | |
570 | ||
571 | print "lexical is $var\n"; | |
572 | ||
573 | no strict 'refs'; | |
574 | print "global is ${'var'}\n"; | |
575 | ||
576 | If you know your package, you can just mention it explicitly, as in | |
577 | $Some_Pack::var. Note that the notation $::var is I<not> the dynamic | |
578 | $var in the current package, but rather the one in the C<main> | |
579 | package, as though you had written $main::var. Specifying the package | |
580 | directly makes you hard-code its name, but it executes faster and | |
581 | avoids running afoul of C<use strict "refs">. | |
582 | ||
583 | =head2 What's the difference between deep and shallow binding? | |
584 | ||
585 | In deep binding, lexical variables mentioned in anonymous subroutines | |
586 | are the same ones that were in scope when the subroutine was created. | |
587 | In shallow binding, they are whichever variables with the same names | |
588 | happen to be in scope when the subroutine is called. Perl always uses | |
589 | deep binding of lexical variables (i.e., those created with my()). | |
590 | However, dynamic variables (aka global, local, or package variables) | |
591 | are effectively shallowly bound. Consider this just one more reason | |
592 | not to use them. See the answer to L<"What's a closure?">. | |
593 | ||
c47ff5f1 | 594 | =head2 Why doesn't "my($foo) = <FILE>;" work right? |
68dc0745 | 595 | |
c8db1d39 | 596 | C<my()> and C<local()> give list context to the right hand side |
c47ff5f1 | 597 | of C<=>. The <FH> read operation, like so many of Perl's |
c8db1d39 TC |
598 | functions and operators, can tell which context it was called in and |
599 | behaves appropriately. In general, the scalar() function can help. | |
600 | This function does nothing to the data itself (contrary to popular myth) | |
601 | but rather tells its argument to behave in whatever its scalar fashion is. | |
602 | If that function doesn't have a defined scalar behavior, this of course | |
603 | doesn't help you (such as with sort()). | |
68dc0745 | 604 | |
605 | To enforce scalar context in this particular case, however, you need | |
606 | merely omit the parentheses: | |
607 | ||
608 | local($foo) = <FILE>; # WRONG | |
609 | local($foo) = scalar(<FILE>); # ok | |
610 | local $foo = <FILE>; # right | |
611 | ||
612 | You should probably be using lexical variables anyway, although the | |
613 | issue is the same here: | |
614 | ||
615 | my($foo) = <FILE>; # WRONG | |
616 | my $foo = <FILE>; # right | |
617 | ||
54310121 | 618 | =head2 How do I redefine a builtin function, operator, or method? |
68dc0745 | 619 | |
620 | Why do you want to do that? :-) | |
621 | ||
622 | If you want to override a predefined function, such as open(), | |
623 | then you'll have to import the new definition from a different | |
4a4eefd0 | 624 | module. See L<perlsub/"Overriding Built-in Functions">. There's |
65acb1b1 | 625 | also an example in L<perltoot/"Class::Template">. |
68dc0745 | 626 | |
627 | If you want to overload a Perl operator, such as C<+> or C<**>, | |
628 | then you'll want to use the C<use overload> pragma, documented | |
629 | in L<overload>. | |
630 | ||
631 | If you're talking about obscuring method calls in parent classes, | |
632 | see L<perltoot/"Overridden Methods">. | |
633 | ||
634 | =head2 What's the difference between calling a function as &foo and foo()? | |
635 | ||
636 | When you call a function as C<&foo>, you allow that function access to | |
637 | your current @_ values, and you by-pass prototypes. That means that | |
638 | the function doesn't get an empty @_, it gets yours! While not | |
639 | strictly speaking a bug (it's documented that way in L<perlsub>), it | |
640 | would be hard to consider this a feature in most cases. | |
641 | ||
c8db1d39 | 642 | When you call your function as C<&foo()>, then you I<do> get a new @_, |
68dc0745 | 643 | but prototyping is still circumvented. |
644 | ||
645 | Normally, you want to call a function using C<foo()>. You may only | |
646 | omit the parentheses if the function is already known to the compiler | |
647 | because it already saw the definition (C<use> but not C<require>), | |
648 | or via a forward reference or C<use subs> declaration. Even in this | |
649 | case, you get a clean @_ without any of the old values leaking through | |
650 | where they don't belong. | |
651 | ||
652 | =head2 How do I create a switch or case statement? | |
653 | ||
654 | This is explained in more depth in the L<perlsyn>. Briefly, there's | |
655 | no official case statement, because of the variety of tests possible | |
656 | in Perl (numeric comparison, string comparison, glob comparison, | |
d92eb7b0 | 657 | regex matching, overloaded comparisons, ...). Larry couldn't decide |
68dc0745 | 658 | how best to do this, so he left it out, even though it's been on the |
659 | wish list since perl1. | |
660 | ||
c8db1d39 TC |
661 | The general answer is to write a construct like this: |
662 | ||
663 | for ($variable_to_test) { | |
664 | if (/pat1/) { } # do something | |
665 | elsif (/pat2/) { } # do something else | |
666 | elsif (/pat3/) { } # do something else | |
667 | else { } # default | |
668 | } | |
68dc0745 | 669 | |
c8db1d39 TC |
670 | Here's a simple example of a switch based on pattern matching, this |
671 | time lined up in a way to make it look more like a switch statement. | |
672 | We'll do a multi-way conditional based on the type of reference stored | |
673 | in $whatchamacallit: | |
674 | ||
675 | SWITCH: for (ref $whatchamacallit) { | |
68dc0745 | 676 | |
677 | /^$/ && die "not a reference"; | |
678 | ||
679 | /SCALAR/ && do { | |
680 | print_scalar($$ref); | |
681 | last SWITCH; | |
682 | }; | |
683 | ||
684 | /ARRAY/ && do { | |
685 | print_array(@$ref); | |
686 | last SWITCH; | |
687 | }; | |
688 | ||
689 | /HASH/ && do { | |
690 | print_hash(%$ref); | |
691 | last SWITCH; | |
692 | }; | |
693 | ||
694 | /CODE/ && do { | |
695 | warn "can't print function ref"; | |
696 | last SWITCH; | |
697 | }; | |
698 | ||
699 | # DEFAULT | |
700 | ||
701 | warn "User defined type skipped"; | |
702 | ||
703 | } | |
704 | ||
c8db1d39 TC |
705 | See C<perlsyn/"Basic BLOCKs and Switch Statements"> for many other |
706 | examples in this style. | |
707 | ||
708 | Sometimes you should change the positions of the constant and the variable. | |
709 | For example, let's say you wanted to test which of many answers you were | |
710 | given, but in a case-insensitive way that also allows abbreviations. | |
711 | You can use the following technique if the strings all start with | |
712 | different characters, or if you want to arrange the matches so that | |
713 | one takes precedence over another, as C<"SEND"> has precedence over | |
714 | C<"STOP"> here: | |
715 | ||
716 | chomp($answer = <>); | |
717 | if ("SEND" =~ /^\Q$answer/i) { print "Action is send\n" } | |
718 | elsif ("STOP" =~ /^\Q$answer/i) { print "Action is stop\n" } | |
719 | elsif ("ABORT" =~ /^\Q$answer/i) { print "Action is abort\n" } | |
720 | elsif ("LIST" =~ /^\Q$answer/i) { print "Action is list\n" } | |
721 | elsif ("EDIT" =~ /^\Q$answer/i) { print "Action is edit\n" } | |
722 | ||
723 | A totally different approach is to create a hash of function references. | |
724 | ||
725 | my %commands = ( | |
726 | "happy" => \&joy, | |
727 | "sad", => \&sullen, | |
728 | "done" => sub { die "See ya!" }, | |
729 | "mad" => \&angry, | |
730 | ); | |
731 | ||
732 | print "How are you? "; | |
733 | chomp($string = <STDIN>); | |
734 | if ($commands{$string}) { | |
735 | $commands{$string}->(); | |
736 | } else { | |
737 | print "No such command: $string\n"; | |
738 | } | |
739 | ||
68dc0745 | 740 | =head2 How can I catch accesses to undefined variables/functions/methods? |
741 | ||
742 | The AUTOLOAD method, discussed in L<perlsub/"Autoloading"> and | |
743 | L<perltoot/"AUTOLOAD: Proxy Methods">, lets you capture calls to | |
744 | undefined functions and methods. | |
745 | ||
746 | When it comes to undefined variables that would trigger a warning | |
747 | under C<-w>, you can use a handler to trap the pseudo-signal | |
748 | C<__WARN__> like this: | |
749 | ||
750 | $SIG{__WARN__} = sub { | |
751 | ||
c8db1d39 | 752 | for ( $_[0] ) { # voici un switch statement |
68dc0745 | 753 | |
754 | /Use of uninitialized value/ && do { | |
755 | # promote warning to a fatal | |
756 | die $_; | |
757 | }; | |
758 | ||
759 | # other warning cases to catch could go here; | |
760 | ||
761 | warn $_; | |
762 | } | |
763 | ||
764 | }; | |
765 | ||
766 | =head2 Why can't a method included in this same file be found? | |
767 | ||
768 | Some possible reasons: your inheritance is getting confused, you've | |
769 | misspelled the method name, or the object is of the wrong type. Check | |
770 | out L<perltoot> for details on these. You may also use C<print | |
771 | ref($object)> to find out the class C<$object> was blessed into. | |
772 | ||
773 | Another possible reason for problems is because you've used the | |
774 | indirect object syntax (eg, C<find Guru "Samy">) on a class name | |
775 | before Perl has seen that such a package exists. It's wisest to make | |
776 | sure your packages are all defined before you start using them, which | |
777 | will be taken care of if you use the C<use> statement instead of | |
778 | C<require>. If not, make sure to use arrow notation (eg, | |
c47ff5f1 | 779 | C<< Guru->find("Samy") >>) instead. Object notation is explained in |
68dc0745 | 780 | L<perlobj>. |
781 | ||
c8db1d39 TC |
782 | Make sure to read about creating modules in L<perlmod> and |
783 | the perils of indirect objects in L<perlobj/"WARNING">. | |
784 | ||
68dc0745 | 785 | =head2 How can I find out my current package? |
786 | ||
787 | If you're just a random program, you can do this to find | |
788 | out what the currently compiled package is: | |
789 | ||
c8db1d39 | 790 | my $packname = __PACKAGE__; |
68dc0745 | 791 | |
792 | But if you're a method and you want to print an error message | |
793 | that includes the kind of object you were called on (which is | |
794 | not necessarily the same as the one in which you were compiled): | |
795 | ||
796 | sub amethod { | |
92c2ed05 | 797 | my $self = shift; |
68dc0745 | 798 | my $class = ref($self) || $self; |
799 | warn "called me from a $class object"; | |
800 | } | |
801 | ||
46fc3d4c | 802 | =head2 How can I comment out a large block of perl code? |
803 | ||
804 | Use embedded POD to discard it: | |
805 | ||
806 | # program is here | |
807 | ||
808 | =for nobody | |
809 | This paragraph is commented out | |
810 | ||
811 | # program continues | |
812 | ||
813 | =begin comment text | |
814 | ||
815 | all of this stuff | |
816 | ||
817 | here will be ignored | |
818 | by everyone | |
819 | ||
820 | =end comment text | |
821 | ||
fc36a67e | 822 | =cut |
823 | ||
c8db1d39 TC |
824 | This can't go just anywhere. You have to put a pod directive where |
825 | the parser is expecting a new statement, not just in the middle | |
826 | of an expression or some other arbitrary yacc grammar production. | |
827 | ||
65acb1b1 TC |
828 | =head2 How do I clear a package? |
829 | ||
830 | Use this code, provided by Mark-Jason Dominus: | |
831 | ||
832 | sub scrub_package { | |
833 | no strict 'refs'; | |
834 | my $pack = shift; | |
835 | die "Shouldn't delete main package" | |
836 | if $pack eq "" || $pack eq "main"; | |
837 | my $stash = *{$pack . '::'}{HASH}; | |
838 | my $name; | |
839 | foreach $name (keys %$stash) { | |
840 | my $fullname = $pack . '::' . $name; | |
841 | # Get rid of everything with that name. | |
842 | undef $$fullname; | |
843 | undef @$fullname; | |
844 | undef %$fullname; | |
845 | undef &$fullname; | |
846 | undef *$fullname; | |
847 | } | |
848 | } | |
849 | ||
850 | Or, if you're using a recent release of Perl, you can | |
851 | just use the Symbol::delete_package() function instead. | |
852 | ||
d92eb7b0 GS |
853 | =head2 How can I use a variable as a variable name? |
854 | ||
855 | Beginners often think they want to have a variable contain the name | |
856 | of a variable. | |
857 | ||
858 | $fred = 23; | |
859 | $varname = "fred"; | |
860 | ++$$varname; # $fred now 24 | |
861 | ||
862 | This works I<sometimes>, but it is a very bad idea for two reasons. | |
863 | ||
864 | The first reason is that they I<only work on global variables>. | |
865 | That means above that if $fred is a lexical variable created with my(), | |
866 | that the code won't work at all: you'll accidentally access the global | |
867 | and skip right over the private lexical altogether. Global variables | |
868 | are bad because they can easily collide accidentally and in general make | |
869 | for non-scalable and confusing code. | |
870 | ||
871 | Symbolic references are forbidden under the C<use strict> pragma. | |
872 | They are not true references and consequently are not reference counted | |
873 | or garbage collected. | |
874 | ||
875 | The other reason why using a variable to hold the name of another | |
876 | variable a bad idea is that the question often stems from a lack of | |
877 | understanding of Perl data structures, particularly hashes. By using | |
878 | symbolic references, you are just using the package's symbol-table hash | |
879 | (like C<%main::>) instead of a user-defined hash. The solution is to | |
880 | use your own hash or a real reference instead. | |
881 | ||
882 | $fred = 23; | |
883 | $varname = "fred"; | |
884 | $USER_VARS{$varname}++; # not $$varname++ | |
885 | ||
886 | There we're using the %USER_VARS hash instead of symbolic references. | |
887 | Sometimes this comes up in reading strings from the user with variable | |
888 | references and wanting to expand them to the values of your perl | |
889 | program's variables. This is also a bad idea because it conflates the | |
890 | program-addressable namespace and the user-addressable one. Instead of | |
891 | reading a string and expanding it to the actual contents of your program's | |
892 | own variables: | |
893 | ||
894 | $str = 'this has a $fred and $barney in it'; | |
895 | $str =~ s/(\$\w+)/$1/eeg; # need double eval | |
896 | ||
897 | Instead, it would be better to keep a hash around like %USER_VARS and have | |
898 | variable references actually refer to entries in that hash: | |
899 | ||
900 | $str =~ s/\$(\w+)/$USER_VARS{$1}/g; # no /e here at all | |
901 | ||
902 | That's faster, cleaner, and safer than the previous approach. Of course, | |
903 | you don't need to use a dollar sign. You could use your own scheme to | |
904 | make it less confusing, like bracketed percent symbols, etc. | |
905 | ||
906 | $str = 'this has a %fred% and %barney% in it'; | |
907 | $str =~ s/%(\w+)%/$USER_VARS{$1}/g; # no /e here at all | |
908 | ||
909 | Another reason that folks sometimes think they want a variable to contain | |
910 | the name of a variable is because they don't know how to build proper | |
911 | data structures using hashes. For example, let's say they wanted two | |
912 | hashes in their program: %fred and %barney, and to use another scalar | |
913 | variable to refer to those by name. | |
914 | ||
915 | $name = "fred"; | |
916 | $$name{WIFE} = "wilma"; # set %fred | |
917 | ||
918 | $name = "barney"; | |
919 | $$name{WIFE} = "betty"; # set %barney | |
920 | ||
921 | This is still a symbolic reference, and is still saddled with the | |
922 | problems enumerated above. It would be far better to write: | |
923 | ||
924 | $folks{"fred"}{WIFE} = "wilma"; | |
925 | $folks{"barney"}{WIFE} = "betty"; | |
926 | ||
927 | And just use a multilevel hash to start with. | |
928 | ||
929 | The only times that you absolutely I<must> use symbolic references are | |
930 | when you really must refer to the symbol table. This may be because it's | |
931 | something that can't take a real reference to, such as a format name. | |
932 | Doing so may also be important for method calls, since these always go | |
933 | through the symbol table for resolution. | |
934 | ||
935 | In those cases, you would turn off C<strict 'refs'> temporarily so you | |
936 | can play around with the symbol table. For example: | |
937 | ||
938 | @colors = qw(red blue green yellow orange purple violet); | |
939 | for my $name (@colors) { | |
940 | no strict 'refs'; # renege for the block | |
941 | *$name = sub { "<FONT COLOR='$name'>@_</FONT>" }; | |
942 | } | |
943 | ||
944 | All those functions (red(), blue(), green(), etc.) appear to be separate, | |
945 | but the real code in the closure actually was compiled only once. | |
946 | ||
947 | So, sometimes you might want to use symbolic references to directly | |
948 | manipulate the symbol table. This doesn't matter for formats, handles, and | |
949 | subroutines, because they are always global -- you can't use my() on them. | |
950 | But for scalars, arrays, and hashes -- and usually for subroutines -- | |
951 | you probably want to use hard references only. | |
952 | ||
68dc0745 | 953 | =head1 AUTHOR AND COPYRIGHT |
954 | ||
65acb1b1 | 955 | Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington. |
5a964f20 TC |
956 | All rights reserved. |
957 | ||
958 | When included as part of the Standard Version of Perl, or as part of | |
959 | its complete documentation whether printed or otherwise, this work | |
d92eb7b0 | 960 | may be distributed only under the terms of Perl's Artistic License. |
5a964f20 TC |
961 | Any distribution of this file or derivatives thereof I<outside> |
962 | of that package require that special arrangements be made with | |
963 | copyright holder. | |
964 | ||
965 | Irrespective of its distribution, all code examples in this file | |
966 | are hereby placed into the public domain. You are permitted and | |
967 | encouraged to use this code in your own programs for fun | |
968 | or for profit as you see fit. A simple comment in the code giving | |
969 | credit would be courteous but is not required. |