|
NAMEPragmatic - Adds pragmata to ExporterSYNOPSISIn module MyModule.pm:package MyModule; require Pragmatic; @ISA = qw (Pragmatic); %PRAGMATA = (mypragma => sub {...}); In other files which wish to use MyModule: use MyModule qw (-mypragma); # Execute pragma at import time use MyModule qw (-mypragma=1,2,3); # Pass pragma argument list DESCRIPTIONPragmatic implements a default "import" method for processing pragmata before passing the rest of the import to Exporter.Perl automatically calls the "import" method when processing a "use" statement for a module. Modules and "use" are documented in perlfunc and perlmod. (Do not confuse Pragmatic with pragmatic modules, such as less, strict and the like. They are standalone pragmata, and are not associated with any other module.) Using Pragmatic ModulesUsing Pragmatic modules is very simple. To invoke any particular pragma for a given module, include it in the argument list to "use" preceded by a hyphen:use MyModule qw (-mypragma); "Pragmatic::import" will filter out these arguments, and pass the remainder of the argument list from the "use" statement to "Exporter::import" (actually, to "Exporter::export_to_level" so that Pragmatic is transparent). If you want to pass the pragma arguments, use syntax similar to that of the -M switch to perl (see perlrun): use MyModule qw (-mypragma=abc,1,2,3); If there are any warnings or fatal errors, they will appear to come from the "use" statement, not from "Pragmatic::import". Writing Pragmatic ModulesWriting Pragmatic modules with Pragmatic is straight-forward. First, "require Pragmatic" (you could "use" it instead, but it exports nothing, so there is little to gain thereby). Declare a package global %PRAGMATA, the keys of which are the names of the pragmata and their corresponding values the code references to invoke. Like this:package MyPackage; require Pragmatic; use strict; use vars qw (%PRAGMATA); sub something_else { 1; } %PRAGMATA = (first => sub { print "@_: first\n"; }, second => sub { $SOME_GLOBAL = 1; }, third => \&something_else, fourth => 'name_of_sub'); When a pragma is given in a "use" statement, the leading hyphen is removed, and the code reference corresponding to that key in %PRAGMATA, or a subroutine with the value's name, is invoked with the name of the package as the first member of the argument list (this is the same as what happens with "import"). Additionally, any arguments given by the caller are included (see "Using Pragmatic Modules", above). EXAMPLESUsing Pragmatic Modules
Writing Pragmatic Modules
SEE ALSOExporterExporter does all the heavy-lifting (and is a very interesting module to study) after Pragmatic has stripped out the pragmata from the "use". DIAGNOSTICSThe following are the diagnostics generated by Pragmatic. Items marked "(W)" are non-fatal (invoke "Carp::carp"); those marked "(F)" are fatal (invoke "Carp::croak").
AUTHORSB. K. Oxley (binkley) <binkley@alumni.rice.edu>COPYRIGHTCopyright 1999-2005, B. K. Oxley. This library is free software; you may redistribute it and/or modify it under the same terms as Perl itself. THANKSThanks to Kevin Caswick <KCaswick@wspackaging.com> for a great patch to run under Perl 5.8.
Visit the GSP FreeBSD Man Page Interface. |