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