.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 .\" .\" 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 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide" .SH "NAME" perlfaq7 \- General Perl Language Issues ($Revision: 1.8 $, $Date: 2002/03/26 15:48:32 $) .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 \&\*(L"$/\*(R" 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 .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 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?" A module is a package that lives in a file of the same name. For example, the Hello::There module would live in Hello/There.pm. For details, read perlmod. You'll also find Exporter helpful. If you're writing a C or mixed-language module with both C and Perl, then you should study perlxstut. .PP The \f(CW\*(C`h2xs\*(C'\fR program will create stubs for all the important stuff for you: .PP .Vb 1 \& % h2xs -XA -n My::Module .Ve .PP The \f(CW\*(C`\-X\*(C'\fR switch tells \f(CW\*(C`h2xs\*(C'\fR that you are not using \f(CW\*(C`XS\*(C'\fR extension code. The \f(CW\*(C`\-A\*(C'\fR switch tells \f(CW\*(C`h2xs\*(C'\fR that you are not using the AutoLoader, and the \f(CW\*(C`\-n\*(C'\fR switch specifies the name of the module. See h2xs for more details. .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 = } ); .Ve .PP If the code to execute had been passed in as a string, \&\f(CW'$line = '\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?" 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 .= "bar"; print $f, "\en" } \& } \& T; \& print "Finally $f\en"; .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 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). .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 ``Passing Regexes'', 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" To pass filehandles to subroutines, 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. .Sp Here's an excerpt: .Sp If you're passing around filehandles, you could usually just use the bare typeglob, like *STDOUT, but typeglobs references would be better because they'll still work properly under \f(CW\*(C`use strict 'refs'\*(C'\fR. For example: .Sp .Vb 5 \& splutter(\e*STDOUT); \& sub splutter { \& my $fh = shift; \& print $fh "her um well a hmmm\en"; \& } .Ve .Sp .Vb 5 \& $rec = get_rec(\e*STDIN); \& sub get_rec { \& my $fh = shift; \& return scalar <$fh>; \& } .Ve .Sp If you're planning on generating new filehandles, you could do this: .Sp .Vb 7 \& sub openit { \& my $path = shift; \& local *FH; \& return open (FH, $path) ? *FH : undef; \& } \& $fh = openit('< /etc/motd'); \& print <$fh>; .Ve .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?" As with most things in Perl, \s-1TMTOWTDI\s0. What is a \*(L"static variable\*(R" in other languages could be either a function-private variable (visible only within a single function, retaining its value between calls to that function), or a file-private variable (visible only to functions within the file it was declared in) in Perl. .PP Here's code to implement a function-private variable: .PP .Vb 5 \& BEGIN { \& my $counter = 42; \& sub prev_counter { return --$counter } \& sub next_counter { return $counter++ } \& } .Ve .PP Now \fIprev_counter()\fR and \fInext_counter()\fR share a private variable \f(CW$counter\fR that was initialized at compile time. .PP To declare a file-private variable, you'll still use a \fImy()\fR, putting the declaration at the outer scope level at the top of the file. Assume this is in file Pax.pm: .PP .Vb 2 \& package Pax; \& my $started = scalar(localtime(time())); .Ve .PP .Vb 1 \& sub begun { return $started } .Ve .PP When \f(CW\*(C`use Pax\*(C'\fR or \f(CW\*(C`require Pax\*(C'\fR 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 \fIbegun()\fR function cares about it, but no one else can get it. It is not called \f(CW$Pax::started\fR 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. .PP 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?" You can do this via symbolic references, provided you haven't set \&\f(CW\*(C`use strict "refs"\*(C'\fR. So instead of \f(CW$var\fR, use \f(CW\*(C`${'var'}\*(C'\fR. .PP .Vb 2 \& local $var = "global"; \& my $var = "lexical"; .Ve .PP .Vb 1 \& print "lexical is $var\en"; .Ve .PP .Vb 2 \& no strict 'refs'; \& print "global is ${'var'}\en"; .Ve .PP 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 \fInot\fR the dynamic \&\f(CW$var\fR in the current package, but rather the one in the \f(CW\*(C`main\*(C'\fR package, as though you had written \f(CW$main::var\fR. Specifying the package directly makes you hard-code its name, but it executes faster and avoids running afoul of \f(CW\*(C`use strict "refs"\*(C'\fR. .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) = ; 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) = ; # WRONG \& local($foo) = scalar(); # ok \& local $foo = ; # right .Ve .PP You should probably be using lexical variables anyway, although the issue is the same here: .PP .Vb 2 \& my($foo) = ; # WRONG \& my $foo = ; # 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 = ); \& if ($commands{$string}) { \& $commands{$string}->(); \& } else { \& print "No such command: $string\en"; \& } .Ve .Sh "How can I catch accesses to undefined variables/functions/methods?" .IX Subsection "How can I catch accesses to undefined variables/functions/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`\-w\*(C'\fR, you can use a handler to trap the pseudo-signal \&\f(CW\*(C`_\|_WARN_\|_\*(C'\fR like this: .PP .Vb 1 \& $SIG{__WARN__} = sub { .Ve .PP .Vb 1 \& for ( $_[0] ) { # voici un switch statement .Ve .PP .Vb 4 \& /Use of uninitialized value/ && do { \& # promote warning to a fatal \& die $_; \& }; .Ve .PP .Vb 1 \& # other warning cases to catch could go here; .Ve .PP .Vb 2 \& warn $_; \& } .Ve .PP .Vb 1 \& }; .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. The =for directive lasts until the next paragraph (two consecutive newlines). .PP .Vb 1 \& # program is here .Ve .PP .Vb 2 \& =for nobody \& This paragraph is commented out .Ve .PP .Vb 1 \& # program continues .Ve .PP The =begin and =end directives can contain multiple paragraphs. .PP .Vb 1 \& =begin comment text .Ve .PP .Vb 1 \& all of this stuff .Ve .PP .Vb 2 \& here will be ignored \& by everyone .Ve .PP .Vb 1 \& =end comment text .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 s 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 \& $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 { "@_" }; \& } .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. .SH "AUTHOR AND COPYRIGHT" .IX Header "AUTHOR AND COPYRIGHT" Copyright (c) 1997\-2002 Tom Christiansen and Nathan Torkington. 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.