File Coverage

blib/lib/Sub/Exporter/GlobExporter.pm
Criterion Covered Total %
statement 33 34 97.0
branch 7 8 87.5
condition 2 3 66.6
subroutine 9 9 100.0
pod 0 1 0.0
total 51 55 92.7


line stmt bran cond sub pod time code
1 1     1   30290 use strict;
  1         2  
  1         28  
2 1     1   6 use warnings;
  1         2  
  1         52  
3             package Sub::Exporter::GlobExporter;
4             {
5             $Sub::Exporter::GlobExporter::VERSION = '0.004';
6             }
7             # ABSTRACT: export shared globs with Sub::Exporter collectors
8              
9 1     1   5 use Scalar::Util ();
  1         1  
  1         27  
10              
11 1     1   7049 use Sub::Exporter -setup => [ qw(glob_exporter) ];
  1         18281  
  1         11  
12              
13              
14             my $is_ref;
15             BEGIN {
16             $is_ref = sub {
17             return(
18 3   66     34 ! Scalar::Util::blessed($_[0])
19             && Scalar::Util::reftype($_[0]) eq $_[1]
20             );
21 1     1   759 };
22             }
23              
24             sub glob_exporter {
25 2     2 0 278 my ($default_name, $globref) = @_;
26              
27 2     2   14 my $globref_method = $is_ref->($globref, 'GLOB') ? sub { $globref }
28 2 50       5 : $is_ref->($globref, 'SCALAR') ? $$globref
    100          
29             : Carp::confess("illegal glob locator '$globref'");
30              
31             return sub {
32 2     2   1047 my ($value, $data) = @_;
33              
34             my @args = defined $value
35 2 100       8 ? ({ map {; $_ => $value->{$_} } grep { ! /^-/ } keys %$value })
  0         0  
  1         13  
36             : ();
37              
38 2         7 my $globref = $data->{class}->$globref_method(@args);
39              
40             # allow a SCALAR ref in the future to do ($$as = $globref) as we allow subs
41             # to be exported into scalar refs -- rjbs, 2010-11-23
42 2         4 my $name;
43 2 100       7 $name = defined $value->{'-as'} ? $value->{'-as'} : $default_name;
44              
45 2         7 my $sym = "$data->{into}::$name";
46              
47             {
48 1     1   14 no strict 'refs';
  1         2  
  1         131  
  2         3  
49 2         3 *{$sym} = *$globref;
  2         14  
50             }
51              
52 2         4 $_[0] = $globref;
53 2         6 return 1;
54             }
55 2         18 }
56              
57             1;
58              
59             __END__