Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / man / man1 / perlfaq7.1
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "PERLFAQ7 1"
132.TH PERLFAQ7 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide"
133.SH "NAME"
134perlfaq7 \- General Perl Language Issues ($Revision: 1.28 $, $Date: 2005/12/31 00:54:37 $)
135.SH "DESCRIPTION"
136.IX Header "DESCRIPTION"
137This section deals with general Perl language issues that don't
138clearly fit into any of the other sections.
139.Sh "Can I get a BNF/yacc/RE for the Perl language?"
140.IX Subsection "Can I get a BNF/yacc/RE for the Perl language?"
141There is no \s-1BNF\s0, but you can paw your way through the yacc grammar in
142perly.y in the source distribution if you're particularly brave. The
143grammar relies on very smart tokenizing code, so be prepared to
144venture into toke.c as well.
145.PP
146In the words of Chaim Frenkel: \*(L"Perl's grammar can not be reduced to \s-1BNF\s0.
147The work of parsing perl is distributed between yacc, the lexer, smoke
148and mirrors.\*(R"
149.Sh "What are all these $@%&* punctuation signs, and how do I know when to use them?"
150.IX Subsection "What are all these $@%&* punctuation signs, and how do I know when to use them?"
151They are type specifiers, as detailed in perldata:
152.PP
153.Vb 6
154\& $ for scalar values (number, string or reference)
155\& @ for arrays
156\& % for hashes (associative arrays)
157\& & for subroutines (aka functions, procedures, methods)
158\& * for all types of that symbol name. In version 4 you used them like
159\& pointers, but in modern perls you can just use references.
160.Ve
161.PP
162There are couple of other symbols that you're likely to encounter that aren't
163really type specifiers:
164.PP
165.Vb 2
166\& <> are used for inputting a record from a filehandle.
167\& \e takes a reference to something.
168.Ve
169.PP
170Note that <\s-1FILE\s0> is \fIneither\fR the type specifier for files
171nor the name of the handle. It is the \f(CW\*(C`<>\*(C'\fR operator applied
172to the handle \s-1FILE\s0. It reads one line (well, record\*(--see
173"$/" in perlvar) from the handle \s-1FILE\s0 in scalar context, or \fIall\fR lines
174in list context. When performing open, close, or any other operation
175besides \f(CW\*(C`<>\*(C'\fR on files, or even when talking about the handle, do
176\&\fInot\fR use the brackets. These are correct: \f(CW\*(C`eof(FH)\*(C'\fR, \f(CW\*(C`seek(FH, 0,
1772)\*(C'\fR and \*(L"copying from \s-1STDIN\s0 to \s-1FILE\s0\*(R".
178.Sh "Do I always/never have to quote my strings or use semicolons and commas?"
179.IX Subsection "Do I always/never have to quote my strings or use semicolons and commas?"
180Normally, a bareword doesn't need to be quoted, but in most cases
181probably should be (and must be under \f(CW\*(C`use strict\*(C'\fR). But a hash key
182consisting of a simple word (that isn't the name of a defined
183subroutine) and the left-hand operand to the \f(CW\*(C`=>\*(C'\fR operator both
184count as though they were quoted:
185.PP
186.Vb 4
187\& This is like this
188\& ------------ ---------------
189\& $foo{line} $foo{'line'}
190\& bar => stuff 'bar' => stuff
191.Ve
192.PP
193The final semicolon in a block is optional, as is the final comma in a
194list. Good style (see perlstyle) says to put them in except for
195one\-liners:
196.PP
197.Vb 2
198\& if ($whoops) { exit 1 }
199\& @nums = (1, 2, 3);
200.Ve
201.PP
202.Vb 7
203\& if ($whoops) {
204\& exit 1;
205\& }
206\& @lines = (
207\& "There Beren came from mountains cold",
208\& "And lost he wandered under leaves",
209\& );
210.Ve
211.Sh "How do I skip some return values?"
212.IX Subsection "How do I skip some return values?"
213One way is to treat the return values as a list and index into it:
214.PP
215.Vb 1
216\& $dir = (getpwnam($user))[7];
217.Ve
218.PP
219Another way is to use undef as an element on the left\-hand\-side:
220.PP
221.Vb 1
222\& ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
223.Ve
224.PP
225You can also use a list slice to select only the elements that
226you need:
227.PP
228.Vb 1
229\& ($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5];
230.Ve
231.Sh "How do I temporarily block warnings?"
232.IX Subsection "How do I temporarily block warnings?"
233If you are running Perl 5.6.0 or better, the \f(CW\*(C`use warnings\*(C'\fR pragma
234allows fine control of what warning are produced.
235See perllexwarn for more details.
236.PP
237.Vb 4
238\& {
239\& no warnings; # temporarily turn off warnings
240\& $a = $b + $c; # I know these might be undef
241\& }
242.Ve
243.PP
244Additionally, you can enable and disable categories of warnings.
245You turn off the categories you want to ignore and you can still
246get other categories of warnings. See perllexwarn for the
247complete details, including the category names and hierarchy.
248.PP
249.Vb 4
250\& {
251\& no warnings 'uninitialized';
252\& $a = $b + $c;
253\& }
254.Ve
255.PP
256If you have an older version of Perl, the \f(CW$^W\fR variable (documented
257in perlvar) controls runtime warnings for a block:
258.PP
259.Vb 4
260\& {
261\& local $^W = 0; # temporarily turn off warnings
262\& $a = $b + $c; # I know these might be undef
263\& }
264.Ve
265.PP
266Note that like all the punctuation variables, you cannot currently
267use \fImy()\fR on \f(CW$^W\fR, only \fIlocal()\fR.
268.Sh "What's an extension?"
269.IX Subsection "What's an extension?"
270An extension is a way of calling compiled C code from Perl. Reading
271perlxstut is a good place to learn more about extensions.
272.Sh "Why do Perl operators have different precedence than C operators?"
273.IX Subsection "Why do Perl operators have different precedence than C operators?"
274Actually, they don't. All C operators that Perl copies have the same
275precedence in Perl as they do in C. The problem is with operators that C
276doesn't have, especially functions that give a list context to everything
277on their right, eg. print, chmod, exec, and so on. Such functions are
278called \*(L"list operators\*(R" and appear as such in the precedence table in
279perlop.
280.PP
281A common mistake is to write:
282.PP
283.Vb 1
284\& unlink $file || die "snafu";
285.Ve
286.PP
287This gets interpreted as:
288.PP
289.Vb 1
290\& unlink ($file || die "snafu");
291.Ve
292.PP
293To avoid this problem, either put in extra parentheses or use the
294super low precedence \f(CW\*(C`or\*(C'\fR operator:
295.PP
296.Vb 2
297\& (unlink $file) || die "snafu";
298\& unlink $file or die "snafu";
299.Ve
300.PP
301The \*(L"English\*(R" operators (\f(CW\*(C`and\*(C'\fR, \f(CW\*(C`or\*(C'\fR, \f(CW\*(C`xor\*(C'\fR, and \f(CW\*(C`not\*(C'\fR)
302deliberately have precedence lower than that of list operators for
303just such situations as the one above.
304.PP
305Another operator with surprising precedence is exponentiation. It
306binds more tightly even than unary minus, making \f(CW\*(C`\-2**2\*(C'\fR product a
307negative not a positive four. It is also right\-associating, meaning
308that \f(CW\*(C`2**3**2\*(C'\fR is two raised to the ninth power, not eight squared.
309.PP
310Although it has the same precedence as in C, Perl's \f(CW\*(C`?:\*(C'\fR operator
311produces an lvalue. This assigns \f(CW$x\fR to either \f(CW$a\fR or \f(CW$b\fR, depending
312on the trueness of \f(CW$maybe:\fR
313.PP
314.Vb 1
315\& ($maybe ? $a : $b) = $x;
316.Ve
317.Sh "How do I declare/create a structure?"
318.IX Subsection "How do I declare/create a structure?"
319In general, you don't \*(L"declare\*(R" a structure. Just use a (probably
320anonymous) hash reference. See perlref and perldsc for details.
321Here's an example:
322.PP
323.Vb 3
324\& $person = {}; # new anonymous hash
325\& $person->{AGE} = 24; # set field AGE to 24
326\& $person->{NAME} = "Nat"; # set field NAME to "Nat"
327.Ve
328.PP
329If you're looking for something a bit more rigorous, try perltoot.
330.Sh "How do I create a module?"
331.IX Subsection "How do I create a module?"
332(contributed by brian d foy)
333.PP
334perlmod, perlmodlib, perlmodstyle explain modules
335in all the gory details. perlnewmod gives a brief
336overview of the process along with a couple of suggestions
337about style.
338.PP
339If you need to include C code or C library interfaces in
340your module, you'll need h2xs. h2xs will create the module
341distribution structure and the initial interface files
342you'll need. perlxs and perlxstut explain the details.
343.PP
344If you don't need to use C code, other tools such as
345ExtUtils::ModuleMaker and Module::Starter, can help you
346create a skeleton module distribution.
347.PP
348You may also want to see Sam Tregar's \*(L"Writing Perl Modules
349for \s-1CPAN\s0\*(R" ( http://apress.com/book/bookDisplay.html?bID=14 )
350which is the best hands-on guide to creating module
351distributions.
352.Sh "How do I create a class?"
353.IX Subsection "How do I create a class?"
354See perltoot for an introduction to classes and objects, as well as
355perlobj and perlbot.
356.Sh "How can I tell if a variable is tainted?"
357.IX Subsection "How can I tell if a variable is tainted?"
358You can use the \fItainted()\fR function of the Scalar::Util module, available
359from \s-1CPAN\s0 (or included with Perl since release 5.8.0).
360See also \*(L"Laundering and Detecting Tainted Data\*(R" in perlsec.
361.Sh "What's a closure?"
362.IX Subsection "What's a closure?"
363Closures are documented in perlref.
364.PP
365\&\fIClosure\fR is a computer science term with a precise but
366hard-to-explain meaning. Closures are implemented in Perl as anonymous
367subroutines with lasting references to lexical variables outside their
368own scopes. These lexicals magically refer to the variables that were
369around when the subroutine was defined (deep binding).
370.PP
371Closures make sense in any programming language where you can have the
372return value of a function be itself a function, as you can in Perl.
373Note that some languages provide anonymous functions but are not
374capable of providing proper closures: the Python language, for
375example. For more information on closures, check out any textbook on
376functional programming. Scheme is a language that not only supports
377but encourages closures.
378.PP
379Here's a classic function-generating function:
380.PP
381.Vb 3
382\& sub add_function_generator {
383\& return sub { shift() + shift() };
384\& }
385.Ve
386.PP
387.Vb 2
388\& $add_sub = add_function_generator();
389\& $sum = $add_sub->(4,5); # $sum is 9 now.
390.Ve
391.PP
392The closure works as a \fIfunction template\fR with some customization
393slots left out to be filled later. The anonymous subroutine returned
394by \fIadd_function_generator()\fR isn't technically a closure because it
395refers to no lexicals outside its own scope.
396.PP
397Contrast this with the following \fImake_adder()\fR function, in which the
398returned anonymous function contains a reference to a lexical variable
399outside the scope of that function itself. Such a reference requires
400that Perl return a proper closure, thus locking in for all time the
401value that the lexical had when the function was created.
402.PP
403.Vb 4
404\& sub make_adder {
405\& my $addpiece = shift;
406\& return sub { shift() + $addpiece };
407\& }
408.Ve
409.PP
410.Vb 2
411\& $f1 = make_adder(20);
412\& $f2 = make_adder(555);
413.Ve
414.PP
415Now \f(CW\*(C`&$f1($n)\*(C'\fR is always 20 plus whatever \f(CW$n\fR you pass in, whereas
416\&\f(CW\*(C`&$f2($n)\*(C'\fR is always 555 plus whatever \f(CW$n\fR you pass in. The \f(CW$addpiece\fR
417in the closure sticks around.
418.PP
419Closures are often used for less esoteric purposes. For example, when
420you want to pass in a bit of code into a function:
421.PP
422.Vb 2
423\& my $line;
424\& timeout( 30, sub { $line = <STDIN> } );
425.Ve
426.PP
427If the code to execute had been passed in as a string,
428\&\f(CW'$line = <STDIN>'\fR, there would have been no way for the
429hypothetical \fItimeout()\fR function to access the lexical variable
430\&\f(CW$line\fR back in its caller's scope.
431.Sh "What is variable suicide and how can I prevent it?"
432.IX Subsection "What is variable suicide and how can I prevent it?"
433This problem was fixed in perl 5.004_05, so preventing it means upgrading
434your version of perl. ;)
435.PP
436Variable suicide is when you (temporarily or permanently) lose the value
437of a variable. It is caused by scoping through \fImy()\fR and \fIlocal()\fR
438interacting with either closures or aliased \fIforeach()\fR iterator variables
439and subroutine arguments. It used to be easy to inadvertently lose a
440variable's value this way, but now it's much harder. Take this code:
441.PP
442.Vb 6
443\& my $f = 'foo';
444\& sub T {
445\& while ($i++ < 3) { my $f = $f; $f .= $i; print $f, "\en" }
446\& }
447\& T;
448\& print "Finally $f\en";
449.Ve
450.PP
451If you are experiencing variable suicide, that \f(CW\*(C`my $f\*(C'\fR in the subroutine
452doesn't pick up a fresh copy of the \f(CW$f\fR whose value is <foo>. The output
453shows that inside the subroutine the value of \f(CW$f\fR leaks through when it
454shouldn't, as in this output:
455.PP
456.Vb 4
457\& foobar
458\& foobarbar
459\& foobarbarbar
460\& Finally foo
461.Ve
462.PP
463The \f(CW$f\fR that has \*(L"bar\*(R" added to it three times should be a new \f(CW$f\fR
464\&\f(CW\*(C`my $f\*(C'\fR should create a new lexical variable each time through the loop.
465The expected output is:
466.PP
467.Vb 4
468\& foobar
469\& foobar
470\& foobar
471\& Finally foo
472.Ve
473.Sh "How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?"
474.IX Subsection "How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?"
475With the exception of regexes, you need to pass references to these
476objects. See \*(L"Pass by Reference\*(R" in perlsub for this particular
477question, and perlref for information on references.
478.PP
479See \*(L"Passing Regexes\*(R", below, for information on passing regular
480expressions.
481.IP "Passing Variables and Functions" 4
482.IX Item "Passing Variables and Functions"
483Regular variables and functions are quite easy to pass: just pass in a
484reference to an existing or anonymous variable or function:
485.Sp
486.Vb 1
487\& func( \e$some_scalar );
488.Ve
489.Sp
490.Vb 2
491\& func( \e@some_array );
492\& func( [ 1 .. 10 ] );
493.Ve
494.Sp
495.Vb 2
496\& func( \e%some_hash );
497\& func( { this => 10, that => 20 } );
498.Ve
499.Sp
500.Vb 2
501\& func( \e&some_func );
502\& func( sub { $_[0] ** $_[1] } );
503.Ve
504.IP "Passing Filehandles" 4
505.IX Item "Passing Filehandles"
506As of Perl 5.6, you can represent filehandles with scalar variables
507which you treat as any other scalar.
508.Sp
509.Vb 2
510\& open my $fh, $filename or die "Cannot open $filename! $!";
511\& func( $fh );
512.Ve
513.Sp
514.Vb 2
515\& sub func {
516\& my $passed_fh = shift;
517.Ve
518.Sp
519.Vb 2
520\& my $line = <$fh>;
521\& }
522.Ve
523.Sp
524Before Perl 5.6, you had to use the \f(CW*FH\fR or \f(CW\*(C`\e*FH\*(C'\fR notations.
525These are \*(L"typeglobs\*(R"\-\-see \*(L"Typeglobs and Filehandles\*(R" in perldata
526and especially \*(L"Pass by Reference\*(R" in perlsub for more information.
527.IP "Passing Regexes" 4
528.IX Item "Passing Regexes"
529To pass regexes around, you'll need to be using a release of Perl
530sufficiently recent as to support the \f(CW\*(C`qr//\*(C'\fR construct, pass around
531strings and use an exception-trapping eval, or else be very, very clever.
532.Sp
533Here's an example of how to pass in a string to be regex compared
534using \f(CW\*(C`qr//\*(C'\fR:
535.Sp
536.Vb 6
537\& sub compare($$) {
538\& my ($val1, $regex) = @_;
539\& my $retval = $val1 =~ /$regex/;
540\& return $retval;
541\& }
542\& $match = compare("old McDonald", qr/d.*D/i);
543.Ve
544.Sp
545Notice how \f(CW\*(C`qr//\*(C'\fR allows flags at the end. That pattern was compiled
546at compile time, although it was executed later. The nifty \f(CW\*(C`qr//\*(C'\fR
547notation wasn't introduced until the 5.005 release. Before that, you
548had to approach this problem much less intuitively. For example, here
549it is again if you don't have \f(CW\*(C`qr//\*(C'\fR:
550.Sp
551.Vb 6
552\& sub compare($$) {
553\& my ($val1, $regex) = @_;
554\& my $retval = eval { $val1 =~ /$regex/ };
555\& die if $@;
556\& return $retval;
557\& }
558.Ve
559.Sp
560.Vb 1
561\& $match = compare("old McDonald", q/($?i)d.*D/);
562.Ve
563.Sp
564Make sure you never say something like this:
565.Sp
566.Vb 1
567\& return eval "\e$val =~ /$regex/"; # WRONG
568.Ve
569.Sp
570or someone can sneak shell escapes into the regex due to the double
571interpolation of the eval and the double-quoted string. For example:
572.Sp
573.Vb 1
574\& $pattern_of_evil = 'danger ${ system("rm -rf * &") } danger';
575.Ve
576.Sp
577.Vb 1
578\& eval "\e$string =~ /$pattern_of_evil/";
579.Ve
580.Sp
581Those preferring to be very, very clever might see the O'Reilly book,
582\&\fIMastering Regular Expressions\fR, by Jeffrey Friedl. Page 273's
583\&\fIBuild_MatchMany_Function()\fR is particularly interesting. A complete
584citation of this book is given in perlfaq2.
585.IP "Passing Methods" 4
586.IX Item "Passing Methods"
587To pass an object method into a subroutine, you can do this:
588.Sp
589.Vb 7
590\& call_a_lot(10, $some_obj, "methname")
591\& sub call_a_lot {
592\& my ($count, $widget, $trick) = @_;
593\& for (my $i = 0; $i < $count; $i++) {
594\& $widget->$trick();
595\& }
596\& }
597.Ve
598.Sp
599Or, you can use a closure to bundle up the object, its
600method call, and arguments:
601.Sp
602.Vb 6
603\& my $whatnot = sub { $some_obj->obfuscate(@args) };
604\& func($whatnot);
605\& sub func {
606\& my $code = shift;
607\& &$code();
608\& }
609.Ve
610.Sp
611You could also investigate the \fIcan()\fR method in the \s-1UNIVERSAL\s0 class
612(part of the standard perl distribution).
613.Sh "How do I create a static variable?"
614.IX Subsection "How do I create a static variable?"
615(contributed by brian d foy)
616.PP
617Perl doesn't have \*(L"static\*(R" variables, which can only be accessed from
618the function in which they are declared. You can get the same effect
619with lexical variables, though.
620.PP
621You can fake a static variable by using a lexical variable which goes
622out of scope. In this example, you define the subroutine \f(CW\*(C`counter\*(C'\fR, and
623it uses the lexical variable \f(CW$count\fR. Since you wrap this in a \s-1BEGIN\s0
624block, \f(CW$count\fR is defined at compile\-time, but also goes out of
625scope at the end of the \s-1BEGIN\s0 block. The \s-1BEGIN\s0 block also ensures that
626the subroutine and the value it uses is defined at compile-time so the
627subroutine is ready to use just like any other subroutine, and you can
628put this code in the same place as other subroutines in the program
629text (i.e. at the end of the code, typically). The subroutine
630\&\f(CW\*(C`counter\*(C'\fR still has a reference to the data, and is the only way you
631can access the value (and each time you do, you increment the value).
632The data in chunk of memory defined by \f(CW$count\fR is private to
633\&\f(CW\*(C`counter\*(C'\fR.
634.PP
635.Vb 4
636\& BEGIN {
637\& my $count = 1;
638\& sub counter { $count++ }
639\& }
640.Ve
641.PP
642.Vb 1
643\& my $start = count();
644.Ve
645.PP
646.Vb 1
647\& .... # code that calls count();
648.Ve
649.PP
650.Vb 1
651\& my $end = count();
652.Ve
653.PP
654In the previous example, you created a function-private variable
655because only one function remembered its reference. You could define
656multiple functions while the variable is in scope, and each function
657can share the \*(L"private\*(R" variable. It's not really \*(L"static\*(R" because you
658can access it outside the function while the lexical variable is in
659scope, and even create references to it. In this example,
660\&\f(CW\*(C`increment_count\*(C'\fR and \f(CW\*(C`return_count\*(C'\fR share the variable. One
661function adds to the value and the other simply returns the value.
662They can both access \f(CW$count\fR, and since it has gone out of scope,
663there is no other way to access it.
664.PP
665.Vb 5
666\& BEGIN {
667\& my $count = 1;
668\& sub increment_count { $count++ }
669\& sub return_count { $count }
670\& }
671.Ve
672.PP
673To declare a file-private variable, you still use a lexical variable.
674A file is also a scope, so a lexical variable defined in the file
675cannot be seen from any other file.
676.PP
677See \*(L"Persistent Private Variables\*(R" in perlsub for more information.
678The discussion of closures in perlref may help you even though we
679did not use anonymous subroutines in this answer. See
680\&\*(L"Persistent Private Variables\*(R" in perlsub for details.
681.Sh "What's the difference between dynamic and lexical (static) scoping? Between \fIlocal()\fP and \fImy()\fP?"
682.IX Subsection "What's the difference between dynamic and lexical (static) scoping? Between local() and my()?"
683\&\f(CW\*(C`local($x)\*(C'\fR saves away the old value of the global variable \f(CW$x\fR
684and assigns a new value for the duration of the subroutine \fIwhich is
685visible in other functions called from that subroutine\fR. This is done
686at run\-time, so is called dynamic scoping. \fIlocal()\fR always affects global
687variables, also called package variables or dynamic variables.
688.PP
689\&\f(CW\*(C`my($x)\*(C'\fR creates a new variable that is only visible in the current
690subroutine. This is done at compile\-time, so it is called lexical or
691static scoping. \fImy()\fR always affects private variables, also called
692lexical variables or (improperly) static(ly scoped) variables.
693.PP
694For instance:
695.PP
696.Vb 3
697\& sub visible {
698\& print "var has value $var\en";
699\& }
700.Ve
701.PP
702.Vb 4
703\& sub dynamic {
704\& local $var = 'local'; # new temporary value for the still-global
705\& visible(); # variable called $var
706\& }
707.Ve
708.PP
709.Vb 4
710\& sub lexical {
711\& my $var = 'private'; # new private variable, $var
712\& visible(); # (invisible outside of sub scope)
713\& }
714.Ve
715.PP
716.Vb 1
717\& $var = 'global';
718.Ve
719.PP
720.Vb 3
721\& visible(); # prints global
722\& dynamic(); # prints local
723\& lexical(); # prints global
724.Ve
725.PP
726Notice how at no point does the value \*(L"private\*(R" get printed. That's
727because \f(CW$var\fR only has that value within the block of the \fIlexical()\fR
728function, and it is hidden from called subroutine.
729.PP
730In summary, \fIlocal()\fR doesn't make what you think of as private, local
731variables. It gives a global variable a temporary value. \fImy()\fR is
732what you're looking for if you want private variables.
733.PP
734See \*(L"Private Variables via \fImy()\fR\*(R" in perlsub and
735\&\*(L"Temporary Values via \fIlocal()\fR\*(R" in perlsub for excruciating details.
736.Sh "How can I access a dynamic variable while a similarly named lexical is in scope?"
737.IX Subsection "How can I access a dynamic variable while a similarly named lexical is in scope?"
738If you know your package, you can just mention it explicitly, as in
739\&\f(CW$Some_Pack::var\fR. Note that the notation \f(CW$::var\fR is \fBnot\fR the dynamic \f(CW$var\fR
740in the current package, but rather the one in the \*(L"main\*(R" package, as
741though you had written \f(CW$main::var\fR.
742.PP
743.Vb 3
744\& use vars '$var';
745\& local $var = "global";
746\& my $var = "lexical";
747.Ve
748.PP
749.Vb 2
750\& print "lexical is $var\en";
751\& print "global is $main::var\en";
752.Ve
753.PP
754Alternatively you can use the compiler directive \fIour()\fR to bring a
755dynamic variable into the current lexical scope.
756.PP
757.Vb 2
758\& require 5.006; # our() did not exist before 5.6
759\& use vars '$var';
760.Ve
761.PP
762.Vb 2
763\& local $var = "global";
764\& my $var = "lexical";
765.Ve
766.PP
767.Vb 1
768\& print "lexical is $var\en";
769.Ve
770.PP
771.Vb 4
772\& {
773\& our $var;
774\& print "global is $var\en";
775\& }
776.Ve
777.Sh "What's the difference between deep and shallow binding?"
778.IX Subsection "What's the difference between deep and shallow binding?"
779In deep binding, lexical variables mentioned in anonymous subroutines
780are the same ones that were in scope when the subroutine was created.
781In shallow binding, they are whichever variables with the same names
782happen to be in scope when the subroutine is called. Perl always uses
783deep binding of lexical variables (i.e., those created with \fImy()\fR).
784However, dynamic variables (aka global, local, or package variables)
785are effectively shallowly bound. Consider this just one more reason
786not to use them. See the answer to \*(L"What's a closure?\*(R".
787.ie n .Sh "Why doesn't ""my($foo) = <\s-1FILE\s0>;"" work right?"
788.el .Sh "Why doesn't ``my($foo) = <\s-1FILE\s0>;'' work right?"
789.IX Subsection "Why doesn't ""my($foo) = <FILE>;"" work right?"
790\&\f(CW\*(C`my()\*(C'\fR and \f(CW\*(C`local()\*(C'\fR give list context to the right hand side
791of \f(CW\*(C`=\*(C'\fR. The <\s-1FH\s0> read operation, like so many of Perl's
792functions and operators, can tell which context it was called in and
793behaves appropriately. In general, the \fIscalar()\fR function can help.
794This function does nothing to the data itself (contrary to popular myth)
795but rather tells its argument to behave in whatever its scalar fashion is.
796If that function doesn't have a defined scalar behavior, this of course
797doesn't help you (such as with \fIsort()\fR).
798.PP
799To enforce scalar context in this particular case, however, you need
800merely omit the parentheses:
801.PP
802.Vb 3
803\& local($foo) = <FILE>; # WRONG
804\& local($foo) = scalar(<FILE>); # ok
805\& local $foo = <FILE>; # right
806.Ve
807.PP
808You should probably be using lexical variables anyway, although the
809issue is the same here:
810.PP
811.Vb 2
812\& my($foo) = <FILE>; # WRONG
813\& my $foo = <FILE>; # right
814.Ve
815.Sh "How do I redefine a builtin function, operator, or method?"
816.IX Subsection "How do I redefine a builtin function, operator, or method?"
817Why do you want to do that? :\-)
818.PP
819If you want to override a predefined function, such as \fIopen()\fR,
820then you'll have to import the new definition from a different
821module. See \*(L"Overriding Built-in Functions\*(R" in perlsub. There's
822also an example in \*(L"Class::Template\*(R" in perltoot.
823.PP
824If you want to overload a Perl operator, such as \f(CW\*(C`+\*(C'\fR or \f(CW\*(C`**\*(C'\fR,
825then you'll want to use the \f(CW\*(C`use overload\*(C'\fR pragma, documented
826in overload.
827.PP
828If you're talking about obscuring method calls in parent classes,
829see \*(L"Overridden Methods\*(R" in perltoot.
830.Sh "What's the difference between calling a function as &foo and \fIfoo()\fP?"
831.IX Subsection "What's the difference between calling a function as &foo and foo()?"
832When you call a function as \f(CW&foo\fR, you allow that function access to
833your current \f(CW@_\fR values, and you bypass prototypes.
834The function doesn't get an empty \f(CW@_\fR\-\-it gets yours! While not
835strictly speaking a bug (it's documented that way in perlsub), it
836would be hard to consider this a feature in most cases.
837.PP
838When you call your function as \f(CW\*(C`&foo()\*(C'\fR, then you \fIdo\fR get a new \f(CW@_\fR,
839but prototyping is still circumvented.
840.PP
841Normally, you want to call a function using \f(CW\*(C`foo()\*(C'\fR. You may only
842omit the parentheses if the function is already known to the compiler
843because it already saw the definition (\f(CW\*(C`use\*(C'\fR but not \f(CW\*(C`require\*(C'\fR),
844or via a forward reference or \f(CW\*(C`use subs\*(C'\fR declaration. Even in this
845case, you get a clean \f(CW@_\fR without any of the old values leaking through
846where they don't belong.
847.Sh "How do I create a switch or case statement?"
848.IX Subsection "How do I create a switch or case statement?"
849This is explained in more depth in the perlsyn. Briefly, there's
850no official case statement, because of the variety of tests possible
851in Perl (numeric comparison, string comparison, glob comparison,
852regex matching, overloaded comparisons, ...).
853Larry couldn't decide how best to do this, so he left it out, even
854though it's been on the wish list since perl1.
855.PP
856Starting from Perl 5.8 to get switch and case one can use the
857Switch extension and say:
858.PP
859.Vb 1
860\& use Switch;
861.Ve
862.PP
863after which one has switch and case. It is not as fast as it could be
864because it's not really part of the language (it's done using source
865filters) but it is available, and it's very flexible.
866.PP
867But if one wants to use pure Perl, the general answer is to write a
868construct like this:
869.PP
870.Vb 6
871\& for ($variable_to_test) {
872\& if (/pat1/) { } # do something
873\& elsif (/pat2/) { } # do something else
874\& elsif (/pat3/) { } # do something else
875\& else { } # default
876\& }
877.Ve
878.PP
879Here's a simple example of a switch based on pattern matching, this
880time lined up in a way to make it look more like a switch statement.
881We'll do a multiway conditional based on the type of reference stored
882in \f(CW$whatchamacallit:\fR
883.PP
884.Vb 1
885\& SWITCH: for (ref $whatchamacallit) {
886.Ve
887.PP
888.Vb 1
889\& /^$/ && die "not a reference";
890.Ve
891.PP
892.Vb 4
893\& /SCALAR/ && do {
894\& print_scalar($$ref);
895\& last SWITCH;
896\& };
897.Ve
898.PP
899.Vb 4
900\& /ARRAY/ && do {
901\& print_array(@$ref);
902\& last SWITCH;
903\& };
904.Ve
905.PP
906.Vb 4
907\& /HASH/ && do {
908\& print_hash(%$ref);
909\& last SWITCH;
910\& };
911.Ve
912.PP
913.Vb 4
914\& /CODE/ && do {
915\& warn "can't print function ref";
916\& last SWITCH;
917\& };
918.Ve
919.PP
920.Vb 1
921\& # DEFAULT
922.Ve
923.PP
924.Vb 1
925\& warn "User defined type skipped";
926.Ve
927.PP
928.Vb 1
929\& }
930.Ve
931.PP
932See \f(CW\*(C`perlsyn/"Basic BLOCKs and Switch Statements"\*(C'\fR for many other
933examples in this style.
934.PP
935Sometimes you should change the positions of the constant and the variable.
936For example, let's say you wanted to test which of many answers you were
937given, but in a case-insensitive way that also allows abbreviations.
938You can use the following technique if the strings all start with
939different characters or if you want to arrange the matches so that
940one takes precedence over another, as \f(CW"SEND"\fR has precedence over
941\&\f(CW"STOP"\fR here:
942.PP
943.Vb 6
944\& chomp($answer = <>);
945\& if ("SEND" =~ /^\eQ$answer/i) { print "Action is send\en" }
946\& elsif ("STOP" =~ /^\eQ$answer/i) { print "Action is stop\en" }
947\& elsif ("ABORT" =~ /^\eQ$answer/i) { print "Action is abort\en" }
948\& elsif ("LIST" =~ /^\eQ$answer/i) { print "Action is list\en" }
949\& elsif ("EDIT" =~ /^\eQ$answer/i) { print "Action is edit\en" }
950.Ve
951.PP
952A totally different approach is to create a hash of function references.
953.PP
954.Vb 6
955\& my %commands = (
956\& "happy" => \e&joy,
957\& "sad", => \e&sullen,
958\& "done" => sub { die "See ya!" },
959\& "mad" => \e&angry,
960\& );
961.Ve
962.PP
963.Vb 7
964\& print "How are you? ";
965\& chomp($string = <STDIN>);
966\& if ($commands{$string}) {
967\& $commands{$string}->();
968\& } else {
969\& print "No such command: $string\en";
970\& }
971.Ve
972.Sh "How can I catch accesses to undefined variables, functions, or methods?"
973.IX Subsection "How can I catch accesses to undefined variables, functions, or methods?"
974The \s-1AUTOLOAD\s0 method, discussed in \*(L"Autoloading\*(R" in perlsub and
975\&\*(L"\s-1AUTOLOAD:\s0 Proxy Methods\*(R" in perltoot, lets you capture calls to
976undefined functions and methods.
977.PP
978When it comes to undefined variables that would trigger a warning
979under \f(CW\*(C`use warnings\*(C'\fR, you can promote the warning to an error.
980.PP
981.Vb 1
982\& use warnings FATAL => qw(uninitialized);
983.Ve
984.Sh "Why can't a method included in this same file be found?"
985.IX Subsection "Why can't a method included in this same file be found?"
986Some possible reasons: your inheritance is getting confused, you've
987misspelled the method name, or the object is of the wrong type. Check
988out perltoot for details about any of the above cases. You may
989also use \f(CW\*(C`print ref($object)\*(C'\fR to find out the class \f(CW$object\fR was
990blessed into.
991.PP
992Another possible reason for problems is because you've used the
993indirect object syntax (eg, \f(CW\*(C`find Guru "Samy"\*(C'\fR) on a class name
994before Perl has seen that such a package exists. It's wisest to make
995sure your packages are all defined before you start using them, which
996will be taken care of if you use the \f(CW\*(C`use\*(C'\fR statement instead of
997\&\f(CW\*(C`require\*(C'\fR. If not, make sure to use arrow notation (eg.,
998\&\f(CW\*(C`Guru\->find("Samy")\*(C'\fR) instead. Object notation is explained in
999perlobj.
1000.PP
1001Make sure to read about creating modules in perlmod and
1002the perils of indirect objects in \*(L"Method Invocation\*(R" in perlobj.
1003.Sh "How can I find out my current package?"
1004.IX Subsection "How can I find out my current package?"
1005If you're just a random program, you can do this to find
1006out what the currently compiled package is:
1007.PP
1008.Vb 1
1009\& my $packname = __PACKAGE__;
1010.Ve
1011.PP
1012But, if you're a method and you want to print an error message
1013that includes the kind of object you were called on (which is
1014not necessarily the same as the one in which you were compiled):
1015.PP
1016.Vb 5
1017\& sub amethod {
1018\& my $self = shift;
1019\& my $class = ref($self) || $self;
1020\& warn "called me from a $class object";
1021\& }
1022.Ve
1023.Sh "How can I comment out a large block of perl code?"
1024.IX Subsection "How can I comment out a large block of perl code?"
1025You can use embedded \s-1POD\s0 to discard it. Enclose the blocks you want
1026to comment out in \s-1POD\s0 markers. The <=begin> directive marks a section
1027for a specific formatter. Use the \f(CW\*(C`comment\*(C'\fR format, which no formatter
1028should claim to understand (by policy). Mark the end of the block
1029with <=end>.
1030.PP
1031.Vb 1
1032\& # program is here
1033.Ve
1034.PP
1035.Vb 1
1036\& =begin comment
1037.Ve
1038.PP
1039.Vb 1
1040\& all of this stuff
1041.Ve
1042.PP
1043.Vb 2
1044\& here will be ignored
1045\& by everyone
1046.Ve
1047.PP
1048.Vb 1
1049\& =end comment
1050.Ve
1051.PP
1052.Vb 1
1053\& =cut
1054.Ve
1055.PP
1056.Vb 1
1057\& # program continues
1058.Ve
1059.PP
1060The pod directives cannot go just anywhere. You must put a
1061pod directive where the parser is expecting a new statement,
1062not just in the middle of an expression or some other
1063arbitrary grammar production.
1064.PP
1065See perlpod for more details.
1066.Sh "How do I clear a package?"
1067.IX Subsection "How do I clear a package?"
1068Use this code, provided by Mark-Jason Dominus:
1069.PP
1070.Vb 17
1071\& sub scrub_package {
1072\& no strict 'refs';
1073\& my $pack = shift;
1074\& die "Shouldn't delete main package"
1075\& if $pack eq "" || $pack eq "main";
1076\& my $stash = *{$pack . '::'}{HASH};
1077\& my $name;
1078\& foreach $name (keys %$stash) {
1079\& my $fullname = $pack . '::' . $name;
1080\& # Get rid of everything with that name.
1081\& undef $$fullname;
1082\& undef @$fullname;
1083\& undef %$fullname;
1084\& undef &$fullname;
1085\& undef *$fullname;
1086\& }
1087\& }
1088.Ve
1089.PP
1090Or, if you're using a recent release of Perl, you can
1091just use the \fISymbol::delete_package()\fR function instead.
1092.Sh "How can I use a variable as a variable name?"
1093.IX Subsection "How can I use a variable as a variable name?"
1094Beginners often think they want to have a variable contain the name
1095of a variable.
1096.PP
1097.Vb 3
1098\& $fred = 23;
1099\& $varname = "fred";
1100\& ++$$varname; # $fred now 24
1101.Ve
1102.PP
1103This works \fIsometimes\fR, but it is a very bad idea for two reasons.
1104.PP
1105The first reason is that this technique \fIonly works on global
1106variables\fR. That means that if \f(CW$fred\fR is a lexical variable created
1107with \fImy()\fR in the above example, the code wouldn't work at all: you'd
1108accidentally access the global and skip right over the private lexical
1109altogether. Global variables are bad because they can easily collide
1110accidentally and in general make for non-scalable and confusing code.
1111.PP
1112Symbolic references are forbidden under the \f(CW\*(C`use strict\*(C'\fR pragma.
1113They are not true references and consequently are not reference counted
1114or garbage collected.
1115.PP
1116The other reason why using a variable to hold the name of another
1117variable is a bad idea is that the question often stems from a lack of
1118understanding of Perl data structures, particularly hashes. By using
1119symbolic references, you are just using the package's symbol-table hash
1120(like \f(CW%main::\fR) instead of a user-defined hash. The solution is to
1121use your own hash or a real reference instead.
1122.PP
1123.Vb 3
1124\& $USER_VARS{"fred"} = 23;
1125\& $varname = "fred";
1126\& $USER_VARS{$varname}++; # not $$varname++
1127.Ve
1128.PP
1129There we're using the \f(CW%USER_VARS\fR hash instead of symbolic references.
1130Sometimes this comes up in reading strings from the user with variable
1131references and wanting to expand them to the values of your perl
1132program's variables. This is also a bad idea because it conflates the
1133program-addressable namespace and the user-addressable one. Instead of
1134reading a string and expanding it to the actual contents of your program's
1135own variables:
1136.PP
1137.Vb 2
1138\& $str = 'this has a $fred and $barney in it';
1139\& $str =~ s/(\e$\ew+)/$1/eeg; # need double eval
1140.Ve
1141.PP
1142it would be better to keep a hash around like \f(CW%USER_VARS\fR and have
1143variable references actually refer to entries in that hash:
1144.PP
1145.Vb 1
1146\& $str =~ s/\e$(\ew+)/$USER_VARS{$1}/g; # no /e here at all
1147.Ve
1148.PP
1149That's faster, cleaner, and safer than the previous approach. Of course,
1150you don't need to use a dollar sign. You could use your own scheme to
1151make it less confusing, like bracketed percent symbols, etc.
1152.PP
1153.Vb 2
1154\& $str = 'this has a %fred% and %barney% in it';
1155\& $str =~ s/%(\ew+)%/$USER_VARS{$1}/g; # no /e here at all
1156.Ve
1157.PP
1158Another reason that folks sometimes think they want a variable to
1159contain the name of a variable is because they don't know how to build
1160proper data structures using hashes. For example, let's say they
1161wanted two hashes in their program: \f(CW%fred\fR and \f(CW%barney\fR, and that they
1162wanted to use another scalar variable to refer to those by name.
1163.PP
1164.Vb 2
1165\& $name = "fred";
1166\& $$name{WIFE} = "wilma"; # set %fred
1167.Ve
1168.PP
1169.Vb 2
1170\& $name = "barney";
1171\& $$name{WIFE} = "betty"; # set %barney
1172.Ve
1173.PP
1174This is still a symbolic reference, and is still saddled with the
1175problems enumerated above. It would be far better to write:
1176.PP
1177.Vb 2
1178\& $folks{"fred"}{WIFE} = "wilma";
1179\& $folks{"barney"}{WIFE} = "betty";
1180.Ve
1181.PP
1182And just use a multilevel hash to start with.
1183.PP
1184The only times that you absolutely \fImust\fR use symbolic references are
1185when you really must refer to the symbol table. This may be because it's
1186something that can't take a real reference to, such as a format name.
1187Doing so may also be important for method calls, since these always go
1188through the symbol table for resolution.
1189.PP
1190In those cases, you would turn off \f(CW\*(C`strict 'refs'\*(C'\fR temporarily so you
1191can play around with the symbol table. For example:
1192.PP
1193.Vb 5
1194\& @colors = qw(red blue green yellow orange purple violet);
1195\& for my $name (@colors) {
1196\& no strict 'refs'; # renege for the block
1197\& *$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
1198\& }
1199.Ve
1200.PP
1201All those functions (\fIred()\fR, \fIblue()\fR, \fIgreen()\fR, etc.) appear to be separate,
1202but the real code in the closure actually was compiled only once.
1203.PP
1204So, sometimes you might want to use symbolic references to directly
1205manipulate the symbol table. This doesn't matter for formats, handles, and
1206subroutines, because they are always global\*(--you can't use \fImy()\fR on them.
1207For scalars, arrays, and hashes, though\*(--and usually for subroutines\*(--
1208you probably only want to use hard references.
1209.ie n .Sh "What does ""bad interpreter"" mean?"
1210.el .Sh "What does ``bad interpreter'' mean?"
1211.IX Subsection "What does bad interpreter mean?"
1212(contributed by brian d foy)
1213.PP
1214The \*(L"bad interpreter\*(R" message comes from the shell, not perl. The
1215actual message may vary depending on your platform, shell, and locale
1216settings.
1217.PP
1218If you see \*(L"bad interpreter \- no such file or directory\*(R", the first
1219line in your perl script (the \*(L"shebang\*(R" line) does not contain the
1220right path to perl (or any other program capable of running scripts).
1221Sometimes this happens when you move the script from one machine to
1222another and each machine has a different path to perl\-\-\-/usr/bin/perl
1223versus /usr/local/bin/perl for instance. It may also indicate
1224that the source machine has \s-1CRLF\s0 line terminators and the
1225destination machine has \s-1LF\s0 only: the shell tries to find
1226/usr/bin/perl<\s-1CR\s0>, but can't.
1227.PP
1228If you see \*(L"bad interpreter: Permission denied\*(R", you need to make your
1229script executable.
1230.PP
1231In either case, you should still be able to run the scripts with perl
1232explicitly:
1233.PP
1234.Vb 1
1235\& % perl script.pl
1236.Ve
1237.PP
1238If you get a message like \*(L"perl: command not found\*(R", perl is not in
1239your \s-1PATH\s0, which might also mean that the location of perl is not
1240where you expect it so you need to adjust your shebang line.
1241.SH "AUTHOR AND COPYRIGHT"
1242.IX Header "AUTHOR AND COPYRIGHT"
1243Copyright (c) 1997\-2006 Tom Christiansen, Nathan Torkington, and
1244other authors as noted. All rights reserved.
1245.PP
1246This documentation is free; you can redistribute it and/or modify it
1247under the same terms as Perl itself.
1248.PP
1249Irrespective of its distribution, all code examples in this file
1250are hereby placed into the public domain. You are permitted and
1251encouraged to use this code in your own programs for fun
1252or for profit as you see fit. A simple comment in the code giving
1253credit would be courteous but is not required.