File Coverage

blib/lib/Exporter/Handy.pm
Criterion Covered Total %
statement 27 59 45.7
branch 0 14 0.0
condition 0 3 0.0
subroutine 9 15 60.0
pod 6 6 100.0
total 42 97 43.3


line stmt bran cond sub pod time code
1             package Exporter::Handy;
2              
3             # ABSTRACT: An EXPERIMENTAL subclass of , which helps create easy-to-extend modules that export symbols
4             our $VERSION = '1.000003';
5              
6 2     2   102327 use utf8;
  2         44  
  2         10  
7 2     2   61 use strict;
  2         4  
  2         37  
8 2     2   10 use warnings;
  2         3  
  2         67  
9              
10 2     2   1152 use Exporter::Extensible -exporter_setup => 1;
  2         14685  
  2         15  
11              
12              
13             # PRAGMATA
14             # Remember: Pragmas effect the current compilation context.
15             # No need to keep track of where we are importing into...
16             # They require their ->import() method to be called directly, no matter how deep the call stack happens to be.
17             # Just call ->import() directly, like below, and it will do the right thing.
18 2     2 1 950 sub strict : Export(-) { strict->import }
  2     0   1140  
  2         13  
  0         0  
19 2     2 1 1374 sub warnings : Export(-) { warnings->import }
  2     0   6  
  2         8  
  0         0  
20 2     2 1 455 sub utf8 : Export(-) { utf8->import }
  2     0   4  
  2         8  
  0         0  
21              
22             sub strictures : Export(-) {
23 0     0 1 0 strict->import;
24 0         0 warnings->import
25 2     2   449 }
  2         4  
  2         11  
26              
27             sub sane : Export(-) {
28 0     0 1   utf8->import;
29 0           strict->import;
30 0           warnings->import;
31 2     2   500 }
  2         4  
  2         7  
32              
33             # use Exporter::Handy qw(-sane -features), exporter_setup => 1;
34             sub features {
35 0     0 1   my ($exporter, $arg)= @_;
36              
37             # default features to turn on/off
38 0           my %feats = (
39             'current_sub' => 1, # Perl v5.16+ (2012) : enable __SUB__ token that returns a ref to the current subroutine (or undef).
40             'evalbytes' => 1, # Perl v5.16+ (2012) : like string eval, but it treats its argument as a byte string.
41             'fc' => 1, # Perl v5.16+ (2012) : enable the fc function (Unicode casefolding).
42             'lexical_subs' => 1, # Perl v5.18+ (2012) : enable declaration of subroutines via my sub foo, state sub foo and our sub foo syntax.
43             'say' => 1, # Perl v5.10+ (2007) : enable the Raku-inspired "say" function.
44             'state' => 1, # Perl v5.10+ (2007) : enable state variables.
45             'unicode_eval' => 1, # Perl v5.16+ (2012) : changes the behavior of plain string eval to work more consistently, especially in the Unicode world.
46             'unicode_strings' => 1, # Perl v5.12+ (2010) : use Unicode rules in all string operations (unless either use locale or use bytes are also within the scope).
47             );
48              
49 0           my @args = eval { @$arg }; # if $arg is an ARRAY-ref, than it denotes a list of features
  0            
50 0           my %args = eval { %$arg }; # if $arg is a HASH-ref, then it denotes individual overrides (1: on, 0:off)
  0            
51              
52 0 0         if (@args) {
53 0 0         if ($args[0] eq '+') { # request to keep defaults.
54 0           shift @args;
55 0           %args = map { $_ => 1 } @args;
  0            
56             } else { # replace defaults
57 0           %feats = map { $_ => 1 } @args;
  0            
58             }
59             }
60              
61             # handle individual overrides
62 0           %feats = (%feats, %args);
63              
64 0 0         return unless %feats;
65              
66             # determine features to be turned ON or OFF
67 0           my (@on, @off);
68 0           for (keys %feats) {
69 0 0         next if m/^-/; # ignore inline args à la , if any: -prefix, -as, ...
70              
71 0 0 0       if (defined $feats{$_} && $feats{$_}) {
72 0           push @on, $_;
73             } else {
74 0           push @off, $_;
75             }
76             }
77              
78             # Do the actual work
79 0           require feature;
80 0 0         feature->import(@on) if @on;
81 0 0         feature->unimport(@off) if @off;
82             }
83              
84             # Note that attribute syntax, i.e. :Export(-?), was not working before version 0.11 of .
85             # So we take the more verbose approach, as below:
86             __PACKAGE__->exporter_register_option('features', \&features, '?');
87              
88             1;
89              
90             __END__