The Default Package
The default package is main, just like the top-level subroutine
name in C. Unless you say otherwise (coming up), all variables are in
this package. These are the same:
#!/usr/bin/perl $name = 'Amelia'; $main::name = 'Amelia'; $type = 'Camel'; $main::type = 'Camel';
Under strict, you have to say otherwise because that pragma doesn’t
let you use undeclared variables:
#!/usr/bin/perl use v5.12; $name = 'Amelia'; # compile–time error $main::name = 'Amelia'; $type = 'Camel'; # compile–time error $main::type = 'Camel';
Only identifiers (names starting with letters or an underscore)
are stored in a package’s symbol table. All other symbols are kept in
the main package, including the
nonalphabetic variables, like $!,
$?, and $_.[139] In addition, when unqualified, the identifiers STDIN, STDOUT, STDERR, ARGV, ARGVOUT, ENV, INC,
and SIG are forced to be in package
main, even when used for other
purposes than their built-in ones. Don’t name your package m, s,
y, tr, q,
qq, qr, qw,
or qx unless you’re looking for a
lot of trouble. For instance, you won’t be able to use the qualified
form of an identifier as a filehandle because it will be interpreted
instead as a pattern match, a substitution, or a
transliteration.
[139] You can have a lexical $_
in v5.10, though.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access