home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / p_man / cat3 / perl5 / Exporter.z / Exporter
Encoding:
Text File  |  1998-10-30  |  10.9 KB  |  331 lines

  1.  
  2.  
  3.  
  4. EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))                                                        EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      Exporter - Implements default import method for modules
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.      In module ModuleName.pm:
  13.  
  14.        package ModuleName;
  15.        require Exporter;
  16.        @ISA = qw(Exporter);
  17.  
  18.        @EXPORT = qw(...);            # symbols to export by default
  19.        @EXPORT_OK = qw(...);         # symbols to export on request
  20.        %EXPORT_TAGS = tag => [...];  # define names for sets of symbols
  21.  
  22.      In other files which wish to use ModuleName:
  23.  
  24.        use ModuleName;               # import default symbols into my package
  25.  
  26.        use ModuleName qw(...);       # import listed symbols into my package
  27.  
  28.        use ModuleName ();            # do not import any symbols
  29.  
  30.  
  31. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  32.      The Exporter module implements a default import method which many modules
  33.      choose to inherit rather than implement their own.
  34.  
  35.      Perl automatically calls the import method when processing a use
  36.      statement for a module. Modules and use are documented in the _p_e_r_l_f_u_n_c
  37.      manpage and the _p_e_r_l_m_o_d manpage. Understanding the concept of modules and
  38.      how the use statement operates is important to understanding the
  39.      Exporter.
  40.  
  41.      SSSSeeeelllleeeeccccttttiiiinnnngggg WWWWhhhhaaaatttt TTTToooo EEEExxxxppppoooorrrrtttt
  42.  
  43.      Do nnnnooootttt export method names!
  44.  
  45.      Do nnnnooootttt export anything else by default without a good reason!
  46.  
  47.      Exports pollute the namespace of the module user.  If you must export try
  48.      to use @EXPORT_OK in preference to @EXPORT and avoid short or common
  49.      symbol names to reduce the risk of name clashes.
  50.  
  51.      Generally anything not exported is still accessible from outside the
  52.      module using the ModuleName::item_name (or $blessed_ref->method) syntax.
  53.      By convention you can use a leading underscore on names to informally
  54.      indicate that they are 'internal' and not for public use.
  55.  
  56.      (It is actually possible to get private functions by saying:
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))                                                        EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))
  71.  
  72.  
  73.  
  74.        my $subref = sub { ... };
  75.        &$subref;
  76.  
  77.      But there's no way to call that directly as a method, since a method must
  78.      have a name in the symbol table.)
  79.  
  80.      As a general rule, if the module is trying to be object oriented then
  81.      export nothing. If it's just a collection of functions then @EXPORT_OK
  82.      anything but use @EXPORT with caution.
  83.  
  84.      Other module design guidelines can be found in the _p_e_r_l_m_o_d manpage.
  85.  
  86.      SSSSppppeeeecccciiiiaaaalllliiiisssseeeedddd IIIImmmmppppoooorrrrtttt LLLLiiiissssttttssss
  87.  
  88.      If the first entry in an import list begins with !, : or / then the list
  89.      is treated as a series of specifications which either add to or delete
  90.      from the list of names to import. They are processed left to right.
  91.      Specifications are in the form:
  92.  
  93.          [!]name         This name only
  94.          [!]:DEFAULT     All names in @EXPORT
  95.          [!]:tag         All names in $EXPORT_TAGS{tag} anonymous list
  96.          [!]/pattern/    All names in @EXPORT and @EXPORT_OK which match
  97.  
  98.      A leading ! indicates that matching names should be deleted from the list
  99.      of names to import.  If the first specification is a deletion it is
  100.      treated as though preceded by :DEFAULT. If you just want to import extra
  101.      names in addition to the default set you will still need to include
  102.      :DEFAULT explicitly.
  103.  
  104.      e.g., Module.pm defines:
  105.  
  106.          @EXPORT      = qw(A1 A2 A3 A4 A5);
  107.          @EXPORT_OK   = qw(B1 B2 B3 B4 B5);
  108.          %EXPORT_TAGS = (T1 => [qw(A1 A2 B1 B2)], T2 => [qw(A1 A2 B3 B4)]);
  109.  
  110.          Note that you cannot use tags in @EXPORT or @EXPORT_OK.
  111.          Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK.
  112.  
  113.      An application using Module can say something like:
  114.  
  115.          use Module qw(:DEFAULT :T2 !B3 A3);
  116.  
  117.      Other examples include:
  118.  
  119.          use Socket qw(!/^[AP]F_/ !SOMAXCONN !SOL_SOCKET);
  120.          use POSIX  qw(:errno_h :termios_h !TCSADRAIN !/^EXIT/);
  121.  
  122.      Remember that most patterns (using //) will need to be anchored with a
  123.      leading ^, e.g., /^EXIT/ rather than /EXIT/.
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))                                                        EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))
  137.  
  138.  
  139.  
  140.      You can say BEGIN { $Exporter::Verbose=1 } to see how the specifications
  141.      are being processed and what is actually being imported into modules.
  142.  
  143.      EEEExxxxppppoooorrrrttttiiiinnnngggg wwwwiiiitttthhhhoooouuuutttt uuuussssiiiinnnngggg EEEExxxxppppoooorrrrtttt''''ssss iiiimmmmppppoooorrrrtttt mmmmeeeetttthhhhoooodddd
  144.  
  145.      Exporter has a special method, 'export_to_level' which is used in
  146.      situations where you can't directly call Export's import method. The
  147.      export_to_level method looks like:
  148.  
  149.      MyPackage->_e_x_p_o_r_t__t_o__l_e_v_e_l($where_to_export, @what_to_export);
  150.  
  151.      where $where_to_export is an integer telling how far up the calling stack
  152.      to export your symbols, and @what_to_export is an array telling what
  153.      symbols *to* export (usually this is @_).
  154.  
  155.      For example, suppose that you have a module, A, which already has an
  156.      import function:
  157.  
  158.      package A;
  159.  
  160.      @ISA = _q_w(Exporter); @EXPORT_OK = qw ($b);
  161.  
  162.      sub import {
  163.          $A::b = 1;     # not a very useful import method }
  164.  
  165.      and you want to Export symbol $A::b back to the module that called
  166.      package A. Since Exporter relies on the import method to work, via
  167.      inheritance, as it stands _E_x_p_o_r_t_e_r::_i_m_p_o_r_t() will never get called.
  168.      Instead, say the following:
  169.  
  170.      package A; @ISA = _q_w(Exporter); @EXPORT_OK = qw ($b);
  171.  
  172.      sub import {
  173.          $A::b = 1;
  174.          A->_e_x_p_o_r_t__t_o__l_e_v_e_l(1, @_); }
  175.  
  176.      This will export the symbols one level 'above' the current package - ie:
  177.      to the program or module that used package A.
  178.  
  179.      Note: Be careful not to modify '@_' at all before you call
  180.      export_to_level - or people using your package will get very unexplained
  181.      results!
  182.  
  183.      MMMMoooodddduuuulllleeee VVVVeeeerrrrssssiiiioooonnnn CCCChhhheeeecccckkkkiiiinnnngggg
  184.  
  185.      The Exporter module will convert an attempt to import a number from a
  186.      module into a call to $module_name->_r_e_q_u_i_r_e__v_e_r_s_i_o_n($value). This can be
  187.      used to validate that the version of the module being used is greater
  188.      than or equal to the required version.
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))                                                        EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))
  203.  
  204.  
  205.  
  206.      The Exporter module supplies a default require_version method which
  207.      checks the value of $VERSION in the exporting module.
  208.  
  209.      Since the default require_version method treats the $VERSION number as a
  210.      simple numeric value it will regard version 1.10 as lower than 1.9. For
  211.      this reason it is strongly recommended that you use numbers with at least
  212.      two decimal places, e.g., 1.09.
  213.  
  214.      MMMMaaaannnnaaaaggggiiiinnnngggg UUUUnnnnkkkknnnnoooowwwwnnnn SSSSyyyymmmmbbbboooollllssss
  215.  
  216.      In some situations you may want to prevent certain symbols from being
  217.      exported. Typically this applies to extensions which have functions or
  218.      constants that may not exist on some systems.
  219.  
  220.      The names of any symbols that cannot be exported should be listed in the
  221.      @EXPORT_FAIL array.
  222.  
  223.      If a module attempts to import any of these symbols the Exporter will
  224.      give the module an opportunity to handle the situation before generating
  225.      an error. The Exporter will call an export_fail method with a list of the
  226.      failed symbols:
  227.  
  228.        @failed_symbols = $module_name->export_fail(@failed_symbols);
  229.  
  230.      If the export_fail method returns an empty list then no error is recorded
  231.      and all the requested symbols are exported. If the returned list is not
  232.      empty then an error is generated for each symbol and the export fails.
  233.      The Exporter provides a default export_fail method which simply returns
  234.      the list unchanged.
  235.  
  236.      Uses for the export_fail method include giving better error messages for
  237.      some symbols and performing lazy architectural checks (put more symbols
  238.      into @EXPORT_FAIL by default and then take them out if someone actually
  239.      tries to use them and an expensive check shows that they are usable on
  240.      that platform).
  241.  
  242.      TTTTaaaagggg HHHHaaaannnnddddlllliiiinnnngggg UUUUttttiiiilllliiiittttyyyy FFFFuuuunnnnccccttttiiiioooonnnnssss
  243.  
  244.      Since the symbols listed within %EXPORT_TAGS must also appear in either
  245.      @EXPORT or @EXPORT_OK, two utility functions are provided which allow you
  246.      to easily add tagged sets of symbols to @EXPORT or @EXPORT_OK:
  247.  
  248.        %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]);
  249.  
  250.        Exporter::export_tags('foo');     # add aa, bb and cc to @EXPORT
  251.        Exporter::export_ok_tags('bar');  # add aa, cc and dd to @EXPORT_OK
  252.  
  253.      Any names which are not tags are added to @EXPORT or @EXPORT_OK unchanged
  254.      but will trigger a warning (with -w) to avoid misspelt tags names being
  255.      silently added to @EXPORT or @EXPORT_OK. Future versions may make this a
  256.      fatal error.
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))                                                        EEEExxxxppppoooorrrrtttteeeerrrr((((3333))))
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.                                                                         PPPPaaaaggggeeee 5555
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.