1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlmod - Perl modules (packages and symbol tables) 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gate=head2 Packages 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gatePerl provides a mechanism for alternative namespaces to protect 10*0Sstevel@tonic-gatepackages from stomping on each other's variables. In fact, there's 11*0Sstevel@tonic-gatereally no such thing as a global variable in Perl. The package 12*0Sstevel@tonic-gatestatement declares the compilation unit as being in the given 13*0Sstevel@tonic-gatenamespace. The scope of the package declaration is from the 14*0Sstevel@tonic-gatedeclaration itself through the end of the enclosing block, C<eval>, 15*0Sstevel@tonic-gateor file, whichever comes first (the same scope as the my() and 16*0Sstevel@tonic-gatelocal() operators). Unqualified dynamic identifiers will be in 17*0Sstevel@tonic-gatethis namespace, except for those few identifiers that if unqualified, 18*0Sstevel@tonic-gatedefault to the main package instead of the current one as described 19*0Sstevel@tonic-gatebelow. A package statement affects only dynamic variables--including 20*0Sstevel@tonic-gatethose you've used local() on--but I<not> lexical variables created 21*0Sstevel@tonic-gatewith my(). Typically it would be the first declaration in a file 22*0Sstevel@tonic-gateincluded by the C<do>, C<require>, or C<use> operators. You can 23*0Sstevel@tonic-gateswitch into a package in more than one place; it merely influences 24*0Sstevel@tonic-gatewhich symbol table is used by the compiler for the rest of that 25*0Sstevel@tonic-gateblock. You can refer to variables and filehandles in other packages 26*0Sstevel@tonic-gateby prefixing the identifier with the package name and a double 27*0Sstevel@tonic-gatecolon: C<$Package::Variable>. If the package name is null, the 28*0Sstevel@tonic-gateC<main> package is assumed. That is, C<$::sail> is equivalent to 29*0Sstevel@tonic-gateC<$main::sail>. 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gateThe old package delimiter was a single quote, but double colon is now the 32*0Sstevel@tonic-gatepreferred delimiter, in part because it's more readable to humans, and 33*0Sstevel@tonic-gatein part because it's more readable to B<emacs> macros. It also makes C++ 34*0Sstevel@tonic-gateprogrammers feel like they know what's going on--as opposed to using the 35*0Sstevel@tonic-gatesingle quote as separator, which was there to make Ada programmers feel 36*0Sstevel@tonic-gatelike they knew what was going on. Because the old-fashioned syntax is still 37*0Sstevel@tonic-gatesupported for backwards compatibility, if you try to use a string like 38*0Sstevel@tonic-gateC<"This is $owner's house">, you'll be accessing C<$owner::s>; that is, 39*0Sstevel@tonic-gatethe $s variable in package C<owner>, which is probably not what you meant. 40*0Sstevel@tonic-gateUse braces to disambiguate, as in C<"This is ${owner}'s house">. 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gatePackages may themselves contain package separators, as in 43*0Sstevel@tonic-gateC<$OUTER::INNER::var>. This implies nothing about the order of 44*0Sstevel@tonic-gatename lookups, however. There are no relative packages: all symbols 45*0Sstevel@tonic-gateare either local to the current package, or must be fully qualified 46*0Sstevel@tonic-gatefrom the outer package name down. For instance, there is nowhere 47*0Sstevel@tonic-gatewithin package C<OUTER> that C<$INNER::var> refers to 48*0Sstevel@tonic-gateC<$OUTER::INNER::var>. C<INNER> refers to a totally 49*0Sstevel@tonic-gateseparate global package. 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gateOnly identifiers starting with letters (or underscore) are stored 52*0Sstevel@tonic-gatein a package's symbol table. All other symbols are kept in package 53*0Sstevel@tonic-gateC<main>, including all punctuation variables, like $_. In addition, 54*0Sstevel@tonic-gatewhen unqualified, the identifiers STDIN, STDOUT, STDERR, ARGV, 55*0Sstevel@tonic-gateARGVOUT, ENV, INC, and SIG are forced to be in package C<main>, 56*0Sstevel@tonic-gateeven when used for other purposes than their built-in ones. If you 57*0Sstevel@tonic-gatehave a package called C<m>, C<s>, or C<y>, then you can't use the 58*0Sstevel@tonic-gatequalified form of an identifier because it would be instead interpreted 59*0Sstevel@tonic-gateas a pattern match, a substitution, or a transliteration. 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gateVariables beginning with underscore used to be forced into package 62*0Sstevel@tonic-gatemain, but we decided it was more useful for package writers to be able 63*0Sstevel@tonic-gateto use leading underscore to indicate private variables and method names. 64*0Sstevel@tonic-gateHowever, variables and functions named with a single C<_>, such as 65*0Sstevel@tonic-gate$_ and C<sub _>, are still forced into the package C<main>. See also 66*0Sstevel@tonic-gateL<perlvar/"Technical Note on the Syntax of Variable Names">. 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gateC<eval>ed strings are compiled in the package in which the eval() was 69*0Sstevel@tonic-gatecompiled. (Assignments to C<$SIG{}>, however, assume the signal 70*0Sstevel@tonic-gatehandler specified is in the C<main> package. Qualify the signal handler 71*0Sstevel@tonic-gatename if you wish to have a signal handler in a package.) For an 72*0Sstevel@tonic-gateexample, examine F<perldb.pl> in the Perl library. It initially switches 73*0Sstevel@tonic-gateto the C<DB> package so that the debugger doesn't interfere with variables 74*0Sstevel@tonic-gatein the program you are trying to debug. At various points, however, it 75*0Sstevel@tonic-gatetemporarily switches back to the C<main> package to evaluate various 76*0Sstevel@tonic-gateexpressions in the context of the C<main> package (or wherever you came 77*0Sstevel@tonic-gatefrom). See L<perldebug>. 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gateThe special symbol C<__PACKAGE__> contains the current package, but cannot 80*0Sstevel@tonic-gate(easily) be used to construct variable names. 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gateSee L<perlsub> for other scoping issues related to my() and local(), 83*0Sstevel@tonic-gateand L<perlref> regarding closures. 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate=head2 Symbol Tables 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gateThe symbol table for a package happens to be stored in the hash of that 88*0Sstevel@tonic-gatename with two colons appended. The main symbol table's name is thus 89*0Sstevel@tonic-gateC<%main::>, or C<%::> for short. Likewise the symbol table for the nested 90*0Sstevel@tonic-gatepackage mentioned earlier is named C<%OUTER::INNER::>. 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gateThe value in each entry of the hash is what you are referring to when you 93*0Sstevel@tonic-gateuse the C<*name> typeglob notation. In fact, the following have the same 94*0Sstevel@tonic-gateeffect, though the first is more efficient because it does the symbol 95*0Sstevel@tonic-gatetable lookups at compile time: 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate local *main::foo = *main::bar; 98*0Sstevel@tonic-gate local $main::{foo} = $main::{bar}; 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate(Be sure to note the B<vast> difference between the second line above 101*0Sstevel@tonic-gateand C<local $main::foo = $main::bar>. The former is accessing the hash 102*0Sstevel@tonic-gateC<%main::>, which is the symbol table of package C<main>. The latter is 103*0Sstevel@tonic-gatesimply assigning scalar C<$bar> in package C<main> to scalar C<$foo> of 104*0Sstevel@tonic-gatethe same package.) 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gateYou can use this to print out all the variables in a package, for 107*0Sstevel@tonic-gateinstance. The standard but antiquated F<dumpvar.pl> library and 108*0Sstevel@tonic-gatethe CPAN module Devel::Symdump make use of this. 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gateAssignment to a typeglob performs an aliasing operation, i.e., 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate *dick = *richard; 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gatecauses variables, subroutines, formats, and file and directory handles 115*0Sstevel@tonic-gateaccessible via the identifier C<richard> also to be accessible via the 116*0Sstevel@tonic-gateidentifier C<dick>. If you want to alias only a particular variable or 117*0Sstevel@tonic-gatesubroutine, assign a reference instead: 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate *dick = \$richard; 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gateWhich makes $richard and $dick the same variable, but leaves 122*0Sstevel@tonic-gate@richard and @dick as separate arrays. Tricky, eh? 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gateThere is one subtle difference between the following statements: 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate *foo = *bar; 127*0Sstevel@tonic-gate *foo = \$bar; 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gateC<*foo = *bar> makes the typeglobs themselves synonymous while 130*0Sstevel@tonic-gateC<*foo = \$bar> makes the SCALAR portions of two distinct typeglobs 131*0Sstevel@tonic-gaterefer to the same scalar value. This means that the following code: 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate $bar = 1; 134*0Sstevel@tonic-gate *foo = \$bar; # Make $foo an alias for $bar 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate { 137*0Sstevel@tonic-gate local $bar = 2; # Restrict changes to block 138*0Sstevel@tonic-gate print $foo; # Prints '1'! 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gateWould print '1', because C<$foo> holds a reference to the I<original> 142*0Sstevel@tonic-gateC<$bar> -- the one that was stuffed away by C<local()> and which will be 143*0Sstevel@tonic-gaterestored when the block ends. Because variables are accessed through the 144*0Sstevel@tonic-gatetypeglob, you can use C<*foo = *bar> to create an alias which can be 145*0Sstevel@tonic-gatelocalized. (But be aware that this means you can't have a separate 146*0Sstevel@tonic-gateC<@foo> and C<@bar>, etc.) 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gateWhat makes all of this important is that the Exporter module uses glob 149*0Sstevel@tonic-gatealiasing as the import/export mechanism. Whether or not you can properly 150*0Sstevel@tonic-gatelocalize a variable that has been exported from a module depends on how 151*0Sstevel@tonic-gateit was exported: 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate @EXPORT = qw($FOO); # Usual form, can't be localized 154*0Sstevel@tonic-gate @EXPORT = qw(*FOO); # Can be localized 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gateYou can work around the first case by using the fully qualified name 157*0Sstevel@tonic-gate(C<$Package::FOO>) where you need a local value, or by overriding it 158*0Sstevel@tonic-gateby saying C<*FOO = *Package::FOO> in your script. 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gateThe C<*x = \$y> mechanism may be used to pass and return cheap references 161*0Sstevel@tonic-gateinto or from subroutines if you don't want to copy the whole 162*0Sstevel@tonic-gatething. It only works when assigning to dynamic variables, not 163*0Sstevel@tonic-gatelexicals. 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate %some_hash = (); # can't be my() 166*0Sstevel@tonic-gate *some_hash = fn( \%another_hash ); 167*0Sstevel@tonic-gate sub fn { 168*0Sstevel@tonic-gate local *hashsym = shift; 169*0Sstevel@tonic-gate # now use %hashsym normally, and you 170*0Sstevel@tonic-gate # will affect the caller's %another_hash 171*0Sstevel@tonic-gate my %nhash = (); # do what you want 172*0Sstevel@tonic-gate return \%nhash; 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gateOn return, the reference will overwrite the hash slot in the 176*0Sstevel@tonic-gatesymbol table specified by the *some_hash typeglob. This 177*0Sstevel@tonic-gateis a somewhat tricky way of passing around references cheaply 178*0Sstevel@tonic-gatewhen you don't want to have to remember to dereference variables 179*0Sstevel@tonic-gateexplicitly. 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gateAnother use of symbol tables is for making "constant" scalars. 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate *PI = \3.14159265358979; 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gateNow you cannot alter C<$PI>, which is probably a good thing all in all. 186*0Sstevel@tonic-gateThis isn't the same as a constant subroutine, which is subject to 187*0Sstevel@tonic-gateoptimization at compile-time. A constant subroutine is one prototyped 188*0Sstevel@tonic-gateto take no arguments and to return a constant expression. See 189*0Sstevel@tonic-gateL<perlsub> for details on these. The C<use constant> pragma is a 190*0Sstevel@tonic-gateconvenient shorthand for these. 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gateYou can say C<*foo{PACKAGE}> and C<*foo{NAME}> to find out what name and 193*0Sstevel@tonic-gatepackage the *foo symbol table entry comes from. This may be useful 194*0Sstevel@tonic-gatein a subroutine that gets passed typeglobs as arguments: 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate sub identify_typeglob { 197*0Sstevel@tonic-gate my $glob = shift; 198*0Sstevel@tonic-gate print 'You gave me ', *{$glob}{PACKAGE}, '::', *{$glob}{NAME}, "\n"; 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate identify_typeglob *foo; 201*0Sstevel@tonic-gate identify_typeglob *bar::baz; 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gateThis prints 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate You gave me main::foo 206*0Sstevel@tonic-gate You gave me bar::baz 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gateThe C<*foo{THING}> notation can also be used to obtain references to the 209*0Sstevel@tonic-gateindividual elements of *foo. See L<perlref>. 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gateSubroutine definitions (and declarations, for that matter) need 212*0Sstevel@tonic-gatenot necessarily be situated in the package whose symbol table they 213*0Sstevel@tonic-gateoccupy. You can define a subroutine outside its package by 214*0Sstevel@tonic-gateexplicitly qualifying the name of the subroutine: 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate package main; 217*0Sstevel@tonic-gate sub Some_package::foo { ... } # &foo defined in Some_package 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gateThis is just a shorthand for a typeglob assignment at compile time: 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate BEGIN { *Some_package::foo = sub { ... } } 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gateand is I<not> the same as writing: 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate { 226*0Sstevel@tonic-gate package Some_package; 227*0Sstevel@tonic-gate sub foo { ... } 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gateIn the first two versions, the body of the subroutine is 231*0Sstevel@tonic-gatelexically in the main package, I<not> in Some_package. So 232*0Sstevel@tonic-gatesomething like this: 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate package main; 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate $Some_package::name = "fred"; 237*0Sstevel@tonic-gate $main::name = "barney"; 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate sub Some_package::foo { 240*0Sstevel@tonic-gate print "in ", __PACKAGE__, ": \$name is '$name'\n"; 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate Some_package::foo(); 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gateprints: 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate in main: $name is 'barney' 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gaterather than: 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate in Some_package: $name is 'fred' 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gateThis also has implications for the use of the SUPER:: qualifier 254*0Sstevel@tonic-gate(see L<perlobj>). 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate=head2 BEGIN, CHECK, INIT and END 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gateFour specially named code blocks are executed at the beginning and at the end 259*0Sstevel@tonic-gateof a running Perl program. These are the C<BEGIN>, C<CHECK>, C<INIT>, and 260*0Sstevel@tonic-gateC<END> blocks. 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gateThese code blocks can be prefixed with C<sub> to give the appearance of a 263*0Sstevel@tonic-gatesubroutine (although this is not considered good style). One should note 264*0Sstevel@tonic-gatethat these code blocks don't really exist as named subroutines (despite 265*0Sstevel@tonic-gatetheir appearance). The thing that gives this away is the fact that you can 266*0Sstevel@tonic-gatehave B<more than one> of these code blocks in a program, and they will get 267*0Sstevel@tonic-gateB<all> executed at the appropriate moment. So you can't execute any of 268*0Sstevel@tonic-gatethese code blocks by name. 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gateA C<BEGIN> code block is executed as soon as possible, that is, the moment 271*0Sstevel@tonic-gateit is completely defined, even before the rest of the containing file (or 272*0Sstevel@tonic-gatestring) is parsed. You may have multiple C<BEGIN> blocks within a file (or 273*0Sstevel@tonic-gateeval'ed string) -- they will execute in order of definition. Because a C<BEGIN> 274*0Sstevel@tonic-gatecode block executes immediately, it can pull in definitions of subroutines 275*0Sstevel@tonic-gateand such from other files in time to be visible to the rest of the compile 276*0Sstevel@tonic-gateand run time. Once a C<BEGIN> has run, it is immediately undefined and any 277*0Sstevel@tonic-gatecode it used is returned to Perl's memory pool. 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gateIt should be noted that C<BEGIN> code blocks B<are> executed inside string 280*0Sstevel@tonic-gateC<eval()>'s. The C<CHECK> and C<INIT> code blocks are B<not> executed inside 281*0Sstevel@tonic-gatea string eval, which e.g. can be a problem in a mod_perl environment. 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gateAn C<END> code block is executed as late as possible, that is, after 284*0Sstevel@tonic-gateperl has finished running the program and just before the interpreter 285*0Sstevel@tonic-gateis being exited, even if it is exiting as a result of a die() function. 286*0Sstevel@tonic-gate(But not if it's polymorphing into another program via C<exec>, or 287*0Sstevel@tonic-gatebeing blown out of the water by a signal--you have to trap that yourself 288*0Sstevel@tonic-gate(if you can).) You may have multiple C<END> blocks within a file--they 289*0Sstevel@tonic-gatewill execute in reverse order of definition; that is: last in, first 290*0Sstevel@tonic-gateout (LIFO). C<END> blocks are not executed when you run perl with the 291*0Sstevel@tonic-gateC<-c> switch, or if compilation fails. 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gateNote that C<END> code blocks are B<not> executed at the end of a string 294*0Sstevel@tonic-gateC<eval()>: if any C<END> code blocks are created in a string C<eval()>, 295*0Sstevel@tonic-gatethey will be executed just as any other C<END> code block of that package 296*0Sstevel@tonic-gatein LIFO order just before the interpreter is being exited. 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gateInside an C<END> code block, C<$?> contains the value that the program is 299*0Sstevel@tonic-gategoing to pass to C<exit()>. You can modify C<$?> to change the exit 300*0Sstevel@tonic-gatevalue of the program. Beware of changing C<$?> by accident (e.g. by 301*0Sstevel@tonic-gaterunning something via C<system>). 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gateC<CHECK> and C<INIT> code blocks are useful to catch the transition between 304*0Sstevel@tonic-gatethe compilation phase and the execution phase of the main program. 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gateC<CHECK> code blocks are run just after the B<initial> Perl compile phase ends 307*0Sstevel@tonic-gateand before the run time begins, in LIFO order. C<CHECK> code blocks are used 308*0Sstevel@tonic-gatein the Perl compiler suite to save the compiled state of the program. 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gateC<INIT> blocks are run just before the Perl runtime begins execution, in 311*0Sstevel@tonic-gate"first in, first out" (FIFO) order. For example, the code generators 312*0Sstevel@tonic-gatedocumented in L<perlcc> make use of C<INIT> blocks to initialize and 313*0Sstevel@tonic-gateresolve pointers to XSUBs. 314*0Sstevel@tonic-gate 315*0Sstevel@tonic-gateWhen you use the B<-n> and B<-p> switches to Perl, C<BEGIN> and 316*0Sstevel@tonic-gateC<END> work just as they do in B<awk>, as a degenerate case. 317*0Sstevel@tonic-gateBoth C<BEGIN> and C<CHECK> blocks are run when you use the B<-c> 318*0Sstevel@tonic-gateswitch for a compile-only syntax check, although your main code 319*0Sstevel@tonic-gateis not. 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gateThe B<begincheck> program makes it all clear, eventually: 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate #!/usr/bin/perl 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate # begincheck 326*0Sstevel@tonic-gate 327*0Sstevel@tonic-gate print " 8. Ordinary code runs at runtime.\n"; 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate END { print "14. So this is the end of the tale.\n" } 330*0Sstevel@tonic-gate INIT { print " 5. INIT blocks run FIFO just before runtime.\n" } 331*0Sstevel@tonic-gate CHECK { print " 4. So this is the fourth line.\n" } 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate print " 9. It runs in order, of course.\n"; 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gate BEGIN { print " 1. BEGIN blocks run FIFO during compilation.\n" } 336*0Sstevel@tonic-gate END { print "13. Read perlmod for the rest of the story.\n" } 337*0Sstevel@tonic-gate CHECK { print " 3. CHECK blocks run LIFO at compilation's end.\n" } 338*0Sstevel@tonic-gate INIT { print " 6. Run this again, using Perl's -c switch.\n" } 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate print "10. This is anti-obfuscated code.\n"; 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate END { print "12. END blocks run LIFO at quitting time.\n" } 343*0Sstevel@tonic-gate BEGIN { print " 2. So this line comes out second.\n" } 344*0Sstevel@tonic-gate INIT { print " 7. You'll see the difference right away.\n" } 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gate print "11. It merely _looks_ like it should be confusing.\n"; 347*0Sstevel@tonic-gate 348*0Sstevel@tonic-gate __END__ 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate=head2 Perl Classes 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gateThere is no special class syntax in Perl, but a package may act 353*0Sstevel@tonic-gateas a class if it provides subroutines to act as methods. Such a 354*0Sstevel@tonic-gatepackage may also derive some of its methods from another class (package) 355*0Sstevel@tonic-gateby listing the other package name(s) in its global @ISA array (which 356*0Sstevel@tonic-gatemust be a package global, not a lexical). 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gateFor more on this, see L<perltoot> and L<perlobj>. 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate=head2 Perl Modules 361*0Sstevel@tonic-gate 362*0Sstevel@tonic-gateA module is just a set of related functions in a library file, i.e., 363*0Sstevel@tonic-gatea Perl package with the same name as the file. It is specifically 364*0Sstevel@tonic-gatedesigned to be reusable by other modules or programs. It may do this 365*0Sstevel@tonic-gateby providing a mechanism for exporting some of its symbols into the 366*0Sstevel@tonic-gatesymbol table of any package using it, or it may function as a class 367*0Sstevel@tonic-gatedefinition and make its semantics available implicitly through 368*0Sstevel@tonic-gatemethod calls on the class and its objects, without explicitly 369*0Sstevel@tonic-gateexporting anything. Or it can do a little of both. 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gateFor example, to start a traditional, non-OO module called Some::Module, 372*0Sstevel@tonic-gatecreate a file called F<Some/Module.pm> and start with this template: 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gate package Some::Module; # assumes Some/Module.pm 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gate use strict; 377*0Sstevel@tonic-gate use warnings; 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate BEGIN { 380*0Sstevel@tonic-gate use Exporter (); 381*0Sstevel@tonic-gate our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); 382*0Sstevel@tonic-gate 383*0Sstevel@tonic-gate # set the version for version checking 384*0Sstevel@tonic-gate $VERSION = 1.00; 385*0Sstevel@tonic-gate # if using RCS/CVS, this may be preferred 386*0Sstevel@tonic-gate $VERSION = sprintf "%d.%03d", q$Revision: 1.1 $ =~ /(\d+)/g; 387*0Sstevel@tonic-gate 388*0Sstevel@tonic-gate @ISA = qw(Exporter); 389*0Sstevel@tonic-gate @EXPORT = qw(&func1 &func2 &func4); 390*0Sstevel@tonic-gate %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], 391*0Sstevel@tonic-gate 392*0Sstevel@tonic-gate # your exported package globals go here, 393*0Sstevel@tonic-gate # as well as any optionally exported functions 394*0Sstevel@tonic-gate @EXPORT_OK = qw($Var1 %Hashit &func3); 395*0Sstevel@tonic-gate } 396*0Sstevel@tonic-gate our @EXPORT_OK; 397*0Sstevel@tonic-gate 398*0Sstevel@tonic-gate # exported package globals go here 399*0Sstevel@tonic-gate our $Var1; 400*0Sstevel@tonic-gate our %Hashit; 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate # non-exported package globals go here 403*0Sstevel@tonic-gate our @more; 404*0Sstevel@tonic-gate our $stuff; 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate # initialize package globals, first exported ones 407*0Sstevel@tonic-gate $Var1 = ''; 408*0Sstevel@tonic-gate %Hashit = (); 409*0Sstevel@tonic-gate 410*0Sstevel@tonic-gate # then the others (which are still accessible as $Some::Module::stuff) 411*0Sstevel@tonic-gate $stuff = ''; 412*0Sstevel@tonic-gate @more = (); 413*0Sstevel@tonic-gate 414*0Sstevel@tonic-gate # all file-scoped lexicals must be created before 415*0Sstevel@tonic-gate # the functions below that use them. 416*0Sstevel@tonic-gate 417*0Sstevel@tonic-gate # file-private lexicals go here 418*0Sstevel@tonic-gate my $priv_var = ''; 419*0Sstevel@tonic-gate my %secret_hash = (); 420*0Sstevel@tonic-gate 421*0Sstevel@tonic-gate # here's a file-private function as a closure, 422*0Sstevel@tonic-gate # callable as &$priv_func; it cannot be prototyped. 423*0Sstevel@tonic-gate my $priv_func = sub { 424*0Sstevel@tonic-gate # stuff goes here. 425*0Sstevel@tonic-gate }; 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gate # make all your functions, whether exported or not; 428*0Sstevel@tonic-gate # remember to put something interesting in the {} stubs 429*0Sstevel@tonic-gate sub func1 {} # no prototype 430*0Sstevel@tonic-gate sub func2() {} # proto'd void 431*0Sstevel@tonic-gate sub func3($$) {} # proto'd to 2 scalars 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate # this one isn't exported, but could be called! 434*0Sstevel@tonic-gate sub func4(\%) {} # proto'd to 1 hash ref 435*0Sstevel@tonic-gate 436*0Sstevel@tonic-gate END { } # module clean-up code here (global destructor) 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate ## YOUR CODE GOES HERE 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate 1; # don't forget to return a true value from the file 441*0Sstevel@tonic-gate 442*0Sstevel@tonic-gateThen go on to declare and use your variables in functions without 443*0Sstevel@tonic-gateany qualifications. See L<Exporter> and the L<perlmodlib> for 444*0Sstevel@tonic-gatedetails on mechanics and style issues in module creation. 445*0Sstevel@tonic-gate 446*0Sstevel@tonic-gatePerl modules are included into your program by saying 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate use Module; 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gateor 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate use Module LIST; 453*0Sstevel@tonic-gate 454*0Sstevel@tonic-gateThis is exactly equivalent to 455*0Sstevel@tonic-gate 456*0Sstevel@tonic-gate BEGIN { require Module; import Module; } 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gateor 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate BEGIN { require Module; import Module LIST; } 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gateAs a special case 463*0Sstevel@tonic-gate 464*0Sstevel@tonic-gate use Module (); 465*0Sstevel@tonic-gate 466*0Sstevel@tonic-gateis exactly equivalent to 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate BEGIN { require Module; } 469*0Sstevel@tonic-gate 470*0Sstevel@tonic-gateAll Perl module files have the extension F<.pm>. The C<use> operator 471*0Sstevel@tonic-gateassumes this so you don't have to spell out "F<Module.pm>" in quotes. 472*0Sstevel@tonic-gateThis also helps to differentiate new modules from old F<.pl> and 473*0Sstevel@tonic-gateF<.ph> files. Module names are also capitalized unless they're 474*0Sstevel@tonic-gatefunctioning as pragmas; pragmas are in effect compiler directives, 475*0Sstevel@tonic-gateand are sometimes called "pragmatic modules" (or even "pragmata" 476*0Sstevel@tonic-gateif you're a classicist). 477*0Sstevel@tonic-gate 478*0Sstevel@tonic-gateThe two statements: 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate require SomeModule; 481*0Sstevel@tonic-gate require "SomeModule.pm"; 482*0Sstevel@tonic-gate 483*0Sstevel@tonic-gatediffer from each other in two ways. In the first case, any double 484*0Sstevel@tonic-gatecolons in the module name, such as C<Some::Module>, are translated 485*0Sstevel@tonic-gateinto your system's directory separator, usually "/". The second 486*0Sstevel@tonic-gatecase does not, and would have to be specified literally. The other 487*0Sstevel@tonic-gatedifference is that seeing the first C<require> clues in the compiler 488*0Sstevel@tonic-gatethat uses of indirect object notation involving "SomeModule", as 489*0Sstevel@tonic-gatein C<$ob = purge SomeModule>, are method calls, not function calls. 490*0Sstevel@tonic-gate(Yes, this really can make a difference.) 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gateBecause the C<use> statement implies a C<BEGIN> block, the importing 493*0Sstevel@tonic-gateof semantics happens as soon as the C<use> statement is compiled, 494*0Sstevel@tonic-gatebefore the rest of the file is compiled. This is how it is able 495*0Sstevel@tonic-gateto function as a pragma mechanism, and also how modules are able to 496*0Sstevel@tonic-gatedeclare subroutines that are then visible as list or unary operators for 497*0Sstevel@tonic-gatethe rest of the current file. This will not work if you use C<require> 498*0Sstevel@tonic-gateinstead of C<use>. With C<require> you can get into this problem: 499*0Sstevel@tonic-gate 500*0Sstevel@tonic-gate require Cwd; # make Cwd:: accessible 501*0Sstevel@tonic-gate $here = Cwd::getcwd(); 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate use Cwd; # import names from Cwd:: 504*0Sstevel@tonic-gate $here = getcwd(); 505*0Sstevel@tonic-gate 506*0Sstevel@tonic-gate require Cwd; # make Cwd:: accessible 507*0Sstevel@tonic-gate $here = getcwd(); # oops! no main::getcwd() 508*0Sstevel@tonic-gate 509*0Sstevel@tonic-gateIn general, C<use Module ()> is recommended over C<require Module>, 510*0Sstevel@tonic-gatebecause it determines module availability at compile time, not in the 511*0Sstevel@tonic-gatemiddle of your program's execution. An exception would be if two modules 512*0Sstevel@tonic-gateeach tried to C<use> each other, and each also called a function from 513*0Sstevel@tonic-gatethat other module. In that case, it's easy to use C<require> instead. 514*0Sstevel@tonic-gate 515*0Sstevel@tonic-gatePerl packages may be nested inside other package names, so we can have 516*0Sstevel@tonic-gatepackage names containing C<::>. But if we used that package name 517*0Sstevel@tonic-gatedirectly as a filename it would make for unwieldy or impossible 518*0Sstevel@tonic-gatefilenames on some systems. Therefore, if a module's name is, say, 519*0Sstevel@tonic-gateC<Text::Soundex>, then its definition is actually found in the library 520*0Sstevel@tonic-gatefile F<Text/Soundex.pm>. 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gatePerl modules always have a F<.pm> file, but there may also be 523*0Sstevel@tonic-gatedynamically linked executables (often ending in F<.so>) or autoloaded 524*0Sstevel@tonic-gatesubroutine definitions (often ending in F<.al>) associated with the 525*0Sstevel@tonic-gatemodule. If so, these will be entirely transparent to the user of 526*0Sstevel@tonic-gatethe module. It is the responsibility of the F<.pm> file to load 527*0Sstevel@tonic-gate(or arrange to autoload) any additional functionality. For example, 528*0Sstevel@tonic-gatealthough the POSIX module happens to do both dynamic loading and 529*0Sstevel@tonic-gateautoloading, the user can say just C<use POSIX> to get it all. 530*0Sstevel@tonic-gate 531*0Sstevel@tonic-gate=head2 Making your module threadsafe 532*0Sstevel@tonic-gate 533*0Sstevel@tonic-gateSince 5.6.0, Perl has had support for a new type of threads called 534*0Sstevel@tonic-gateinterpreter threads (ithreads). These threads can be used explicitly 535*0Sstevel@tonic-gateand implicitly. 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gateIthreads work by cloning the data tree so that no data is shared 538*0Sstevel@tonic-gatebetween different threads. These threads can be used by using the C<threads> 539*0Sstevel@tonic-gatemodule or by doing fork() on win32 (fake fork() support). When a 540*0Sstevel@tonic-gatethread is cloned all Perl data is cloned, however non-Perl data cannot 541*0Sstevel@tonic-gatebe cloned automatically. Perl after 5.7.2 has support for the C<CLONE> 542*0Sstevel@tonic-gatespecial subroutine . In C<CLONE> you can do whatever you need to do, 543*0Sstevel@tonic-gatelike for example handle the cloning of non-Perl data, if necessary. 544*0Sstevel@tonic-gateC<CLONE> will be executed once for every package that has it defined 545*0Sstevel@tonic-gate(or inherits it). It will be called in the context of the new thread, 546*0Sstevel@tonic-gateso all modifications are made in the new area. 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gateIf you want to CLONE all objects you will need to keep track of them per 549*0Sstevel@tonic-gatepackage. This is simply done using a hash and Scalar::Util::weaken(). 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate=head1 SEE ALSO 552*0Sstevel@tonic-gate 553*0Sstevel@tonic-gateSee L<perlmodlib> for general style issues related to building Perl 554*0Sstevel@tonic-gatemodules and classes, as well as descriptions of the standard library 555*0Sstevel@tonic-gateand CPAN, L<Exporter> for how Perl's standard import/export mechanism 556*0Sstevel@tonic-gateworks, L<perltoot> and L<perltooc> for an in-depth tutorial on 557*0Sstevel@tonic-gatecreating classes, L<perlobj> for a hard-core reference document on 558*0Sstevel@tonic-gateobjects, L<perlsub> for an explanation of functions and scoping, 559*0Sstevel@tonic-gateand L<perlxstut> and L<perlguts> for more information on writing 560*0Sstevel@tonic-gateextension modules. 561