File Coverage

blib/lib/Acme/Damn.pm
Criterion Covered Total %
statement 47 47 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 9 9 100.0
pod n/a
total 63 65 96.9


line stmt bran cond sub pod time code
1             package Acme::Damn;
2              
3 6     6   121112 use 5.000;
  6         17  
4 6     6   28 use strict;
  6         8  
  6         115  
5 6     6   21 use warnings;
  6         10  
  6         137  
6              
7 6     6   20 use Exporter;
  6         7  
  6         245  
8 6     6   23 use DynaLoader qw( AUTOLOAD );
  6         6  
  6         186  
9              
10 6     6   19 use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK );
  6         8  
  6         1791  
11              
12             $VERSION = '0.07';
13             @ISA = qw( Exporter DynaLoader );
14             @EXPORT = qw( damn );
15             @EXPORT_OK = qw( bless );
16              
17             # ensure we aren't exposed to changes in inherited AUTOLOAD behaviour
18             *Acme::Damn::AUTOLOAD = *DynaLoader::AUTOLOAD;
19              
20              
21             sub import
22             {
23 9     9   654 my $class = shift;
24              
25             # check the unknown symbols to ensure they are 'safe'
26 9         16 my @bad = grep { /\W/o } @_;
  26         49  
27 9 100       30 if ( @bad ) {
28             # throw an error message informing the user where the problem is
29 3         8 my ( undef, $file , $line ) = caller 0;
30              
31             die sprintf( "Bad choice of symbol name%s %s for import at %s line %s\n"
32             , ( @bad == 1 ) ? '' : 's'
33 3 50       91 , join( ', ' , map { qq|'$_'| } @bad ) , $file , $line );
  3         30  
34             }
35              
36             # remove duplicates from the list of aliases, as well as those symbol
37             # names listed in @EXPORT
38             # - we keep @EXPORT_OK in a separate list since they are optionally
39             # requested at use() time
40 6         18 my @aliases = do { local %_;
  6         9  
41 6         91 @_{ @_ } = undef;
42 6         16 delete @_{ @EXPORT };
43 6         28 keys %_
44             };
45              
46             # 'import' the symbols into the host package
47             # - ensure 'EXPORT_OK' is correctly honoured
48 6         17 my %reserved = map { $_ => 1 } @EXPORT , @EXPORT_OK;
  12         32  
49 6         7 my @reserved = ();
50 6         31 my ( $pkg ) = caller 1;
51 6         87 foreach my $alias ( @aliases ) {
52             # if this alias is a reserved symbol as defined by @EXPORT et al.
53             # then add it to the list of symbols to export
54 21 100 66     49 $reserved{ $alias }
55             and push @reserved , $alias
56             and next;
57              
58             # otherwise, create an alias for 'damn'
59 6     6   30 no strict 'refs';
  6         11  
  6         986  
60              
61 20         94 *{ $pkg . '::' . $alias } = sub {
62 30     30   14126 my $ref = shift;
63 30         70 my ( undef , $file , $line ) = caller 1;
64              
65             # call damn() with the location of where this method was
66             # originally called
67 30         1164 &{ __PACKAGE__ . '::damn' }( $ref , $alias , $file , $line );
  30         235  
68              
69             # NB: wanted to do something like
70             # goto \&{ __PACKAGE__ . '::damn' };
71             # having set the @_ array appropriately, but this caused a
72             # "Attempt to free unrefernced SV" error that I couldn't solve
73             # - I think it was to do with the @_ array
74 20         63 };
75             }
76              
77             # add the known symbols to @_
78 6         18 splice @_ , 0; push @_ , $class , @reserved;
  6         10  
79              
80             # run the "proper" import() routine
81 6         3833 goto \&Exporter::import;
82             } # import()
83              
84              
85             bootstrap Acme::Damn $VERSION;
86              
87              
88             1; # end of module
89             __END__