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