- NAME
- DESCRIPTION
- Can I get a BNF/yacc/RE for the Perl language?
- What are all these $@%&* punctuation signs, and how do I know when to use them?
- Do I always/never have to quote my strings or use semicolons and commas?
- How do I skip some return values?
- How do I temporarily block warnings?
- What's an extension?
- Why do Perl operators have different precedence than C operators?
- How do I declare/create a structure?
- How do I create a module?
- How do I adopt or take over a module already on CPAN?
- How do I create a class?
- How can I tell if a variable is tainted?
- What's a closure?
- What is variable suicide and how can I prevent it?
- How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?
- How do I create a static variable?
- What's the difference between dynamic and lexical (static) scoping? Between
local() and my()?
- How can I access a dynamic variable while a similarly named lexical is in scope?
- What's the difference between deep and shallow binding?
- Why doesn't "my($foo) = <FILE>;" work right?
- How do I redefine a builtin function, operator, or method?
- What's the difference between calling a function as &foo and foo()?
- How do I create a switch or case statement?
- How can I catch accesses to undefined variables, functions, or methods?
- Why can't a method included in this same file be found?
- How can I find out my current package?
- How can I comment out a large block of perl code?
- How do I clear a package?
- How can I use a variable as a variable name?
- What does "bad interpreter" mean?
- REVISION
- AUTHOR AND COPYRIGHT
perlfaq7 - General Perl Language Issues ($Revision: 10100 $)
This section deals with general Perl language issues that don't
clearly fit into any of the other sections.
There is no BNF, 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.
In the words of Chaim Frenkel: "Perl's grammar can not be reduced to BNF.
The work of parsing perl is distributed between yacc, the lexer, smoke
and mirrors."
They are type specifiers, as detailed in the perldata manpage:
$ 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.
There are couple of other symbols that you're likely to encounter that aren't
really type specifiers:
<> are used for inputting a record from a filehandle.
\ takes a reference to something.
Note that <FILE> is neither the type specifier for files
nor the name of the handle. It is the <> operator applied
to the handle FILE. It reads one line (well, record--see
$/ in the perlvar manpage) from the handle FILE in scalar context, or all lines
in list context. When performing open, close, or any other operation
besides <> on files, or even when talking about the handle, do
not use the brackets. These are correct: eof(FH), seek(FH, 0,
2) and "copying from STDIN to FILE".
Normally, a bareword doesn't need to be quoted, but in most cases
probably should be (and must be under use strict). 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 => operator both
count as though they were quoted:
This is like this
------------ ---------------
$foo{line} $foo{'line'}
bar => stuff 'bar' => stuff
The final semicolon in a block is optional, as is the final comma in a
list. Good style (see the perlstyle manpage) says to put them in except for
one-liners:
if ($whoops) { exit 1 }
@nums = (1, 2, 3);
if ($whoops) {
exit 1;
}
@lines = (
"There Beren came from mountains cold",
"And lost he wandered under leaves",
);
One way is to treat the return values as a list and index into it:
$dir = (getpwnam($user))[7];
Another way is to use undef as an element on the left-hand-side:
($dev, $ino, undef, undef, $uid, $gid) = stat($file);
You can also use a list slice to select only the elements that
you need:
($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5];
If you are running Perl 5.6.0 or better, the use warnings pragma
allows fine control of what warning are produced.
See the perllexwarn manpage for more details.
{
no warnings;
$a = $b + $c;
}
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 the perllexwarn manpage for the
complete details, including the category names and hierarchy.
{
no warnings 'uninitialized';
$a = $b + $c;
}
If you have an older version of Perl, the $^W variable (documented
in the perlvar manpage) controls runtime warnings for a block:
{
local $^W = 0;
$a = $b + $c;
}
Note that like all the punctuation variables, you cannot currently
use my() on $^W, only local().
An extension is a way of calling compiled C code from Perl. Reading
the perlxstut manpage is a good place to learn more about extensions.
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 "list operators" and appear as such in the precedence table in
the perlop manpage.
A common mistake is to write:
unlink $file || die "snafu";
This gets interpreted as:
unlink ($file || die "snafu");
To avoid this problem, either put in extra parentheses or use the
super low precedence or operator:
(unlink $file) || die "snafu";
unlink $file or die "snafu";
The "English" operators (and, or, xor, and not)
deliberately have precedence lower than that of list operators for
just such situations as the one above.
Another operator with surprising precedence is exponentiation. It
binds more tightly even than unary minus, making -2**2 product a
negative not a positive four. It is also right-associating, meaning
that 2**3**2 is two raised to the ninth power, not eight squared.
Although it has the same precedence as in C, Perl's ?: operator
produces an lvalue. This assigns $x to either $a or $b, depending
on the trueness of $maybe:
($maybe ? $a : $b) = $x;
In general, you don't "declare" a structure. Just use a (probably
anonymous) hash reference. See the perlref manpage and the perldsc manpage for details.
Here's an example:
$person = {};
$person->{AGE} = 24;
$person->{NAME} = "Nat";
If you're looking for something a bit more rigorous, try L<perltoot>.
(contributed by brian d foy)
the perlmod manpage, the perlmodlib manpage, the perlmodstyle manpage explain modules
in all the gory details. the perlnewmod manpage gives a brief
overview of the process along with a couple of suggestions
about style.
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. the perlxs manpage and the perlxstut manpage explain the details.
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.
You may also want to see Sam Tregar's "Writing Perl Modules
for CPAN" ( http://apress.com/book/bookDisplay.html?bID=14 )
which is the best hands-on guide to creating module
distributions.
(contributed by brian d foy)
The easiest way to take over a module is to have the current
module maintainer either make you a co-maintainer or transfer
the module to you.
If you can't reach the author for some reason (e.g. email bounces),
the PAUSE admins at modules@perl.org can help. The PAUSE admins
treat each case individually.
-
-
Get a login for the Perl Authors Upload Server (PAUSE) if you don't
already have one: http://pause.perl.org
-
-
Write to modules@perl.org explaining what you did to contact the
current maintainer. The PAUSE admins will also try to reach the
maintainer.
-
-
Post a public message in a heavily trafficked site announcing your
intention to take over the module.
-
-
Wait a bit. The PAUSE admins don't want to act too quickly in case
the current maintainer is on holiday. If there's no response to
private communication or the public post, a PAUSE admin can transfer
it to you.
See the perltoot manpage for an introduction to classes and objects, as well as
the perlobj manpage and the perlbot manpage.
You can use the tainted() function of the Scalar::Util module, available
from CPAN (or included with Perl since release 5.8.0).
See also Laundering and Detecting Tainted Data in the perlsec manpage.
Closures are documented in the perlref manpage.
Closure is a computer science term with a precise but
hard-to-explain meaning. Usually, 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).
Closures are most often used in programming languages 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.
Here's a classic non-closure function-generating function:
sub add_function_generator {
return sub { shift() + shift() };
}
$add_sub = add_function_generator();
$sum = $add_sub->(4,5);
The anonymous subroutine returned by add_function_generator() isn't
technically a closure because it refers to no lexicals outside its own
scope. Using a closure gives you a function template with some
customization slots left out to be filled later.
Contrast this with the following make_adder() 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.
sub make_adder {
my $addpiece = shift;
return sub { shift() + $addpiece };
}
$f1 = make_adder(20);
$f2 = make_adder(555);
Now &$f1($n) is always 20 plus whatever $n you pass in, whereas
&$f2($n) is always 555 plus whatever $n you pass in. The $addpiece
in the closure sticks around.
Closures are often used for less esoteric purposes. For example, when
you want to pass in a bit of code into a function:
my $line;
timeout( 30, sub { $line = <STDIN> } );
If the code to execute had been passed in as a string,
'$line = <STDIN>', there would have been no way for the
hypothetical timeout() function to access the lexical variable
$line back in its caller's scope.
Another use for a closure is to make a variable private to a
named subroutine, e.g. a counter that gets initialized at creation
time of the sub and can only be modified from within the sub.
This is sometimes used with a BEGIN block in package files to make
sure a variable doesn't get meddled with during the lifetime of the
package:
BEGIN {
my $id = 0;
sub next_id { ++$id }
}
This is discussed in more detail in the perlsub manpage, see the entry on
Persistent Private Variables.
This problem was fixed in perl 5.004_05, so preventing it means upgrading
your version of perl. ;)
Variable suicide is when you (temporarily or permanently) lose the value
of a variable. It is caused by scoping through my() and local()
interacting with either closures or aliased foreach() 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:
my $f = 'foo';
sub |