home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Exporter.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  13.1 KB  |  445 lines

  1. package Exporter;
  2.  
  3. require 5.001;
  4.  
  5.  
  6. $ExportLevel = 0;
  7. $Verbose = 0 unless $Verbose;
  8.  
  9. sub export {
  10.  
  11.     local $SIG{__WARN__} = sub {
  12.     my $text = shift;
  13.     if ($text =~ s/ at \S*Exporter.pm line \d+.*\n//) {
  14.         require Carp;
  15.         local $Carp::CarpLevel = 1;    # ignore package calling us too.
  16.         Carp::carp($text);
  17.     }
  18.     else {
  19.         warn $text;
  20.     }
  21.     };
  22.     local $SIG{__DIE__} = sub {
  23.     require Carp;
  24.     local $Carp::CarpLevel = 1;    # ignore package calling us too.
  25.     Carp::croak("$_[0]Illegal null symbol in \@${1}::EXPORT")
  26.         if $_[0] =~ /^Unable to create sub named "(.*?)::"/;
  27.     };
  28.  
  29.     my($pkg, $callpkg, @imports) = @_;
  30.     my($type, $sym, $oops);
  31.     *exports = *{"${pkg}::EXPORT"};
  32.  
  33.     if (@imports) {
  34.     if (!%exports) {
  35.         grep(s/^&//, @exports);
  36.         @exports{@exports} = (1) x @exports;
  37.         my $ok = \@{"${pkg}::EXPORT_OK"};
  38.         if (@$ok) {
  39.         grep(s/^&//, @$ok);
  40.         @exports{@$ok} = (1) x @$ok;
  41.         }
  42.     }
  43.  
  44.     if ($imports[0] =~ m#^[/!:]#){
  45.         my $tagsref = \%{"${pkg}::EXPORT_TAGS"};
  46.         my $tagdata;
  47.         my %imports;
  48.         my($remove, $spec, @names, @allexports);
  49.         unshift @imports, ':DEFAULT' if $imports[0] =~ m/^!/;
  50.         foreach $spec (@imports){
  51.         $remove = $spec =~ s/^!//;
  52.  
  53.         if ($spec =~ s/^://){
  54.             if ($spec eq 'DEFAULT'){
  55.             @names = @exports;
  56.             }
  57.             elsif ($tagdata = $tagsref->{$spec}) {
  58.             @names = @$tagdata;
  59.             }
  60.             else {
  61.             warn qq["$spec" is not defined in %${pkg}::EXPORT_TAGS];
  62.             ++$oops;
  63.             next;
  64.             }
  65.         }
  66.         elsif ($spec =~ m:^/(.*)/$:){
  67.             my $patn = $1;
  68.             @allexports = keys %exports unless @allexports; # only do keys once
  69.             @names = grep(/$patn/, @allexports); # not anchored by default
  70.         }
  71.         else {
  72.             @names = ($spec); # is a normal symbol name
  73.         }
  74.  
  75.         warn "Import ".($remove ? "del":"add").": @names "
  76.             if $Verbose;
  77.  
  78.         if ($remove) {
  79.            foreach $sym (@names) { delete $imports{$sym} } 
  80.         }
  81.         else {
  82.             @imports{@names} = (1) x @names;
  83.         }
  84.         }
  85.         @imports = keys %imports;
  86.     }
  87.  
  88.     foreach $sym (@imports) {
  89.         if (!$exports{$sym}) {
  90.         if ($sym =~ m/^\d/) {
  91.             $pkg->require_version($sym);
  92.             if (@imports == 1) {
  93.             @imports = @exports;
  94.             last;
  95.             }
  96.             if (@imports == 2 and !$imports[1]) {
  97.             @imports = ();
  98.             last;
  99.             }
  100.         } elsif ($sym !~ s/^&// || !$exports{$sym}) {
  101.                     require Carp;
  102.             Carp::carp(qq["$sym" is not exported by the $pkg module]);
  103.             $oops++;
  104.         }
  105.         }
  106.     }
  107.     if ($oops) {
  108.         require Carp;
  109.         Carp::croak("Can't continue after import errors");
  110.     }
  111.     }
  112.     else {
  113.     @imports = @exports;
  114.     }
  115.  
  116.     *fail = *{"${pkg}::EXPORT_FAIL"};
  117.     if (@fail) {
  118.     if (!%fail) {
  119.         my @expanded = map { /^\w/ ? ($_, '&'.$_) : $_ } @fail;
  120.         warn "${pkg}::EXPORT_FAIL cached: @expanded" if $Verbose;
  121.         @fail{@expanded} = (1) x @expanded;
  122.     }
  123.     my @failed;
  124.     foreach $sym (@imports) { push(@failed, $sym) if $fail{$sym} }
  125.     if (@failed) {
  126.         @failed = $pkg->export_fail(@failed);
  127.         foreach $sym (@failed) {
  128.                 require Carp;
  129.         Carp::carp(qq["$sym" is not implemented by the $pkg module ],
  130.             "on this architecture");
  131.         }
  132.         if (@failed) {
  133.         require Carp;
  134.         Carp::croak("Can't continue after import errors");
  135.         }
  136.     }
  137.     }
  138.  
  139.     warn "Importing into $callpkg from $pkg: ",
  140.         join(", ",sort @imports) if $Verbose;
  141.  
  142.     foreach $sym (@imports) {
  143.     (*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next)
  144.         unless $sym =~ s/^(\W)//;
  145.     $type = $1;
  146.     *{"${callpkg}::$sym"} =
  147.         $type eq '&' ? \&{"${pkg}::$sym"} :
  148.         $type eq '$' ? \${"${pkg}::$sym"} :
  149.         $type eq '@' ? \@{"${pkg}::$sym"} :
  150.         $type eq '%' ? \%{"${pkg}::$sym"} :
  151.         $type eq '*' ?  *{"${pkg}::$sym"} :
  152.         do { require Carp; Carp::croak("Can't export symbol: $type$sym") };
  153.     }
  154. }
  155.  
  156. sub export_to_level
  157. {
  158.       my $pkg = shift;
  159.       my ($level, $junk) = (shift, shift);  # need to get rid of first arg
  160.       my $callpkg = caller($level);
  161.       $pkg->export($callpkg, @_);
  162. }
  163.  
  164. sub import {
  165.     my $pkg = shift;
  166.     my $callpkg = caller($ExportLevel);
  167.     export $pkg, $callpkg, @_;
  168. }
  169.  
  170.  
  171.  
  172.  
  173. sub _push_tags {
  174.     my($pkg, $var, $syms) = @_;
  175.     my $nontag;
  176.     *export_tags = \%{"${pkg}::EXPORT_TAGS"};
  177.     push(@{"${pkg}::$var"},
  178.     map { $export_tags{$_} ? @{$export_tags{$_}} : scalar(++$nontag,$_) }
  179.         (@$syms) ? @$syms : keys %export_tags);
  180.     if ($nontag and $^W) {
  181.     require Carp;
  182.     Carp::carp("Some names are not tags");
  183.     }
  184. }
  185.  
  186. sub export_tags    { _push_tags((caller)[0], "EXPORT",    \@_) }
  187. sub export_ok_tags { _push_tags((caller)[0], "EXPORT_OK", \@_) }
  188.  
  189.  
  190.  
  191. sub export_fail {
  192.     my $self = shift;
  193.     @_;
  194. }
  195.  
  196. sub require_version {
  197.     my($self, $wanted) = @_;
  198.     my $pkg = ref $self || $self;
  199.     my $version = ${"${pkg}::VERSION"};
  200.     if (!$version or $version < $wanted) {
  201.     $version ||= "(undef)";
  202.     my $file = $INC{"$pkg.pm"};
  203.     $file &&= " ($file)";
  204.     require Carp;
  205.     Carp::croak("$pkg $wanted required--this is only version $version$file")
  206.     }
  207.     $version;
  208. }
  209.  
  210. 1;
  211.  
  212. __END__
  213. package Test;
  214. $INC{'Exporter.pm'} = 1;
  215. @ISA = qw(Exporter);
  216. @EXPORT      = qw(A1 A2 A3 A4 A5);
  217. @EXPORT_OK   = qw(B1 B2 B3 B4 B5);
  218. %EXPORT_TAGS = (T1=>[qw(A1 A2 B1 B2)], T2=>[qw(A1 A2 B3 B4)], T3=>[qw(X3)]);
  219. @EXPORT_FAIL = qw(B4);
  220. Exporter::export_ok_tags('T3', 'unknown_tag');
  221. sub export_fail {
  222.     map { "Test::$_" } @_    # edit symbols just as an example
  223. }
  224.  
  225. package main;
  226. $Exporter::Verbose = 1;
  227. import Test qw(:T2 !B4);
  228. import Test qw(:T2);        # should fail
  229. 1;
  230.  
  231. =head1 NAME
  232.  
  233. Exporter - Implements default import method for modules
  234.  
  235. =head1 SYNOPSIS
  236.  
  237. In module ModuleName.pm:
  238.  
  239.   package ModuleName;
  240.   require Exporter;
  241.   @ISA = qw(Exporter);
  242.  
  243.   @EXPORT = qw(...);            # symbols to export by default
  244.   @EXPORT_OK = qw(...);         # symbols to export on request
  245.   %EXPORT_TAGS = tag => [...];  # define names for sets of symbols
  246.  
  247. In other files which wish to use ModuleName:
  248.  
  249.   use ModuleName;               # import default symbols into my package
  250.  
  251.   use ModuleName qw(...);       # import listed symbols into my package
  252.  
  253.   use ModuleName ();            # do not import any symbols
  254.  
  255. =head1 DESCRIPTION
  256.  
  257. The Exporter module implements a default C<import> method which
  258. many modules choose to inherit rather than implement their own.
  259.  
  260. Perl automatically calls the C<import> method when processing a
  261. C<use> statement for a module. Modules and C<use> are documented
  262. in L<perlfunc> and L<perlmod>. Understanding the concept of
  263. modules and how the C<use> statement operates is important to
  264. understanding the Exporter.
  265.  
  266. =head2 Selecting What To Export
  267.  
  268. Do B<not> export method names!
  269.  
  270. Do B<not> export anything else by default without a good reason!
  271.  
  272. Exports pollute the namespace of the module user.  If you must export
  273. try to use @EXPORT_OK in preference to @EXPORT and avoid short or
  274. common symbol names to reduce the risk of name clashes.
  275.  
  276. Generally anything not exported is still accessible from outside the
  277. module using the ModuleName::item_name (or $blessed_ref-E<gt>method)
  278. syntax.  By convention you can use a leading underscore on names to
  279. informally indicate that they are 'internal' and not for public use.
  280.  
  281. (It is actually possible to get private functions by saying:
  282.  
  283.   my $subref = sub { ... };
  284.   &$subref;
  285.  
  286. But there's no way to call that directly as a method, since a method
  287. must have a name in the symbol table.)
  288.  
  289. As a general rule, if the module is trying to be object oriented
  290. then export nothing. If it's just a collection of functions then
  291. @EXPORT_OK anything but use @EXPORT with caution.
  292.  
  293. Other module design guidelines can be found in L<perlmod>.
  294.  
  295. =head2 Specialised Import Lists
  296.  
  297. If the first entry in an import list begins with !, : or / then the
  298. list is treated as a series of specifications which either add to or
  299. delete from the list of names to import. They are processed left to
  300. right. Specifications are in the form:
  301.  
  302.     [!]name         This name only
  303.     [!]:DEFAULT     All names in @EXPORT
  304.     [!]:tag         All names in $EXPORT_TAGS{tag} anonymous list
  305.     [!]/pattern/    All names in @EXPORT and @EXPORT_OK which match
  306.  
  307. A leading ! indicates that matching names should be deleted from the
  308. list of names to import.  If the first specification is a deletion it
  309. is treated as though preceded by :DEFAULT. If you just want to import
  310. extra names in addition to the default set you will still need to
  311. include :DEFAULT explicitly.
  312.  
  313. e.g., Module.pm defines:
  314.  
  315.     @EXPORT      = qw(A1 A2 A3 A4 A5);
  316.     @EXPORT_OK   = qw(B1 B2 B3 B4 B5);
  317.     %EXPORT_TAGS = (T1 => [qw(A1 A2 B1 B2)], T2 => [qw(A1 A2 B3 B4)]);
  318.  
  319.     Note that you cannot use tags in @EXPORT or @EXPORT_OK.
  320.     Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK.
  321.  
  322. An application using Module can say something like:
  323.  
  324.     use Module qw(:DEFAULT :T2 !B3 A3);
  325.  
  326. Other examples include:
  327.  
  328.     use Socket qw(!/^[AP]F_/ !SOMAXCONN !SOL_SOCKET);
  329.     use POSIX  qw(:errno_h :termios_h !TCSADRAIN !/^EXIT/);
  330.  
  331. Remember that most patterns (using //) will need to be anchored
  332. with a leading ^, e.g., C</^EXIT/> rather than C</EXIT/>.
  333.  
  334. You can say C<BEGIN { $Exporter::Verbose=1 }> to see how the
  335. specifications are being processed and what is actually being imported
  336. into modules.
  337.  
  338. =head2 Exporting without using Export's import method
  339.  
  340. Exporter has a special method, 'export_to_level' which is used in situations
  341. where you can't directly call Export's import method. The export_to_level
  342. method looks like:
  343.  
  344. MyPackage->export_to_level($where_to_export, @what_to_export);
  345.  
  346. where $where_to_export is an integer telling how far up the calling stack
  347. to export your symbols, and @what_to_export is an array telling what
  348. symbols *to* export (usually this is @_).
  349.  
  350. For example, suppose that you have a module, A, which already has an
  351. import function:
  352.  
  353. package A;
  354.  
  355. @ISA = qw(Exporter);
  356. @EXPORT_OK = qw ($b);
  357.  
  358. sub import
  359. {
  360.     $A::b = 1;     # not a very useful import method
  361. }
  362.  
  363. and you want to Export symbol $A::b back to the module that called 
  364. package A. Since Exporter relies on the import method to work, via 
  365. inheritance, as it stands Exporter::import() will never get called. 
  366. Instead, say the following:
  367.  
  368. package A;
  369. @ISA = qw(Exporter);
  370. @EXPORT_OK = qw ($b);
  371.  
  372. sub import
  373. {
  374.     $A::b = 1;
  375.     A->export_to_level(1, @_);
  376. }
  377.  
  378. This will export the symbols one level 'above' the current package - ie: to 
  379. the program or module that used package A. 
  380.  
  381. Note: Be careful not to modify '@_' at all before you call export_to_level
  382. - or people using your package will get very unexplained results!
  383.  
  384.  
  385. =head2 Module Version Checking
  386.  
  387. The Exporter module will convert an attempt to import a number from a
  388. module into a call to $module_name-E<gt>require_version($value). This can
  389. be used to validate that the version of the module being used is
  390. greater than or equal to the required version.
  391.  
  392. The Exporter module supplies a default require_version method which
  393. checks the value of $VERSION in the exporting module.
  394.  
  395. Since the default require_version method treats the $VERSION number as
  396. a simple numeric value it will regard version 1.10 as lower than
  397. 1.9. For this reason it is strongly recommended that you use numbers
  398. with at least two decimal places, e.g., 1.09.
  399.  
  400. =head2 Managing Unknown Symbols
  401.  
  402. In some situations you may want to prevent certain symbols from being
  403. exported. Typically this applies to extensions which have functions
  404. or constants that may not exist on some systems.
  405.  
  406. The names of any symbols that cannot be exported should be listed
  407. in the C<@EXPORT_FAIL> array.
  408.  
  409. If a module attempts to import any of these symbols the Exporter
  410. will give the module an opportunity to handle the situation before
  411. generating an error. The Exporter will call an export_fail method
  412. with a list of the failed symbols:
  413.  
  414.   @failed_symbols = $module_name->export_fail(@failed_symbols);
  415.  
  416. If the export_fail method returns an empty list then no error is
  417. recorded and all the requested symbols are exported. If the returned
  418. list is not empty then an error is generated for each symbol and the
  419. export fails. The Exporter provides a default export_fail method which
  420. simply returns the list unchanged.
  421.  
  422. Uses for the export_fail method include giving better error messages
  423. for some symbols and performing lazy architectural checks (put more
  424. symbols into @EXPORT_FAIL by default and then take them out if someone
  425. actually tries to use them and an expensive check shows that they are
  426. usable on that platform).
  427.  
  428. =head2 Tag Handling Utility Functions
  429.  
  430. Since the symbols listed within %EXPORT_TAGS must also appear in either
  431. @EXPORT or @EXPORT_OK, two utility functions are provided which allow
  432. you to easily add tagged sets of symbols to @EXPORT or @EXPORT_OK:
  433.  
  434.   %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]);
  435.  
  436.   Exporter::export_tags('foo');     # add aa, bb and cc to @EXPORT
  437.   Exporter::export_ok_tags('bar');  # add aa, cc and dd to @EXPORT_OK
  438.  
  439. Any names which are not tags are added to @EXPORT or @EXPORT_OK
  440. unchanged but will trigger a warning (with C<-w>) to avoid misspelt tags
  441. names being silently added to @EXPORT or @EXPORT_OK. Future versions
  442. may make this a fatal error.
  443.  
  444. =cut
  445.