Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / man / man1 / perlfaq7.1
.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. | will give a
.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.\"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "PERLFAQ7 1"
.TH PERLFAQ7 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide"
.SH "NAME"
perlfaq7 \- General Perl Language Issues ($Revision: 1.28 $, $Date: 2005/12/31 00:54:37 $)
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This section deals with general Perl language issues that don't
clearly fit into any of the other sections.
.Sh "Can I get a BNF/yacc/RE for the Perl language?"
.IX Subsection "Can I get a BNF/yacc/RE for the Perl language?"
There is no \s-1BNF\s0, but you can paw your way through the yacc grammar in
perly.y in the source distribution if you're particularly brave. The
grammar relies on very smart tokenizing code, so be prepared to
venture into toke.c as well.
.PP
In the words of Chaim Frenkel: \*(L"Perl's grammar can not be reduced to \s-1BNF\s0.
The work of parsing perl is distributed between yacc, the lexer, smoke
and mirrors.\*(R"
.Sh "What are all these $@%&* punctuation signs, and how do I know when to use them?"
.IX Subsection "What are all these $@%&* punctuation signs, and how do I know when to use them?"
They are type specifiers, as detailed in perldata:
.PP
.Vb 6
\& $ for scalar values (number, string or reference)
\& @ for arrays
\& % for hashes (associative arrays)
\& & for subroutines (aka functions, procedures, methods)
\& * for all types of that symbol name. In version 4 you used them like
\& pointers, but in modern perls you can just use references.
.Ve
.PP
There are couple of other symbols that you're likely to encounter that aren't
really type specifiers:
.PP
.Vb 2
\& <> are used for inputting a record from a filehandle.
\& \e takes a reference to something.
.Ve
.PP
Note that <\s-1FILE\s0> is \fIneither\fR the type specifier for files
nor the name of the handle. It is the \f(CW\*(C`<>\*(C'\fR operator applied
to the handle \s-1FILE\s0. It reads one line (well, record\*(--see
"$/" in perlvar) from the handle \s-1FILE\s0 in scalar context, or \fIall\fR lines
in list context. When performing open, close, or any other operation
besides \f(CW\*(C`<>\*(C'\fR on files, or even when talking about the handle, do
\&\fInot\fR use the brackets. These are correct: \f(CW\*(C`eof(FH)\*(C'\fR, \f(CW\*(C`seek(FH, 0,
2)\*(C'\fR and \*(L"copying from \s-1STDIN\s0 to \s-1FILE\s0\*(R".
.Sh "Do I always/never have to quote my strings or use semicolons and commas?"
.IX Subsection "Do I always/never have to quote my strings or use semicolons and commas?"
Normally, a bareword doesn't need to be quoted, but in most cases
probably should be (and must be under \f(CW\*(C`use strict\*(C'\fR). But a hash key
consisting of a simple word (that isn't the name of a defined
subroutine) and the left-hand operand to the \f(CW\*(C`=>\*(C'\fR operator both
count as though they were quoted:
.PP
.Vb 4
\& This is like this
\& ------------ ---------------
\& $foo{line} $foo{'line'}
\& bar => stuff 'bar' => stuff
.Ve
.PP
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:
.PP
.Vb 2
\& if ($whoops) { exit 1 }
\& @nums = (1, 2, 3);
.Ve
.PP
.Vb 7
\& if ($whoops) {
\& exit 1;
\& }
\& @lines = (
\& "There Beren came from mountains cold",
\& "And lost he wandered under leaves",
\& );
.Ve
.Sh "How do I skip some return values?"
.IX Subsection "How do I skip some return values?"
One way is to treat the return values as a list and index into it:
.PP
.Vb 1
\& $dir = (getpwnam($user))[7];
.Ve
.PP
Another way is to use undef as an element on the left\-hand\-side:
.PP
.Vb 1
\& ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
.Ve
.PP
You can also use a list slice to select only the elements that
you need:
.PP
.Vb 1
\& ($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5];
.Ve
.Sh "How do I temporarily block warnings?"
.IX Subsection "How do I temporarily block warnings?"
If you are running Perl 5.6.0 or better, the \f(CW\*(C`use warnings\*(C'\fR pragma
allows fine control of what warning are produced.
See perllexwarn for more details.
.PP
.Vb 4
\& {
\& no warnings; # temporarily turn off warnings
\& $a = $b + $c; # I know these might be undef
\& }
.Ve
.PP
Additionally, you can enable and disable categories of warnings.
You turn off the categories you want to ignore and you can still
get other categories of warnings. See perllexwarn for the
complete details, including the category names and hierarchy.
.PP
.Vb 4
\& {
\& no warnings 'uninitialized';
\& $a = $b + $c;
\& }
.Ve
.PP
If you have an older version of Perl, the \f(CW$^W\fR variable (documented
in perlvar) controls runtime warnings for a block:
.PP
.Vb 4
\& {
\& local $^W = 0; # temporarily turn off warnings
\& $a = $b + $c; # I know these might be undef
\& }
.Ve
.PP
Note that like all the punctuation variables, you cannot currently
use \fImy()\fR on \f(CW$^W\fR, only \fIlocal()\fR.
.Sh "What's an extension?"
.IX Subsection "What's an extension?"
An extension is a way of calling compiled C code from Perl. Reading
perlxstut is a good place to learn more about extensions.
.Sh "Why do Perl operators have different precedence than C operators?"
.IX Subsection "Why do Perl operators have different precedence than C operators?"
Actually, they don't. All C operators that Perl copies have the same
precedence in Perl as they do in C. The problem is with operators that C
doesn't have, especially functions that give a list context to everything
on their right, eg. print, chmod, exec, and so on. Such functions are
called \*(L"list operators\*(R" and appear as such in the precedence table in
perlop.
.PP
A common mistake is to write:
.PP
.Vb 1
\& unlink $file || die "snafu";
.Ve
.PP
This gets interpreted as:
.PP
.Vb 1
\& unlink ($file || die "snafu");
.Ve
.PP
To avoid this problem, either put in extra parentheses or use the
super low precedence \f(CW\*(C`or\*(C'\fR operator:
.PP
.Vb 2
\& (unlink $file) || die "snafu";
\& unlink $file or die "snafu";
.Ve
.PP
The \*(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)
deliberately have precedence lower than that of list operators for
just such situations as the one above.
.PP
Another operator with surprising precedence is exponentiation. It
binds more tightly even than unary minus, making \f(CW\*(C`\-2**2\*(C'\fR product a
negative not a positive four. It is also right\-associating, meaning
that \f(CW\*(C`2**3**2\*(C'\fR is two raised to the ninth power, not eight squared.
.PP
Although it has the same precedence as in C, Perl's \f(CW\*(C`?:\*(C'\fR operator
produces an lvalue. This assigns \f(CW$x\fR to either \f(CW$a\fR or \f(CW$b\fR, depending
on the trueness of \f(CW$maybe:\fR
.PP
.Vb 1
\& ($maybe ? $a : $b) = $x;
.Ve
.Sh "How do I declare/create a structure?"
.IX Subsection "How do I declare/create a structure?"
In general, you don't \*(L"declare\*(R" a structure. Just use a (probably
anonymous) hash reference. See perlref and perldsc for details.
Here's an example:
.PP
.Vb 3
\& $person = {}; # new anonymous hash
\& $person->{AGE} = 24; # set field AGE to 24
\& $person->{NAME} = "Nat"; # set field NAME to "Nat"
.Ve
.PP
If you're looking for something a bit more rigorous, try perltoot.
.Sh "How do I create a module?"
.IX Subsection "How do I create a module?"
(contributed by brian d foy)
.PP
perlmod, perlmodlib, perlmodstyle explain modules
in all the gory details. perlnewmod gives a brief
overview of the process along with a couple of suggestions
about style.
.PP
If you need to include C code or C library interfaces in
your module, you'll need h2xs. h2xs will create the module
distribution structure and the initial interface files
you'll need. perlxs and perlxstut explain the details.
.PP
If you don't need to use C code, other tools such as
ExtUtils::ModuleMaker and Module::Starter, can help you
create a skeleton module distribution.
.PP
You may also want to see Sam Tregar's \*(L"Writing Perl Modules
for \s-1CPAN\s0\*(R" ( http://apress.com/book/bookDisplay.html?bID=14 )
which is the best hands-on guide to creating module
distributions.
.Sh "How do I create a class?"
.IX Subsection "How do I create a class?"
See perltoot for an introduction to classes and objects, as well as
perlobj and perlbot.
.Sh "How can I tell if a variable is tainted?"
.IX Subsection "How can I tell if a variable is tainted?"
You can use the \fItainted()\fR function of the Scalar::Util module, available
from \s-1CPAN\s0 (or included with Perl since release 5.8.0).
See also \*(L"Laundering and Detecting Tainted Data\*(R" in perlsec.
.Sh "What's a closure?"
.IX Subsection "What's a closure?"
Closures are documented in perlref.
.PP
\&\fIClosure\fR is a computer science term with a precise but
hard-to-explain meaning. Closures are implemented in Perl as anonymous
subroutines with lasting references to lexical variables outside their
own scopes. These lexicals magically refer to the variables that were
around when the subroutine was defined (deep binding).
.PP
Closures make sense in any programming language where you can have the
return value of a function be itself a function, as you can in Perl.
Note that some languages provide anonymous functions but are not
capable of providing proper closures: the Python language, for
example. For more information on closures, check out any textbook on
functional programming. Scheme is a language that not only supports
but encourages closures.
.PP
Here's a classic function-generating function:
.PP
.Vb 3
\& sub add_function_generator {
\& return sub { shift() + shift() };
\& }
.Ve
.PP
.Vb 2
\& $add_sub = add_function_generator();
\& $sum = $add_sub->(4,5); # $sum is 9 now.
.Ve
.PP
The closure works as a \fIfunction template\fR with some customization
slots left out to be filled later. The anonymous subroutine returned
by \fIadd_function_generator()\fR isn't technically a closure because it
refers to no lexicals outside its own scope.
.PP
Contrast this with the following \fImake_adder()\fR function, in which the
returned anonymous function contains a reference to a lexical variable
outside the scope of that function itself. Such a reference requires
that Perl return a proper closure, thus locking in for all time the
value that the lexical had when the function was created.
.PP
.Vb 4
\& sub make_adder {
\& my $addpiece = shift;
\& return sub { shift() + $addpiece };
\& }
.Ve
.PP
.Vb 2
\& $f1 = make_adder(20);
\& $f2 = make_adder(555);
.Ve
.PP
Now \f(CW\*(C`&$f1($n)\*(C'\fR is always 20 plus whatever \f(CW$n\fR you pass in, whereas
\&\f(CW\*(C`&$f2($n)\*(C'\fR is always 555 plus whatever \f(CW$n\fR you pass in. The \f(CW$addpiece\fR
in the closure sticks around.
.PP
Closures are often used for less esoteric purposes. For example, when
you want to pass in a bit of code into a function:
.PP
.Vb 2
\& my $line;
\& timeout( 30, sub { $line = <STDIN> } );
.Ve
.PP
If the code to execute had been passed in as a string,
\&\f(CW'$line = <STDIN>'\fR, there would have been no way for the
hypothetical \fItimeout()\fR function to access the lexical variable
\&\f(CW$line\fR back in its caller's scope.
.Sh "What is variable suicide and how can I prevent it?"
.IX Subsection "What is variable suicide and how can I prevent it?"
This problem was fixed in perl 5.004_05, so preventing it means upgrading
your version of perl. ;)
.PP
Variable suicide is when you (temporarily or permanently) lose the value
of a variable. It is caused by scoping through \fImy()\fR and \fIlocal()\fR
interacting with either closures or aliased \fIforeach()\fR iterator variables
and subroutine arguments. It used to be easy to inadvertently lose a
variable's value this way, but now it's much harder. Take this code:
.PP
.Vb 6
\& my $f = 'foo';
\& sub T {
\& while ($i++ < 3) { my $f = $f; $f .= $i; print $f, "\en" }
\& }
\& T;
\& print "Finally $f\en";
.Ve
.PP
If you are experiencing variable suicide, that \f(CW\*(C`my $f\*(C'\fR in the subroutine
doesn't pick up a fresh copy of the \f(CW$f\fR whose value is <foo>. The output
shows that inside the subroutine the value of \f(CW$f\fR leaks through when it
shouldn't, as in this output:
.PP
.Vb 4
\& foobar
\& foobarbar
\& foobarbarbar
\& Finally foo
.Ve
.PP
The \f(CW$f\fR that has \*(L"bar\*(R" added to it three times should be a new \f(CW$f\fR
\&\f(CW\*(C`my $f\*(C'\fR should create a new lexical variable each time through the loop.
The expected output is:
.PP
.Vb 4
\& foobar
\& foobar
\& foobar
\& Finally foo
.Ve
.Sh "How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?"
.IX Subsection "How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?"
With the exception of regexes, you need to pass references to these
objects. See \*(L"Pass by Reference\*(R" in perlsub for this particular
question, and perlref for information on references.
.PP
See \*(L"Passing Regexes\*(R", below, for information on passing regular
expressions.
.IP "Passing Variables and Functions" 4
.IX Item "Passing Variables and Functions"
Regular variables and functions are quite easy to pass: just pass in a
reference to an existing or anonymous variable or function:
.Sp
.Vb 1
\& func( \e$some_scalar );
.Ve
.Sp
.Vb 2
\& func( \e@some_array );
\& func( [ 1 .. 10 ] );
.Ve
.Sp
.Vb 2
\& func( \e%some_hash );
\& func( { this => 10, that => 20 } );
.Ve
.Sp
.Vb 2
\& func( \e&some_func );
\& func( sub { $_[0] ** $_[1] } );
.Ve
.IP "Passing Filehandles" 4
.IX Item "Passing Filehandles"
As of Perl 5.6, you can represent filehandles with scalar variables
which you treat as any other scalar.
.Sp
.Vb 2
\& open my $fh, $filename or die "Cannot open $filename! $!";
\& func( $fh );
.Ve
.Sp
.Vb 2
\& sub func {
\& my $passed_fh = shift;
.Ve
.Sp
.Vb 2
\& my $line = <$fh>;
\& }
.Ve
.Sp
Before Perl 5.6, you had to use the \f(CW*FH\fR or \f(CW\*(C`\e*FH\*(C'\fR notations.
These are \*(L"typeglobs\*(R"\-\-see \*(L"Typeglobs and Filehandles\*(R" in perldata
and especially \*(L"Pass by Reference\*(R" in perlsub for more information.
.IP "Passing Regexes" 4
.IX Item "Passing Regexes"
To pass regexes around, you'll need to be using a release of Perl
sufficiently recent as to support the \f(CW\*(C`qr//\*(C'\fR construct, pass around
strings and use an exception-trapping eval, or else be very, very clever.
.Sp
Here's an example of how to pass in a string to be regex compared
using \f(CW\*(C`qr//\*(C'\fR:
.Sp
.Vb 6
\& sub compare($$) {
\& my ($val1, $regex) = @_;
\& my $retval = $val1 =~ /$regex/;
\& return $retval;
\& }
\& $match = compare("old McDonald", qr/d.*D/i);
.Ve
.Sp
Notice how \f(CW\*(C`qr//\*(C'\fR allows flags at the end. That pattern was compiled
at compile time, although it was executed later. The nifty \f(CW\*(C`qr//\*(C'\fR
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 \f(CW\*(C`qr//\*(C'\fR:
.Sp
.Vb 6
\& sub compare($$) {
\& my ($val1, $regex) = @_;
\& my $retval = eval { $val1 =~ /$regex/ };
\& die if $@;
\& return $retval;
\& }
.Ve
.Sp
.Vb 1
\& $match = compare("old McDonald", q/($?i)d.*D/);
.Ve
.Sp
Make sure you never say something like this:
.Sp
.Vb 1
\& return eval "\e$val =~ /$regex/"; # WRONG
.Ve
.Sp
or someone can sneak shell escapes into the regex due to the double
interpolation of the eval and the double-quoted string. For example:
.Sp
.Vb 1
\& $pattern_of_evil = 'danger ${ system("rm -rf * &") } danger';
.Ve
.Sp
.Vb 1
\& eval "\e$string =~ /$pattern_of_evil/";
.Ve
.Sp
Those preferring to be very, very clever might see the O'Reilly book,
\&\fIMastering Regular Expressions\fR, by Jeffrey Friedl. Page 273's
\&\fIBuild_MatchMany_Function()\fR is particularly interesting. A complete
citation of this book is given in perlfaq2.
.IP "Passing Methods" 4
.IX Item "Passing Methods"
To pass an object method into a subroutine, you can do this:
.Sp
.Vb 7
\& call_a_lot(10, $some_obj, "methname")
\& sub call_a_lot {
\& my ($count, $widget, $trick) = @_;
\& for (my $i = 0; $i < $count; $i++) {
\& $widget->$trick();
\& }
\& }
.Ve
.Sp
Or, you can use a closure to bundle up the object, its
method call, and arguments:
.Sp
.Vb 6
\& my $whatnot = sub { $some_obj->obfuscate(@args) };
\& func($whatnot);
\& sub func {
\& my $code = shift;
\& &$code();
\& }
.Ve
.Sp
You could also investigate the \fIcan()\fR method in the \s-1UNIVERSAL\s0 class
(part of the standard perl distribution).
.Sh "How do I create a static variable?"
.IX Subsection "How do I create a static variable?"
(contributed by brian d foy)
.PP
Perl doesn't have \*(L"static\*(R" variables, which can only be accessed from
the function in which they are declared. You can get the same effect
with lexical variables, though.
.PP
You can fake a static variable by using a lexical variable which goes
out of scope. In this example, you define the subroutine \f(CW\*(C`counter\*(C'\fR, and
it uses the lexical variable \f(CW$count\fR. Since you wrap this in a \s-1BEGIN\s0
block, \f(CW$count\fR is defined at compile\-time, but also goes out of
scope at the end of the \s-1BEGIN\s0 block. The \s-1BEGIN\s0 block also ensures that
the subroutine and the value it uses is defined at compile-time so the
subroutine is ready to use just like any other subroutine, and you can
put this code in the same place as other subroutines in the program
text (i.e. at the end of the code, typically). The subroutine
\&\f(CW\*(C`counter\*(C'\fR still has a reference to the data, and is the only way you
can access the value (and each time you do, you increment the value).
The data in chunk of memory defined by \f(CW$count\fR is private to
\&\f(CW\*(C`counter\*(C'\fR.
.PP
.Vb 4
\& BEGIN {
\& my $count = 1;
\& sub counter { $count++ }
\& }
.Ve
.PP
.Vb 1
\& my $start = count();
.Ve
.PP
.Vb 1
\& .... # code that calls count();
.Ve
.PP
.Vb 1
\& my $end = count();
.Ve
.PP
In the previous example, you created a function-private variable
because only one function remembered its reference. You could define
multiple functions while the variable is in scope, and each function
can share the \*(L"private\*(R" variable. It's not really \*(L"static\*(R" because you
can access it outside the function while the lexical variable is in
scope, and even create references to it. In this example,
\&\f(CW\*(C`increment_count\*(C'\fR and \f(CW\*(C`return_count\*(C'\fR share the variable. One
function adds to the value and the other simply returns the value.
They can both access \f(CW$count\fR, and since it has gone out of scope,
there is no other way to access it.
.PP
.Vb 5
\& BEGIN {
\& my $count = 1;
\& sub increment_count { $count++ }
\& sub return_count { $count }
\& }
.Ve
.PP
To declare a file-private variable, you still use a lexical variable.
A file is also a scope, so a lexical variable defined in the file
cannot be seen from any other file.
.PP
See \*(L"Persistent Private Variables\*(R" in perlsub for more information.
The discussion of closures in perlref may help you even though we
did not use anonymous subroutines in this answer. See
\&\*(L"Persistent Private Variables\*(R" in perlsub for details.
.Sh "What's the difference between dynamic and lexical (static) scoping? Between \fIlocal()\fP and \fImy()\fP?"
.IX Subsection "What's the difference between dynamic and lexical (static) scoping? Between local() and my()?"
\&\f(CW\*(C`local($x)\*(C'\fR saves away the old value of the global variable \f(CW$x\fR
and assigns a new value for the duration of the subroutine \fIwhich is
visible in other functions called from that subroutine\fR. This is done
at run\-time, so is called dynamic scoping. \fIlocal()\fR always affects global
variables, also called package variables or dynamic variables.
.PP
\&\f(CW\*(C`my($x)\*(C'\fR creates a new variable that is only visible in the current
subroutine. This is done at compile\-time, so it is called lexical or
static scoping. \fImy()\fR always affects private variables, also called
lexical variables or (improperly) static(ly scoped) variables.
.PP
For instance:
.PP
.Vb 3
\& sub visible {
\& print "var has value $var\en";
\& }
.Ve
.PP
.Vb 4
\& sub dynamic {
\& local $var = 'local'; # new temporary value for the still-global
\& visible(); # variable called $var
\& }
.Ve
.PP
.Vb 4
\& sub lexical {
\& my $var = 'private'; # new private variable, $var
\& visible(); # (invisible outside of sub scope)
\& }
.Ve
.PP
.Vb 1
\& $var = 'global';
.Ve
.PP
.Vb 3
\& visible(); # prints global
\& dynamic(); # prints local
\& lexical(); # prints global
.Ve
.PP
Notice how at no point does the value \*(L"private\*(R" get printed. That's
because \f(CW$var\fR only has that value within the block of the \fIlexical()\fR
function, and it is hidden from called subroutine.
.PP
In summary, \fIlocal()\fR doesn't make what you think of as private, local
variables. It gives a global variable a temporary value. \fImy()\fR is
what you're looking for if you want private variables.
.PP
See \*(L"Private Variables via \fImy()\fR\*(R" in perlsub and
\&\*(L"Temporary Values via \fIlocal()\fR\*(R" in perlsub for excruciating details.
.Sh "How can I access a dynamic variable while a similarly named lexical is in scope?"
.IX Subsection "How can I access a dynamic variable while a similarly named lexical is in scope?"
If you know your package, you can just mention it explicitly, as in
\&\f(CW$Some_Pack::var\fR. Note that the notation \f(CW$::var\fR is \fBnot\fR the dynamic \f(CW$var\fR
in the current package, but rather the one in the \*(L"main\*(R" package, as
though you had written \f(CW$main::var\fR.
.PP
.Vb 3
\& use vars '$var';
\& local $var = "global";
\& my $var = "lexical";
.Ve
.PP
.Vb 2
\& print "lexical is $var\en";
\& print "global is $main::var\en";
.Ve
.PP
Alternatively you can use the compiler directive \fIour()\fR to bring a
dynamic variable into the current lexical scope.
.PP
.Vb 2
\& require 5.006; # our() did not exist before 5.6
\& use vars '$var';
.Ve
.PP
.Vb 2
\& local $var = "global";
\& my $var = "lexical";
.Ve
.PP
.Vb 1
\& print "lexical is $var\en";
.Ve
.PP
.Vb 4
\& {
\& our $var;
\& print "global is $var\en";
\& }
.Ve
.Sh "What's the difference between deep and shallow binding?"
.IX Subsection "What's the difference between deep and shallow binding?"
In deep binding, lexical variables mentioned in anonymous subroutines
are the same ones that were in scope when the subroutine was created.
In shallow binding, they are whichever variables with the same names
happen to be in scope when the subroutine is called. Perl always uses
deep binding of lexical variables (i.e., those created with \fImy()\fR).
However, dynamic variables (aka global, local, or package variables)
are effectively shallowly bound. Consider this just one more reason
not to use them. See the answer to \*(L"What's a closure?\*(R".
.ie n .Sh "Why doesn't ""my($foo) = <\s-1FILE\s0>;"" work right?"
.el .Sh "Why doesn't ``my($foo) = <\s-1FILE\s0>;'' work right?"
.IX Subsection "Why doesn't ""my($foo) = <FILE>;"" work right?"
\&\f(CW\*(C`my()\*(C'\fR and \f(CW\*(C`local()\*(C'\fR give list context to the right hand side
of \f(CW\*(C`=\*(C'\fR. The <\s-1FH\s0> read operation, like so many of Perl's
functions and operators, can tell which context it was called in and
behaves appropriately. In general, the \fIscalar()\fR function can help.
This function does nothing to the data itself (contrary to popular myth)
but rather tells its argument to behave in whatever its scalar fashion is.
If that function doesn't have a defined scalar behavior, this of course
doesn't help you (such as with \fIsort()\fR).
.PP
To enforce scalar context in this particular case, however, you need
merely omit the parentheses:
.PP
.Vb 3
\& local($foo) = <FILE>; # WRONG
\& local($foo) = scalar(<FILE>); # ok
\& local $foo = <FILE>; # right
.Ve
.PP
You should probably be using lexical variables anyway, although the
issue is the same here:
.PP
.Vb 2
\& my($foo) = <FILE>; # WRONG
\& my $foo = <FILE>; # right
.Ve
.Sh "How do I redefine a builtin function, operator, or method?"
.IX Subsection "How do I redefine a builtin function, operator, or method?"
Why do you want to do that? :\-)
.PP
If you want to override a predefined function, such as \fIopen()\fR,
then you'll have to import the new definition from a different
module. See \*(L"Overriding Built-in Functions\*(R" in perlsub. There's
also an example in \*(L"Class::Template\*(R" in perltoot.
.PP
If you want to overload a Perl operator, such as \f(CW\*(C`+\*(C'\fR or \f(CW\*(C`**\*(C'\fR,
then you'll want to use the \f(CW\*(C`use overload\*(C'\fR pragma, documented
in overload.
.PP
If you're talking about obscuring method calls in parent classes,
see \*(L"Overridden Methods\*(R" in perltoot.
.Sh "What's the difference between calling a function as &foo and \fIfoo()\fP?"
.IX Subsection "What's the difference between calling a function as &foo and foo()?"
When you call a function as \f(CW&foo\fR, you allow that function access to
your current \f(CW@_\fR values, and you bypass prototypes.
The function doesn't get an empty \f(CW@_\fR\-\-it gets yours! While not
strictly speaking a bug (it's documented that way in perlsub), it
would be hard to consider this a feature in most cases.
.PP
When you call your function as \f(CW\*(C`&foo()\*(C'\fR, then you \fIdo\fR get a new \f(CW@_\fR,
but prototyping is still circumvented.
.PP
Normally, you want to call a function using \f(CW\*(C`foo()\*(C'\fR. You may only
omit the parentheses if the function is already known to the compiler
because it already saw the definition (\f(CW\*(C`use\*(C'\fR but not \f(CW\*(C`require\*(C'\fR),
or via a forward reference or \f(CW\*(C`use subs\*(C'\fR declaration. Even in this
case, you get a clean \f(CW@_\fR without any of the old values leaking through
where they don't belong.
.Sh "How do I create a switch or case statement?"
.IX Subsection "How do I create a switch or case statement?"
This is explained in more depth in the perlsyn. Briefly, there's
no official case statement, because of the variety of tests possible
in Perl (numeric comparison, string comparison, glob comparison,
regex matching, overloaded comparisons, ...).
Larry couldn't decide how best to do this, so he left it out, even
though it's been on the wish list since perl1.
.PP
Starting from Perl 5.8 to get switch and case one can use the
Switch extension and say:
.PP
.Vb 1
\& use Switch;
.Ve
.PP
after which one has switch and case. It is not as fast as it could be
because it's not really part of the language (it's done using source
filters) but it is available, and it's very flexible.
.PP
But if one wants to use pure Perl, the general answer is to write a
construct like this:
.PP
.Vb 6
\& for ($variable_to_test) {
\& if (/pat1/) { } # do something
\& elsif (/pat2/) { } # do something else
\& elsif (/pat3/) { } # do something else
\& else { } # default
\& }
.Ve
.PP
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 multiway conditional based on the type of reference stored
in \f(CW$whatchamacallit:\fR
.PP
.Vb 1
\& SWITCH: for (ref $whatchamacallit) {
.Ve
.PP
.Vb 1
\& /^$/ && die "not a reference";
.Ve
.PP
.Vb 4
\& /SCALAR/ && do {
\& print_scalar($$ref);
\& last SWITCH;
\& };
.Ve
.PP
.Vb 4
\& /ARRAY/ && do {
\& print_array(@$ref);
\& last SWITCH;
\& };
.Ve
.PP
.Vb 4
\& /HASH/ && do {
\& print_hash(%$ref);
\& last SWITCH;
\& };
.Ve
.PP
.Vb 4
\& /CODE/ && do {
\& warn "can't print function ref";
\& last SWITCH;
\& };
.Ve
.PP
.Vb 1
\& # DEFAULT
.Ve
.PP
.Vb 1
\& warn "User defined type skipped";
.Ve
.PP
.Vb 1
\& }
.Ve
.PP
See \f(CW\*(C`perlsyn/"Basic BLOCKs and Switch Statements"\*(C'\fR for many other
examples in this style.
.PP
Sometimes you should change the positions of the constant and the variable.
For example, let's say you wanted to test which of many answers you were
given, but in a case-insensitive way that also allows abbreviations.
You can use the following technique if the strings all start with
different characters or if you want to arrange the matches so that
one takes precedence over another, as \f(CW"SEND"\fR has precedence over
\&\f(CW"STOP"\fR here:
.PP
.Vb 6
\& chomp($answer = <>);
\& if ("SEND" =~ /^\eQ$answer/i) { print "Action is send\en" }
\& elsif ("STOP" =~ /^\eQ$answer/i) { print "Action is stop\en" }
\& elsif ("ABORT" =~ /^\eQ$answer/i) { print "Action is abort\en" }
\& elsif ("LIST" =~ /^\eQ$answer/i) { print "Action is list\en" }
\& elsif ("EDIT" =~ /^\eQ$answer/i) { print "Action is edit\en" }
.Ve
.PP
A totally different approach is to create a hash of function references.
.PP
.Vb 6
\& my %commands = (
\& "happy" => \e&joy,
\& "sad", => \e&sullen,
\& "done" => sub { die "See ya!" },
\& "mad" => \e&angry,
\& );
.Ve
.PP
.Vb 7
\& print "How are you? ";
\& chomp($string = <STDIN>);
\& if ($commands{$string}) {
\& $commands{$string}->();
\& } else {
\& print "No such command: $string\en";
\& }
.Ve
.Sh "How can I catch accesses to undefined variables, functions, or methods?"
.IX Subsection "How can I catch accesses to undefined variables, functions, or methods?"
The \s-1AUTOLOAD\s0 method, discussed in \*(L"Autoloading\*(R" in perlsub and
\&\*(L"\s-1AUTOLOAD:\s0 Proxy Methods\*(R" in perltoot, lets you capture calls to
undefined functions and methods.
.PP
When it comes to undefined variables that would trigger a warning
under \f(CW\*(C`use warnings\*(C'\fR, you can promote the warning to an error.
.PP
.Vb 1
\& use warnings FATAL => qw(uninitialized);
.Ve
.Sh "Why can't a method included in this same file be found?"
.IX Subsection "Why can't a method included in this same file be found?"
Some possible reasons: your inheritance is getting confused, you've
misspelled the method name, or the object is of the wrong type. Check
out perltoot for details about any of the above cases. You may
also use \f(CW\*(C`print ref($object)\*(C'\fR to find out the class \f(CW$object\fR was
blessed into.
.PP
Another possible reason for problems is because you've used the
indirect object syntax (eg, \f(CW\*(C`find Guru "Samy"\*(C'\fR) on a class name
before Perl has seen that such a package exists. It's wisest to make
sure your packages are all defined before you start using them, which
will be taken care of if you use the \f(CW\*(C`use\*(C'\fR statement instead of
\&\f(CW\*(C`require\*(C'\fR. If not, make sure to use arrow notation (eg.,
\&\f(CW\*(C`Guru\->find("Samy")\*(C'\fR) instead. Object notation is explained in
perlobj.
.PP
Make sure to read about creating modules in perlmod and
the perils of indirect objects in \*(L"Method Invocation\*(R" in perlobj.
.Sh "How can I find out my current package?"
.IX Subsection "How can I find out my current package?"
If you're just a random program, you can do this to find
out what the currently compiled package is:
.PP
.Vb 1
\& my $packname = __PACKAGE__;
.Ve
.PP
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):
.PP
.Vb 5
\& sub amethod {
\& my $self = shift;
\& my $class = ref($self) || $self;
\& warn "called me from a $class object";
\& }
.Ve
.Sh "How can I comment out a large block of perl code?"
.IX Subsection "How can I comment out a large block of perl code?"
You can use embedded \s-1POD\s0 to discard it. Enclose the blocks you want
to comment out in \s-1POD\s0 markers. The <=begin> directive marks a section
for a specific formatter. Use the \f(CW\*(C`comment\*(C'\fR format, which no formatter
should claim to understand (by policy). Mark the end of the block
with <=end>.
.PP
.Vb 1
\& # program is here
.Ve
.PP
.Vb 1
\& =begin comment
.Ve
.PP
.Vb 1
\& all of this stuff
.Ve
.PP
.Vb 2
\& here will be ignored
\& by everyone
.Ve
.PP
.Vb 1
\& =end comment
.Ve
.PP
.Vb 1
\& =cut
.Ve
.PP
.Vb 1
\& # program continues
.Ve
.PP
The pod directives cannot go just anywhere. You must put a
pod directive where the parser is expecting a new statement,
not just in the middle of an expression or some other
arbitrary grammar production.
.PP
See perlpod for more details.
.Sh "How do I clear a package?"
.IX Subsection "How do I clear a package?"
Use this code, provided by Mark-Jason Dominus:
.PP
.Vb 17
\& sub scrub_package {
\& no strict 'refs';
\& my $pack = shift;
\& die "Shouldn't delete main package"
\& if $pack eq "" || $pack eq "main";
\& my $stash = *{$pack . '::'}{HASH};
\& my $name;
\& foreach $name (keys %$stash) {
\& my $fullname = $pack . '::' . $name;
\& # Get rid of everything with that name.
\& undef $$fullname;
\& undef @$fullname;
\& undef %$fullname;
\& undef &$fullname;
\& undef *$fullname;
\& }
\& }
.Ve
.PP
Or, if you're using a recent release of Perl, you can
just use the \fISymbol::delete_package()\fR function instead.
.Sh "How can I use a variable as a variable name?"
.IX Subsection "How can I use a variable as a variable name?"
Beginners often think they want to have a variable contain the name
of a variable.
.PP
.Vb 3
\& $fred = 23;
\& $varname = "fred";
\& ++$$varname; # $fred now 24
.Ve
.PP
This works \fIsometimes\fR, but it is a very bad idea for two reasons.
.PP
The first reason is that this technique \fIonly works on global
variables\fR. That means that if \f(CW$fred\fR is a lexical variable created
with \fImy()\fR in the above example, the code wouldn't work at all: you'd
accidentally access the global and skip right over the private lexical
altogether. Global variables are bad because they can easily collide
accidentally and in general make for non-scalable and confusing code.
.PP
Symbolic references are forbidden under the \f(CW\*(C`use strict\*(C'\fR pragma.
They are not true references and consequently are not reference counted
or garbage collected.
.PP
The other reason why using a variable to hold the name of another
variable is a bad idea is that the question often stems from a lack of
understanding of Perl data structures, particularly hashes. By using
symbolic references, you are just using the package's symbol-table hash
(like \f(CW%main::\fR) instead of a user-defined hash. The solution is to
use your own hash or a real reference instead.
.PP
.Vb 3
\& $USER_VARS{"fred"} = 23;
\& $varname = "fred";
\& $USER_VARS{$varname}++; # not $$varname++
.Ve
.PP
There we're using the \f(CW%USER_VARS\fR 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:
.PP
.Vb 2
\& $str = 'this has a $fred and $barney in it';
\& $str =~ s/(\e$\ew+)/$1/eeg; # need double eval
.Ve
.PP
it would be better to keep a hash around like \f(CW%USER_VARS\fR and have
variable references actually refer to entries in that hash:
.PP
.Vb 1
\& $str =~ s/\e$(\ew+)/$USER_VARS{$1}/g; # no /e here at all
.Ve
.PP
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.
.PP
.Vb 2
\& $str = 'this has a %fred% and %barney% in it';
\& $str =~ s/%(\ew+)%/$USER_VARS{$1}/g; # no /e here at all
.Ve
.PP
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: \f(CW%fred\fR and \f(CW%barney\fR, and that they
wanted to use another scalar variable to refer to those by name.
.PP
.Vb 2
\& $name = "fred";
\& $$name{WIFE} = "wilma"; # set %fred
.Ve
.PP
.Vb 2
\& $name = "barney";
\& $$name{WIFE} = "betty"; # set %barney
.Ve
.PP
This is still a symbolic reference, and is still saddled with the
problems enumerated above. It would be far better to write:
.PP
.Vb 2
\& $folks{"fred"}{WIFE} = "wilma";
\& $folks{"barney"}{WIFE} = "betty";
.Ve
.PP
And just use a multilevel hash to start with.
.PP
The only times that you absolutely \fImust\fR use symbolic references are
when you really must refer to the symbol table. This may be because it's
something that can't take a real reference to, such as a format name.
Doing so may also be important for method calls, since these always go
through the symbol table for resolution.
.PP
In those cases, you would turn off \f(CW\*(C`strict 'refs'\*(C'\fR temporarily so you
can play around with the symbol table. For example:
.PP
.Vb 5
\& @colors = qw(red blue green yellow orange purple violet);
\& for my $name (@colors) {
\& no strict 'refs'; # renege for the block
\& *$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
\& }
.Ve
.PP
All those functions (\fIred()\fR, \fIblue()\fR, \fIgreen()\fR, etc.) appear to be separate,
but the real code in the closure actually was compiled only once.
.PP
So, sometimes you might want to use symbolic references to directly
manipulate the symbol table. This doesn't matter for formats, handles, and
subroutines, because they are always global\*(--you can't use \fImy()\fR on them.
For scalars, arrays, and hashes, though\*(--and usually for subroutines\*(--
you probably only want to use hard references.
.ie n .Sh "What does ""bad interpreter"" mean?"
.el .Sh "What does ``bad interpreter'' mean?"
.IX Subsection "What does bad interpreter mean?"
(contributed by brian d foy)
.PP
The \*(L"bad interpreter\*(R" message comes from the shell, not perl. The
actual message may vary depending on your platform, shell, and locale
settings.
.PP
If you see \*(L"bad interpreter \- no such file or directory\*(R", the first
line in your perl script (the \*(L"shebang\*(R" line) does not contain the
right path to perl (or any other program capable of running scripts).
Sometimes this happens when you move the script from one machine to
another and each machine has a different path to perl\-\-\-/usr/bin/perl
versus /usr/local/bin/perl for instance. It may also indicate
that the source machine has \s-1CRLF\s0 line terminators and the
destination machine has \s-1LF\s0 only: the shell tries to find
/usr/bin/perl<\s-1CR\s0>, but can't.
.PP
If you see \*(L"bad interpreter: Permission denied\*(R", you need to make your
script executable.
.PP
In either case, you should still be able to run the scripts with perl
explicitly:
.PP
.Vb 1
\& % perl script.pl
.Ve
.PP
If you get a message like \*(L"perl: command not found\*(R", perl is not in
your \s-1PATH\s0, which might also mean that the location of perl is not
where you expect it so you need to adjust your shebang line.
.SH "AUTHOR AND COPYRIGHT"
.IX Header "AUTHOR AND COPYRIGHT"
Copyright (c) 1997\-2006 Tom Christiansen, Nathan Torkington, and
other authors as noted. All rights reserved.
.PP
This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.
.PP
Irrespective of its distribution, all code examples in this file
are hereby placed into the public domain. You are permitted and
encouraged to use this code in your own programs for fun
or for profit as you see fit. A simple comment in the code giving
credit would be courteous but is not required.