[sf-perl] exporting tags

Joe Brenner doom at kzsu.stanford.edu
Mon Aug 6 13:11:22 PDT 2007


David Alban <extasia at extasia.org> wrote:

> Greeings,
>
> I have:
>   package My::Module;
>
>   ...
>
>   BEGIN {
>      ...
>      @EXPORT = qw(
>               &sub_1
>               &sub_2
>               &sub_3
>             );
>
>      @EXPORT_OK = qw(
>                       $scalar_1
>                       $scalar_2
>                     );
>
>      %EXPORT_TAGS = ( foo => [ qw( $scalar_1 $scalar_2 ) ] );
>
>   }
>
> When the module is used as:
>
>   use My::Module qw( :foo );
>
> I see the scalars, but not the exported subroutines.  I can do:
>
>   use My::Module qw( :DEFAULT :foo );
>
> which works, but what I'd really like is to be able to do:
>
>   use My::Module qw( :foo );
>
> and get the subs in @EXPORT along with the subs in the :foo tag.
>
> I think I'm missing something.  Is this impossible?
>
> If I just did 'use My::Module;' I'd get everything in @EXPORT.  Why does
> that stop if I supply a tag.

When you ask for anything at all, the default exports are supressed

Even if you ask for the empty list:

   use My::Module ();

You won't get the items specified in @EXPORT.

(Think about it: otherwise, if you *wanted* to avoid a conflict from
something exported, you need some sort of syntax to *subtract off*
something from the export list.)


I would try something like this (warning: untested):

   require Exporter;

   our @ISA = qw(Exporter);

   our @EXPORT = qw(
               sub_1
               sub_2
               sub_3
   );

   our %EXPORT_TAGS = ( 'foo' => [
         qw(
            $scalar_1
            $scalar_2
           ),
         @EXPORT
      ] );

  our @EXPORT_OK = ( @{ $EXPORT_TAGS{'foo'} } );



More information about the SanFrancisco-pm mailing list