Документация
HTML CSS PHP PERL другое

Section 15.4.  %EXPORT_TAGS

 
Previous
Table of Contents
Next

15.4. %EXPORT_TAGS

We don't have to list every function or variable that we want to import, either. We can create shortcuts, or tags, to group them under a single name. In the import list, we precede the tag name with a colon. For example, the core Fcntl module makes the flock constants available as a group with the :flock tag:

use Fcntl qw( :flock );        # import all flock constants

As described in the Exporter documentation, a few shortcuts are available automatically. The DEFAULT tag pulls in the same things as if we had provided no import list:

use Navigate::SeatOfPants qw(:DEFAULT);

That isn't very useful on its own, but if we want to pull in the default symbols and more, we don't have to type everything out simply because we supply an import list:

use Navigate::SeatOfPants qw(:DEFAULT get_north_from_professor);

These are rarely seen in practice. Why? The purpose of explicitly providing an import list is generally to control the subroutine names we use in our program. Those last examples do not insulate us from future changes to the module, which may import additional subroutines that could collide with our code.[Section 15.4.  %EXPORT_TAGS] In a few cases, a module may supply dozens or hundreds of possible symbols. These modules can use advanced techniques (described in the Exporter documentation) to make it easy to import batches of related symbols.

[Section 15.4.  %EXPORT_TAGS] For this reason, it is generally considered a bad idea for an update to a released module to introduce new default imports. If you know that your first release is still missing a function, though, there's no reason why you can't put in a placeholder: sub according_to_GPS { die "not implemented yet" }.

In our modules, we use the %EXPORT_TAGS hash to define these tags. The hash key is the name of the tag (without the colon), and the value is an anonymous array of symbols.

package Navigate::SeatOfPants;
use base qw(Exporter);

our @EXPORT    = qw(guess_direction_toward);
our @EXPORT_OK = qw(
                get_north_from_professor
                according_to_GPS
                ask_the_skipper_about
                );

our %EXPORT_TAGS = (
        all       => [ @EXPORT, @EXPORT_OK ],
        gps       => [ qw( according_to_GPS ) ],
        direction => [ qw(
                get_north_from_professor
                according_to_GPS
                guess_direction_toward
                ask_the_skipper_about
                ) ],
        );

Our first tag, all, includes all of the exportable symbols (everything in both @EXPORT and @EXPORT_OK). The gps tag comprises only the functions that deal with GPS, and the direction tag includes all the functions that deal with direction. The tags can contain overlaps too, and you'll notice that according_to_GPS shows up in each one of them. No matter how we define our tags, everything they include has to be either in @EXPORT or @EXPORT_OK.

Once we define our export tags, our users can use them in their import lists:

use Navigate::SeatOfPants qw(:direction);


Previous
Table of Contents
Next
© 2000- NIV