Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Exporter::Auto;
- our $VERSION = 0.10;
- our $AUTHOR = "Tero Niemi <tero.niemi\100nimbus\056fi>";
- #
- # Create Exporter variables (and tag groups) automatically.
- # In your modules, use like Exporter. F.ex.:
- #
- # use parent 'Exporter::Auto';
- #
- # The following variables will be created,
- # or, if they exists, extended:
- #
- # our $VERSION = 0.1;
- # our @EXPORT_OK;
- # our %EXPORT_TAGS;
- #
- # All subs are automatically marked 'OK' for exporting.
- # Variables have to be manually added:
- #
- # our @EXPORT_OK = qw( %hash @array $scalar );
- #
- use warnings;
- use strict;
- no strict 'refs';
- require Exporter;
- sub import {
- my $package = $_[0].'::'; # Package name
- my %export_ok = (); # List of exportable items
- my %export_tags = (); # Exportable items grouped into tags
- # Add items in @EXPORT and @EXPORT_OK into exportable list
- foreach (@{$package.'EXPORT'}, @{$package.'EXPORT_OK'}) {
- $export_ok{$_}++;
- }
- # Automatically add all subroutines into exportable list
- foreach (keys %{$package}) {
- next unless defined &{$package.$_};
- $export_ok{$_}++;
- }
- # Convert possibly predefined tag arrays into inner hashes
- %export_tags = (%{$package.'EXPORT_TAGS'});
- foreach my $tag (keys %export_tags) {
- my @items = (@{$export_tags{$tag}});
- $export_tags{$tag} = {};
- foreach (@items) {
- $export_tags{$tag}->{$_}++;
- }
- }
- # Automatically group items (by prefixes) into tags
- foreach (keys %export_ok) {
- my $prefix = /^[\%\@\$]?([^_]+)/i ? lc $1 : undef;
- $export_tags{$prefix}->{$_}++ if defined $prefix;
- $export_tags{all}->{$_}++;
- }
- # Convert inner hashes back into arrays
- foreach my $tag (keys %export_tags) {
- $export_tags{$tag} = [keys $export_tags{$tag}];
- }
- # Set variables ready for Exporter
- ${$package.'VERSION'} = 0.1 unless defined ${$package.'VERSION'};
- @{$package.'EXPORT_OK'} = @{$export_tags{all}};
- %{$package.'EXPORT_TAGS'} = %export_tags;
- # Let Exporter do the rest
- goto &Exporter::import;
- }
- 1;
- __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement