1*0Sstevel@tonic-gatepackage Exporter; 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gaterequire 5.006; 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate# Be lean. 6*0Sstevel@tonic-gate#use strict; 7*0Sstevel@tonic-gate#no strict 'refs'; 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gateour $Debug = 0; 10*0Sstevel@tonic-gateour $ExportLevel = 0; 11*0Sstevel@tonic-gateour $Verbose ||= 0; 12*0Sstevel@tonic-gateour $VERSION = '5.58'; 13*0Sstevel@tonic-gateour (%Cache); 14*0Sstevel@tonic-gate$Carp::Internal{Exporter} = 1; 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gatesub as_heavy { 17*0Sstevel@tonic-gate require Exporter::Heavy; 18*0Sstevel@tonic-gate # Unfortunately, this does not work if the caller is aliased as *name = \&foo 19*0Sstevel@tonic-gate # Thus the need to create a lot of identical subroutines 20*0Sstevel@tonic-gate my $c = (caller(1))[3]; 21*0Sstevel@tonic-gate $c =~ s/.*:://; 22*0Sstevel@tonic-gate \&{"Exporter::Heavy::heavy_$c"}; 23*0Sstevel@tonic-gate} 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gatesub export { 26*0Sstevel@tonic-gate goto &{as_heavy()}; 27*0Sstevel@tonic-gate} 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gatesub import { 30*0Sstevel@tonic-gate my $pkg = shift; 31*0Sstevel@tonic-gate my $callpkg = caller($ExportLevel); 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate if ($pkg eq "Exporter" and @_ and $_[0] eq "import") { 34*0Sstevel@tonic-gate *{$callpkg."::import"} = \&import; 35*0Sstevel@tonic-gate return; 36*0Sstevel@tonic-gate } 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate # We *need* to treat @{"$pkg\::EXPORT_FAIL"} since Carp uses it :-( 39*0Sstevel@tonic-gate my($exports, $fail) = (\@{"$pkg\::EXPORT"}, \@{"$pkg\::EXPORT_FAIL"}); 40*0Sstevel@tonic-gate return export $pkg, $callpkg, @_ 41*0Sstevel@tonic-gate if $Verbose or $Debug or @$fail > 1; 42*0Sstevel@tonic-gate my $export_cache = ($Cache{$pkg} ||= {}); 43*0Sstevel@tonic-gate my $args = @_ or @_ = @$exports; 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate local $_; 46*0Sstevel@tonic-gate if ($args and not %$export_cache) { 47*0Sstevel@tonic-gate s/^&//, $export_cache->{$_} = 1 48*0Sstevel@tonic-gate foreach (@$exports, @{"$pkg\::EXPORT_OK"}); 49*0Sstevel@tonic-gate } 50*0Sstevel@tonic-gate my $heavy; 51*0Sstevel@tonic-gate # Try very hard not to use {} and hence have to enter scope on the foreach 52*0Sstevel@tonic-gate # We bomb out of the loop with last as soon as heavy is set. 53*0Sstevel@tonic-gate if ($args or $fail) { 54*0Sstevel@tonic-gate ($heavy = (/\W/ or $args and not exists $export_cache->{$_} 55*0Sstevel@tonic-gate or @$fail and $_ eq $fail->[0])) and last 56*0Sstevel@tonic-gate foreach (@_); 57*0Sstevel@tonic-gate } else { 58*0Sstevel@tonic-gate ($heavy = /\W/) and last 59*0Sstevel@tonic-gate foreach (@_); 60*0Sstevel@tonic-gate } 61*0Sstevel@tonic-gate return export $pkg, $callpkg, ($args ? @_ : ()) if $heavy; 62*0Sstevel@tonic-gate local $SIG{__WARN__} = 63*0Sstevel@tonic-gate sub {require Carp; &Carp::carp}; 64*0Sstevel@tonic-gate # shortcut for the common case of no type character 65*0Sstevel@tonic-gate *{"$callpkg\::$_"} = \&{"$pkg\::$_"} foreach @_; 66*0Sstevel@tonic-gate} 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate# Default methods 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gatesub export_fail { 71*0Sstevel@tonic-gate my $self = shift; 72*0Sstevel@tonic-gate @_; 73*0Sstevel@tonic-gate} 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate# Unfortunately, caller(1)[3] "does not work" if the caller is aliased as 76*0Sstevel@tonic-gate# *name = \&foo. Thus the need to create a lot of identical subroutines 77*0Sstevel@tonic-gate# Otherwise we could have aliased them to export(). 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gatesub export_to_level { 80*0Sstevel@tonic-gate goto &{as_heavy()}; 81*0Sstevel@tonic-gate} 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gatesub export_tags { 84*0Sstevel@tonic-gate goto &{as_heavy()}; 85*0Sstevel@tonic-gate} 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gatesub export_ok_tags { 88*0Sstevel@tonic-gate goto &{as_heavy()}; 89*0Sstevel@tonic-gate} 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gatesub require_version { 92*0Sstevel@tonic-gate goto &{as_heavy()}; 93*0Sstevel@tonic-gate} 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate1; 96*0Sstevel@tonic-gate__END__ 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate=head1 NAME 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gateExporter - Implements default import method for modules 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate=head1 SYNOPSIS 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gateIn module YourModule.pm: 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate package YourModule; 107*0Sstevel@tonic-gate require Exporter; 108*0Sstevel@tonic-gate @ISA = qw(Exporter); 109*0Sstevel@tonic-gate @EXPORT_OK = qw(munge frobnicate); # symbols to export on request 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gateor 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate package YourModule; 114*0Sstevel@tonic-gate use Exporter 'import'; # gives you Exporter's import() method directly 115*0Sstevel@tonic-gate @EXPORT_OK = qw(munge frobnicate); # symbols to export on request 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gateIn other files which wish to use YourModule: 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate use ModuleName qw(frobnicate); # import listed symbols 120*0Sstevel@tonic-gate frobnicate ($left, $right) # calls YourModule::frobnicate 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate=head1 DESCRIPTION 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gateThe Exporter module implements an C<import> method which allows a module 125*0Sstevel@tonic-gateto export functions and variables to its users' namespaces. Many modules 126*0Sstevel@tonic-gateuse Exporter rather than implementing their own C<import> method because 127*0Sstevel@tonic-gateExporter provides a highly flexible interface, with an implementation optimised 128*0Sstevel@tonic-gatefor the common case. 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gatePerl automatically calls the C<import> method when processing a 131*0Sstevel@tonic-gateC<use> statement for a module. Modules and C<use> are documented 132*0Sstevel@tonic-gatein L<perlfunc> and L<perlmod>. Understanding the concept of 133*0Sstevel@tonic-gatemodules and how the C<use> statement operates is important to 134*0Sstevel@tonic-gateunderstanding the Exporter. 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate=head2 How to Export 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gateThe arrays C<@EXPORT> and C<@EXPORT_OK> in a module hold lists of 139*0Sstevel@tonic-gatesymbols that are going to be exported into the users name space by 140*0Sstevel@tonic-gatedefault, or which they can request to be exported, respectively. The 141*0Sstevel@tonic-gatesymbols can represent functions, scalars, arrays, hashes, or typeglobs. 142*0Sstevel@tonic-gateThe symbols must be given by full name with the exception that the 143*0Sstevel@tonic-gateampersand in front of a function is optional, e.g. 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate @EXPORT = qw(afunc $scalar @array); # afunc is a function 146*0Sstevel@tonic-gate @EXPORT_OK = qw(&bfunc %hash *typeglob); # explicit prefix on &bfunc 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gateIf you are only exporting function names it is recommended to omit the 149*0Sstevel@tonic-gateampersand, as the implementation is faster this way. 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate=head2 Selecting What To Export 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gateDo B<not> export method names! 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gateDo B<not> export anything else by default without a good reason! 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gateExports pollute the namespace of the module user. If you must export 158*0Sstevel@tonic-gatetry to use @EXPORT_OK in preference to @EXPORT and avoid short or 159*0Sstevel@tonic-gatecommon symbol names to reduce the risk of name clashes. 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gateGenerally anything not exported is still accessible from outside the 162*0Sstevel@tonic-gatemodule using the ModuleName::item_name (or $blessed_ref-E<gt>method) 163*0Sstevel@tonic-gatesyntax. By convention you can use a leading underscore on names to 164*0Sstevel@tonic-gateinformally indicate that they are 'internal' and not for public use. 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate(It is actually possible to get private functions by saying: 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate my $subref = sub { ... }; 169*0Sstevel@tonic-gate $subref->(@args); # Call it as a function 170*0Sstevel@tonic-gate $obj->$subref(@args); # Use it as a method 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gateHowever if you use them for methods it is up to you to figure out 173*0Sstevel@tonic-gatehow to make inheritance work.) 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gateAs a general rule, if the module is trying to be object oriented 176*0Sstevel@tonic-gatethen export nothing. If it's just a collection of functions then 177*0Sstevel@tonic-gate@EXPORT_OK anything but use @EXPORT with caution. For function and 178*0Sstevel@tonic-gatemethod names use barewords in preference to names prefixed with 179*0Sstevel@tonic-gateampersands for the export lists. 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gateOther module design guidelines can be found in L<perlmod>. 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate=head2 How to Import 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gateIn other files which wish to use your module there are three basic ways for 186*0Sstevel@tonic-gatethem to load your module and import its symbols: 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate=over 4 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate=item C<use ModuleName;> 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gateThis imports all the symbols from ModuleName's @EXPORT into the namespace 193*0Sstevel@tonic-gateof the C<use> statement. 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate=item C<use ModuleName ();> 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gateThis causes perl to load your module but does not import any symbols. 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate=item C<use ModuleName qw(...);> 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gateThis imports only the symbols listed by the caller into their namespace. 202*0Sstevel@tonic-gateAll listed symbols must be in your @EXPORT or @EXPORT_OK, else an error 203*0Sstevel@tonic-gateoccurs. The advanced export features of Exporter are accessed like this, 204*0Sstevel@tonic-gatebut with list entries that are syntactically distinct from symbol names. 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate=back 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gateUnless you want to use its advanced features, this is probably all you 209*0Sstevel@tonic-gateneed to know to use Exporter. 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate=head1 Advanced features 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate=head2 Specialised Import Lists 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gateIf any of the entries in an import list begins with !, : or / then 216*0Sstevel@tonic-gatethe list is treated as a series of specifications which either add to 217*0Sstevel@tonic-gateor delete from the list of names to import. They are processed left to 218*0Sstevel@tonic-gateright. Specifications are in the form: 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate [!]name This name only 221*0Sstevel@tonic-gate [!]:DEFAULT All names in @EXPORT 222*0Sstevel@tonic-gate [!]:tag All names in $EXPORT_TAGS{tag} anonymous list 223*0Sstevel@tonic-gate [!]/pattern/ All names in @EXPORT and @EXPORT_OK which match 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gateA leading ! indicates that matching names should be deleted from the 226*0Sstevel@tonic-gatelist of names to import. If the first specification is a deletion it 227*0Sstevel@tonic-gateis treated as though preceded by :DEFAULT. If you just want to import 228*0Sstevel@tonic-gateextra names in addition to the default set you will still need to 229*0Sstevel@tonic-gateinclude :DEFAULT explicitly. 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gatee.g., Module.pm defines: 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate @EXPORT = qw(A1 A2 A3 A4 A5); 234*0Sstevel@tonic-gate @EXPORT_OK = qw(B1 B2 B3 B4 B5); 235*0Sstevel@tonic-gate %EXPORT_TAGS = (T1 => [qw(A1 A2 B1 B2)], T2 => [qw(A1 A2 B3 B4)]); 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate Note that you cannot use tags in @EXPORT or @EXPORT_OK. 238*0Sstevel@tonic-gate Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK. 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gateAn application using Module can say something like: 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate use Module qw(:DEFAULT :T2 !B3 A3); 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gateOther examples include: 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate use Socket qw(!/^[AP]F_/ !SOMAXCONN !SOL_SOCKET); 247*0Sstevel@tonic-gate use POSIX qw(:errno_h :termios_h !TCSADRAIN !/^EXIT/); 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gateRemember that most patterns (using //) will need to be anchored 250*0Sstevel@tonic-gatewith a leading ^, e.g., C</^EXIT/> rather than C</EXIT/>. 251*0Sstevel@tonic-gate 252*0Sstevel@tonic-gateYou can say C<BEGIN { $Exporter::Verbose=1 }> to see how the 253*0Sstevel@tonic-gatespecifications are being processed and what is actually being imported 254*0Sstevel@tonic-gateinto modules. 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate=head2 Exporting without using Exporter's import method 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gateExporter has a special method, 'export_to_level' which is used in situations 259*0Sstevel@tonic-gatewhere you can't directly call Exporter's import method. The export_to_level 260*0Sstevel@tonic-gatemethod looks like: 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate MyPackage->export_to_level($where_to_export, $package, @what_to_export); 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gatewhere $where_to_export is an integer telling how far up the calling stack 265*0Sstevel@tonic-gateto export your symbols, and @what_to_export is an array telling what 266*0Sstevel@tonic-gatesymbols *to* export (usually this is @_). The $package argument is 267*0Sstevel@tonic-gatecurrently unused. 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gateFor example, suppose that you have a module, A, which already has an 270*0Sstevel@tonic-gateimport function: 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate package A; 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate @ISA = qw(Exporter); 275*0Sstevel@tonic-gate @EXPORT_OK = qw ($b); 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate sub import 278*0Sstevel@tonic-gate { 279*0Sstevel@tonic-gate $A::b = 1; # not a very useful import method 280*0Sstevel@tonic-gate } 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gateand you want to Export symbol $A::b back to the module that called 283*0Sstevel@tonic-gatepackage A. Since Exporter relies on the import method to work, via 284*0Sstevel@tonic-gateinheritance, as it stands Exporter::import() will never get called. 285*0Sstevel@tonic-gateInstead, say the following: 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate package A; 288*0Sstevel@tonic-gate @ISA = qw(Exporter); 289*0Sstevel@tonic-gate @EXPORT_OK = qw ($b); 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate sub import 292*0Sstevel@tonic-gate { 293*0Sstevel@tonic-gate $A::b = 1; 294*0Sstevel@tonic-gate A->export_to_level(1, @_); 295*0Sstevel@tonic-gate } 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gateThis will export the symbols one level 'above' the current package - ie: to 298*0Sstevel@tonic-gatethe program or module that used package A. 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gateNote: Be careful not to modify C<@_> at all before you call export_to_level 301*0Sstevel@tonic-gate- or people using your package will get very unexplained results! 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate=head2 Exporting without inheriting from Exporter 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gateBy including Exporter in your @ISA you inherit an Exporter's import() method 306*0Sstevel@tonic-gatebut you also inherit several other helper methods which you probably don't 307*0Sstevel@tonic-gatewant. To avoid this you can do 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate package YourModule; 310*0Sstevel@tonic-gate use Exporter qw( import ); 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gatewhich will export Exporter's own import() method into YourModule. 313*0Sstevel@tonic-gateEverything will work as before but you won't need to include Exporter in 314*0Sstevel@tonic-gate@YourModule::ISA. 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate=head2 Module Version Checking 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gateThe Exporter module will convert an attempt to import a number from a 319*0Sstevel@tonic-gatemodule into a call to $module_name-E<gt>require_version($value). This can 320*0Sstevel@tonic-gatebe used to validate that the version of the module being used is 321*0Sstevel@tonic-gategreater than or equal to the required version. 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gateThe Exporter module supplies a default require_version method which 324*0Sstevel@tonic-gatechecks the value of $VERSION in the exporting module. 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gateSince the default require_version method treats the $VERSION number as 327*0Sstevel@tonic-gatea simple numeric value it will regard version 1.10 as lower than 328*0Sstevel@tonic-gate1.9. For this reason it is strongly recommended that you use numbers 329*0Sstevel@tonic-gatewith at least two decimal places, e.g., 1.09. 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate=head2 Managing Unknown Symbols 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gateIn some situations you may want to prevent certain symbols from being 334*0Sstevel@tonic-gateexported. Typically this applies to extensions which have functions 335*0Sstevel@tonic-gateor constants that may not exist on some systems. 336*0Sstevel@tonic-gate 337*0Sstevel@tonic-gateThe names of any symbols that cannot be exported should be listed 338*0Sstevel@tonic-gatein the C<@EXPORT_FAIL> array. 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gateIf a module attempts to import any of these symbols the Exporter 341*0Sstevel@tonic-gatewill give the module an opportunity to handle the situation before 342*0Sstevel@tonic-gategenerating an error. The Exporter will call an export_fail method 343*0Sstevel@tonic-gatewith a list of the failed symbols: 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate @failed_symbols = $module_name->export_fail(@failed_symbols); 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gateIf the export_fail method returns an empty list then no error is 348*0Sstevel@tonic-gaterecorded and all the requested symbols are exported. If the returned 349*0Sstevel@tonic-gatelist is not empty then an error is generated for each symbol and the 350*0Sstevel@tonic-gateexport fails. The Exporter provides a default export_fail method which 351*0Sstevel@tonic-gatesimply returns the list unchanged. 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gateUses for the export_fail method include giving better error messages 354*0Sstevel@tonic-gatefor some symbols and performing lazy architectural checks (put more 355*0Sstevel@tonic-gatesymbols into @EXPORT_FAIL by default and then take them out if someone 356*0Sstevel@tonic-gateactually tries to use them and an expensive check shows that they are 357*0Sstevel@tonic-gateusable on that platform). 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate=head2 Tag Handling Utility Functions 360*0Sstevel@tonic-gate 361*0Sstevel@tonic-gateSince the symbols listed within %EXPORT_TAGS must also appear in either 362*0Sstevel@tonic-gate@EXPORT or @EXPORT_OK, two utility functions are provided which allow 363*0Sstevel@tonic-gateyou to easily add tagged sets of symbols to @EXPORT or @EXPORT_OK: 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]); 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gate Exporter::export_tags('foo'); # add aa, bb and cc to @EXPORT 368*0Sstevel@tonic-gate Exporter::export_ok_tags('bar'); # add aa, cc and dd to @EXPORT_OK 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gateAny names which are not tags are added to @EXPORT or @EXPORT_OK 371*0Sstevel@tonic-gateunchanged but will trigger a warning (with C<-w>) to avoid misspelt tags 372*0Sstevel@tonic-gatenames being silently added to @EXPORT or @EXPORT_OK. Future versions 373*0Sstevel@tonic-gatemay make this a fatal error. 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate=head2 Generating combined tags 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gateIf several symbol categories exist in %EXPORT_TAGS, it's usually 378*0Sstevel@tonic-gateuseful to create the utility ":all" to simplify "use" statements. 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gateThe simplest way to do this is: 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]); 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gate # add all the other ":class" tags to the ":all" class, 385*0Sstevel@tonic-gate # deleting duplicates 386*0Sstevel@tonic-gate { 387*0Sstevel@tonic-gate my %seen; 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gate push @{$EXPORT_TAGS{all}}, 390*0Sstevel@tonic-gate grep {!$seen{$_}++} @{$EXPORT_TAGS{$_}} foreach keys %EXPORT_TAGS; 391*0Sstevel@tonic-gate } 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gateCGI.pm creates an ":all" tag which contains some (but not really 394*0Sstevel@tonic-gateall) of its categories. That could be done with one small 395*0Sstevel@tonic-gatechange: 396*0Sstevel@tonic-gate 397*0Sstevel@tonic-gate # add some of the other ":class" tags to the ":all" class, 398*0Sstevel@tonic-gate # deleting duplicates 399*0Sstevel@tonic-gate { 400*0Sstevel@tonic-gate my %seen; 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate push @{$EXPORT_TAGS{all}}, 403*0Sstevel@tonic-gate grep {!$seen{$_}++} @{$EXPORT_TAGS{$_}} 404*0Sstevel@tonic-gate foreach qw/html2 html3 netscape form cgi internal/; 405*0Sstevel@tonic-gate } 406*0Sstevel@tonic-gate 407*0Sstevel@tonic-gateNote that the tag names in %EXPORT_TAGS don't have the leading ':'. 408*0Sstevel@tonic-gate 409*0Sstevel@tonic-gate=head2 C<AUTOLOAD>ed Constants 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gateMany modules make use of C<AUTOLOAD>ing for constant subroutines to 412*0Sstevel@tonic-gateavoid having to compile and waste memory on rarely used values (see 413*0Sstevel@tonic-gateL<perlsub> for details on constant subroutines). Calls to such 414*0Sstevel@tonic-gateconstant subroutines are not optimized away at compile time because 415*0Sstevel@tonic-gatethey can't be checked at compile time for constancy. 416*0Sstevel@tonic-gate 417*0Sstevel@tonic-gateEven if a prototype is available at compile time, the body of the 418*0Sstevel@tonic-gatesubroutine is not (it hasn't been C<AUTOLOAD>ed yet). perl needs to 419*0Sstevel@tonic-gateexamine both the C<()> prototype and the body of a subroutine at 420*0Sstevel@tonic-gatecompile time to detect that it can safely replace calls to that 421*0Sstevel@tonic-gatesubroutine with the constant value. 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gateA workaround for this is to call the constants once in a C<BEGIN> block: 424*0Sstevel@tonic-gate 425*0Sstevel@tonic-gate package My ; 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gate use Socket ; 428*0Sstevel@tonic-gate 429*0Sstevel@tonic-gate foo( SO_LINGER ); ## SO_LINGER NOT optimized away; called at runtime 430*0Sstevel@tonic-gate BEGIN { SO_LINGER } 431*0Sstevel@tonic-gate foo( SO_LINGER ); ## SO_LINGER optimized away at compile time. 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gateThis forces the C<AUTOLOAD> for C<SO_LINGER> to take place before 434*0Sstevel@tonic-gateSO_LINGER is encountered later in C<My> package. 435*0Sstevel@tonic-gate 436*0Sstevel@tonic-gateIf you are writing a package that C<AUTOLOAD>s, consider forcing 437*0Sstevel@tonic-gatean C<AUTOLOAD> for any constants explicitly imported by other packages 438*0Sstevel@tonic-gateor which are usually used when your package is C<use>d. 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate=cut 441