This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
sort pragma tweaks.
[perl5.git] / pod / perlmod.pod
index 676940e..ca59e29 100644 (file)
@@ -8,7 +8,7 @@ perlmod - Perl modules (packages and symbol tables)
 
 Perl provides a mechanism for alternative namespaces to protect
 packages from stomping on each other's variables.  In fact, there's
-really no such thing as a global variable in Perl .  The package
+really no such thing as a global variable in Perl.  The package
 statement declares the compilation unit as being in the given
 namespace.  The scope of the package declaration is from the
 declaration itself through the end of the enclosing block, C<eval>,
@@ -61,8 +61,8 @@ as a pattern match, a substitution, or a transliteration.
 Variables beginning with underscore used to be forced into package
 main, but we decided it was more useful for package writers to be able
 to use leading underscore to indicate private variables and method names.
-$_ is still global though.  See also L<perlvar/"Technical Note on the
-Syntax of Variable Names">.
+$_ is still global though.  See also
+L<perlvar/"Technical Note on the Syntax of Variable Names">.
 
 C<eval>ed strings are compiled in the package in which the eval() was
 compiled.  (Assignments to C<$SIG{}>, however, assume the signal
@@ -96,6 +96,12 @@ table lookups at compile time:
     local *main::foo    = *main::bar;
     local $main::{foo}  = $main::{bar};
 
+(Be sure to note the B<vast> difference between the second line above
+and C<local $main::foo = $main::bar>. The former is accessing the hash
+C<%main::>, which is the symbol table of package C<main>. The latter is
+simply assigning scalar C<$bar> in package C<main> to scalar C<$foo> of
+the same package.)
+
 You can use this to print out all the variables in a package, for
 instance.  The standard but antiquated F<dumpvar.pl> library and
 the CPAN module Devel::Symdump make use of this.
@@ -114,7 +120,43 @@ subroutine, assign a reference instead:
 Which makes $richard and $dick the same variable, but leaves
 @richard and @dick as separate arrays.  Tricky, eh?
 
-This mechanism may be used to pass and return cheap references
+There is one subtle difference between the following statements:
+
+    *foo = *bar;
+    *foo = \$bar;
+
+C<*foo = *bar> makes the typeglobs themselves synonymous while
+C<*foo = \$bar> makes the SCALAR portions of two distinct typeglobs
+refer to the same scalar value. This means that the following code:
+
+    $bar = 1;
+    *foo = \$bar;       # Make $foo an alias for $bar
+
+    {
+        local $bar = 2; # Restrict changes to block
+        print $foo;     # Prints '1'!
+    }
+
+Would print '1', because C<$foo> holds a reference to the I<original>
+C<$bar> -- the one that was stuffed away by C<local()> and which will be
+restored when the block ends. Because variables are accessed through the
+typeglob, you can use C<*foo = *bar> to create an alias which can be
+localized. (But be aware that this means you can't have a separate
+C<@foo> and C<@bar>, etc.)
+
+What makes all of this important is that the Exporter module uses glob
+aliasing as the import/export mechanism. Whether or not you can properly
+localize a variable that has been exported from a module depends on how
+it was exported:
+
+    @EXPORT = qw($FOO); # Usual form, can't be localized
+    @EXPORT = qw(*FOO); # Can be localized
+
+You can work around the first case by using the fully qualified name 
+(C<$Package::FOO>) where you need a local value, or by overriding it
+by saying C<*FOO = *Package::FOO> in your script.
+
+The C<*x = \$y> mechanism may be used to pass and return cheap references
 into or from subroutines if you don't want to copy the whole
 thing.  It only works when assigning to dynamic variables, not
 lexicals.
@@ -139,7 +181,7 @@ Another use of symbol tables is for making "constant" scalars.
 
     *PI = \3.14159265358979;
 
-Now you cannot alter $PI, which is probably a good thing all in all.
+Now you cannot alter C<$PI>, which is probably a good thing all in all.
 This isn't the same as a constant subroutine, which is subject to
 optimization at compile-time.  A constant subroutine is one prototyped
 to take no arguments and to return a constant expression.  See 
@@ -233,7 +275,7 @@ being blown out of the water by a signal--you have to trap that yourself
 (if you can).)  You may have multiple C<END> blocks within a file--they
 will execute in reverse order of definition; that is: last in, first
 out (LIFO).  C<END> blocks are not executed when you run perl with the
-C<-c> switch.
+C<-c> switch, or if compilation fails.
 
 Inside an C<END> subroutine, C<$?> contains the value that the program is
 going to pass to C<exit()>.  You can modify C<$?> to change the exit
@@ -304,6 +346,10 @@ create a file called F<Some/Module.pm> and start with this template:
     }
     our @EXPORT_OK;
 
+    # exported package globals go here
+    our $Var1;
+    our %Hashit;
+
     # non-exported package globals go here
     our @more;
     our $stuff;
@@ -433,12 +479,29 @@ the module.  It is the responsibility of the F<.pm> file to load
 although the POSIX module happens to do both dynamic loading and
 autoloading, the user can say just C<use POSIX> to get it all.
 
+=head2 Making your module threadsafe
+
+Perl has since 5.6.0 support for a new type of threads called
+interpreter threads. These threads can be used explicitly and implicitly.
+
+Ithreads work by cloning the data tree so that no data is shared
+between different threads. These threads can be used using the threads
+module or by doing fork() on win32 (fake fork() support). When a thread is
+cloned all perl data is cloned, however non perl data cannot be cloned.
+Perl after 5.7.2 has support for the C<CLONE> keyword. C<CLONE> will be
+executed once for every package that has it defined (or inherits it).
+It will be called in the context of the new thread, so all modifications
+are made in the new area.
+
+If you want to CLONE all objects you will need to keep track of them per
+package. This is simply done using a hash and Scalar::Util::weaken().
+
 =head1 SEE ALSO
 
 See L<perlmodlib> for general style issues related to building Perl
 modules and classes, as well as descriptions of the standard library
 and CPAN, L<Exporter> for how Perl's standard import/export mechanism
-works, L<perltoot> and L<perltootc> for an in-depth tutorial on
+works, L<perltoot> and L<perltooc> for an in-depth tutorial on
 creating classes, L<perlobj> for a hard-core reference document on
 objects, L<perlsub> for an explanation of functions and scoping,
 and L<perlxstut> and L<perlguts> for more information on writing