This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make qr/(?[ ])/ work in UTF-8 locales
[perl5.git] / pod / perlmod.pod
index e31b0b9..0ed4bd9 100644 (file)
@@ -4,6 +4,27 @@ perlmod - Perl modules (packages and symbol tables)
 
 =head1 DESCRIPTION
 
+=head2 Is this the document you were after?
+
+There are other documents which might contain the information that you're
+looking for:
+
+=over 2
+
+=item This doc
+
+Perl's packages, namespaces, and some info on classes.
+
+=item L<perlnewmod>
+
+Tutorial on making a new module.
+
+=item L<perlmodstyle>
+
+Best practices for making a new module.
+
+=back
+
 =head2 Packages
 X<package> X<namespace> X<variable, global> X<global variable> X<global>
 
@@ -195,7 +216,8 @@ in a subroutine that gets passed typeglobs as arguments:
 
     sub identify_typeglob {
         my $glob = shift;
-        print 'You gave me ', *{$glob}{PACKAGE}, '::', *{$glob}{NAME}, "\n";
+        print 'You gave me ', *{$glob}{PACKAGE},
+            '::', *{$glob}{NAME}, "\n";
     }
     identify_typeglob *foo;
     identify_typeglob *bar::baz;
@@ -307,7 +329,7 @@ the main program.
 
 C<UNITCHECK> blocks are run just after the unit which defined them has
 been compiled.  The main program file and each module it loads are
-compilation units, as are string C<eval>s, code compiled using the
+compilation units, as are string C<eval>s, run-time code compiled using the
 C<(?{ })> construct in a regex, calls to C<do FILE>, C<require FILE>,
 and code after the C<-e> switch on the command line.
 
@@ -368,7 +390,7 @@ The B<begincheck> program makes it all clear, eventually:
   }
   INIT { print  " 9.   You'll see the difference right away.\n" }
 
-  print         "13.   It merely _looks_ like it should be confusing.\n";
+  print         "13.   It only _looks_ like it should be confusing.\n";
 
   __END__
 
@@ -404,65 +426,50 @@ create a file called F<Some/Module.pm> and start with this template:
     use warnings;
 
     BEGIN {
-        use Exporter   ();
-        our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
+        require Exporter;
 
         # set the version for version checking
-        $VERSION     = 1.00;
-        # if using RCS/CVS, this may be preferred
-        $VERSION = sprintf "%d.%03d", q$Revision: 1.1 $ =~ /(\d+)/g;
+        our $VERSION     = 1.00;
 
-        @ISA         = qw(Exporter);
-        @EXPORT      = qw(&func1 &func2 &func4);
-        %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
+        # Inherit from Exporter to export functions and variables
+        our @ISA         = qw(Exporter);
 
-        # your exported package globals go here,
-        # as well as any optionally exported functions
-        @EXPORT_OK   = qw($Var1 %Hashit &func3);
+        # Functions and variables which are exported by default
+        our @EXPORT      = qw(func1 func2);
+
+        # Functions and variables which can be optionally exported
+        our @EXPORT_OK   = qw($Var1 %Hashit func3);
     }
-    our @EXPORT_OK;
 
     # exported package globals go here
-    our $Var1;
-    our %Hashit;
+    our $Var1    = '';
+    our %Hashit  = ();
 
     # non-exported package globals go here
-    our @more;
-    our $stuff;
-
-    # initialize package globals, first exported ones
-    $Var1   = '';
-    %Hashit = ();
+    # (they are still accessible as $Some::Module::stuff)
+    our @more    = ();
+    our $stuff   = '';
 
-    # then the others (which are still accessible as $Some::Module::stuff)
-    $stuff  = '';
-    @more   = ();
-
-    # all file-scoped lexicals must be created before
-    # the functions below that use them.
-
-    # file-private lexicals go here
+    # file-private lexicals go here, before any functions which use them
     my $priv_var    = '';
     my %secret_hash = ();
 
     # here's a file-private function as a closure,
-    # callable as &$priv_func;  it cannot be prototyped.
+    # callable as $priv_func->();
     my $priv_func = sub {
-        # stuff goes here.
+        ...
     };
 
     # make all your functions, whether exported or not;
     # remember to put something interesting in the {} stubs
-    sub func1      {}    # no prototype
-    sub func2()    {}    # proto'd void
-    sub func3($$)  {}    # proto'd to 2 scalars
-
-    # this one isn't exported, but could be called!
-    sub func4(\%)  {}    # proto'd to 1 hash ref
+    sub func1      { ... }
+    sub func2      { ... }
 
-    END { }       # module clean-up code here (global destructor)
+    # this one isn't exported, but could be called directly
+    # as Some::Module::func3()
+    sub func3      { ... }
 
-    ## YOUR CODE GOES HERE
+    END { ... }       # module clean-up code here (global destructor)
 
     1;  # don't forget to return a true value from the file
 
@@ -480,11 +487,11 @@ or
 
 This is exactly equivalent to
 
-    BEGIN { require Module; import Module; }
+    BEGIN { require 'Module.pm'; 'Module'->import; }
 
 or
 
-    BEGIN { require Module; import Module LIST; }
+    BEGIN { require 'Module.pm'; 'Module'->import( LIST ); }
 
 As a special case
 
@@ -492,7 +499,7 @@ As a special case
 
 is exactly equivalent to
 
-    BEGIN { require Module; }
+    BEGIN { require 'Module.pm'; }
 
 All Perl module files have the extension F<.pm>.  The C<use> operator
 assumes this so you don't have to spell out "F<Module.pm>" in quotes.
@@ -560,15 +567,14 @@ X<threadsafe> X<thread safe>
 X<module, threadsafe> X<module, thread safe>
 X<CLONE> X<CLONE_SKIP> X<thread> X<threads> X<ithread>
 
-Since 5.6.0, Perl has had support for a new type of threads called
-interpreter threads (ithreads). These threads can be used explicitly
-and implicitly.
+Perl supports a type of threads called interpreter threads (ithreads).
+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 by using the C<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 automatically.  Perl after 5.7.2 has support for the C<CLONE>
+be cloned automatically.  Perl after 5.8.0 has support for the C<CLONE>
 special subroutine.  In C<CLONE> you can do whatever
 you need to do,
 like for example handle the cloning of non-Perl data, if necessary.