X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/ee8c7f5465f003860e2347a2946abacac39bd9b9..c079d275c4f9c514ec0de3de1aef066d6f4595aa:/pod/perlmod.pod diff --git a/pod/perlmod.pod b/pod/perlmod.pod index 6bec46b..29ad67c 100644 --- a/pod/perlmod.pod +++ b/pod/perlmod.pod @@ -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, @@ -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. +$_ is still global though. See also +L. Ced 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 difference between the second line above +and C. The former is accessing the hash +C<%main::>, which is the symbol table of package C
. The latter is +simply assigning scalar C<$bar> in package C
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 library and the CPAN module Devel::Symdump make use of this. @@ -139,7 +145,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 @@ -304,6 +310,10 @@ create a file called F 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,6 +443,23 @@ 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 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 keyword. C 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 for general style issues related to building Perl