File Coverage

blib/lib/perl5i/2.pm
Criterion Covered Total %
statement 70 72 97.2
branch 14 16 87.5
condition 5 5 100.0
subroutine 16 16 100.0
pod 0 5 0.0
total 105 114 92.1


line stmt bran cond sub pod time code
1             # vi: set ts=4 sw=4 ht=4 et :
2             package perl5i::2;
3              
4 101     101   77936 use 5.010;
  101         413  
  101         3669  
5              
6 101     101   433 use strict;
  101         124  
  101         3243  
7 101     101   409 use warnings;
  101         118  
  101         2924  
8              
9             #This should come first
10 101     101   35461 use perl5i::2::RequireMessage;
  101         173  
  101         2641  
11              
12 101     101   49963 use Carp::Fix::1_25;
  101         60555  
  101         7728  
13 101     101   34949 use perl5i::2::autobox;
  101         271  
  101         716  
14 101     101   102624 use Import::Into;
  101         41837  
  101         2974  
15              
16 101     101   1138 use perl5i::VERSION; our $VERSION = perl5i::VERSION->VERSION;
  101         131  
  101         93615  
17              
18             our $Latest = perl5i::VERSION->latest;
19              
20             my %Features = (
21             # A stub for autodie. It's handled specially in import().
22             autodie => sub {},
23             autobox => sub {
24             my ($class, $caller) = @_;
25             perl5i::2::autobox->import::into($caller);
26             },
27             autovivification => sub {
28             my ($class, $caller) = @_;
29             autovivification->unimport::out_of($caller);
30             },
31             capture => sub {
32             my ($class, $caller) = @_;
33             (\&capture)->alias($caller, "capture");
34             },
35             "Carp::Fix::1_25" => sub {
36             my ($class, $caller) = @_;
37             Carp::Fix::1_25->import::into($caller);
38             },
39             Child => sub {
40             my ($class, $caller) = @_;
41             Child->import::into( $caller => qw(child) );
42             },
43             CLASS => sub {
44             my ($class, $caller) = @_;
45             CLASS->import::into( $caller );
46             },
47             die => sub {
48             my ($class, $caller) = @_;
49             (\&perl5i_die)->alias($caller, "die");
50             },
51             English => sub {
52             my ($class, $caller) = @_;
53             English->import::into( $caller => qw(-no_match_vars) );
54             },
55             'File::chdir' => sub {
56             my ($class, $caller) = @_;
57             File::chdir->import::into( $caller );
58             },
59             indirect => sub {
60             my ($class, $caller) = @_;
61             indirect->unimport::out_of($caller, ":fatal");
62             },
63             list => sub {
64             my ($class, $caller) = @_;
65             (\&force_list_context)->alias($caller, 'list');
66             },
67             Meta => sub {
68             require perl5i::2::Meta;
69             },
70             'Modern::Perl' => sub {
71             my ($class, $caller) = @_;
72              
73             # use Modern::Perl
74             Modern::Perl->import::into($caller);
75              
76             # no strict vars for oneliners - GH #63
77             strict::unimport($class, 'vars')
78             if $class eq 'perl5i::cmd'
79             or $0 eq '-e';
80              
81             # Modern::Perl won't pass this through to our caller.
82             require mro;
83             mro::set_mro($caller, 'c3');
84             },
85             'Perl6::Caller' => sub {
86             my ($class, $caller) = @_;
87             Perl6::Caller->import::into($caller);
88             },
89             Signatures => sub {
90             my ($class, $caller) = @_;
91             perl5i::2::Signatures->import::into($caller);
92             },
93             stat => sub {
94             my ($class, $caller) = @_;
95              
96             require File::stat;
97             # Export our stat and lstat
98             (\&stat)->alias($caller, 'stat');
99             (\&lstat)->alias($caller, 'lstat');
100             },
101             time => sub {
102             my ($class, $caller) = @_;
103              
104             require perl5i::2::DateTime;
105             # Export our gmtime() and localtime()
106             (\&perl5i::2::DateTime::dt_gmtime)->alias($caller, 'gmtime');
107             (\&perl5i::2::DateTime::dt_localtime)->alias($caller, 'localtime');
108             (\&perl5i::2::DateTime::dt_time)->alias($caller, 'time');
109             },
110             true => sub {
111             my ($class, $caller) = @_;
112             true->import::into($caller);
113             },
114             'Try::Tiny' => sub {
115             my ($class, $caller) = @_;
116             Try::Tiny->import::into($caller);
117             },
118             'utf8::all' => sub {
119             my ($class, $caller) = @_;
120             utf8::all->import::into($caller);
121             "feature"->unimport::out_of($caller, "unicode_eval") if $^V >= v5.16.0;
122             },
123             Want => sub {
124             my ($class, $caller) = @_;
125             Want->import::into( $caller => qw(want) );
126             },
127             );
128              
129             # This is necessary for autodie to work and be lexical
130 101     101   561 use parent 'autodie';
  101         153  
  101         808  
131              
132             ## no critic (Subroutines::RequireArgUnpacking)
133             sub import {
134 113     113   1834 my $class = shift;
135 113         253 my %import = @_;
136              
137 113         247 my $caller = caller;
138              
139             # Read the skip list and turn it into a hash
140 113   100     1214 my $skips = delete $import{-skip} || [];
141 113         302 $skips = { map { $_ => 1 } @$skips };
  3         12  
142              
143             # Any remaining import parameters are unknown
144 113 100       480 if( keys %import ) {
145 1         153 croak sprintf "Unknown parameters '%s' in import list",
146 1         4 join(", ", map { "$_ => $import{$_}" } keys %import);
147             }
148              
149             # Check all the skipped features are valid
150 112         382 for my $f ( grep { !exists $Features{$_} } keys %$skips ) {
  3         13  
151 1         201 croak "Unknown feature '$f' in skip list";
152             }
153              
154             # Current lexically active major version of perl5i.
155 111         572 $^H{perl5i} = 2;
156              
157             # Load all the features.
158 111         569 for my $feature (keys %Features) {
159 2442 100       2746350 next if $skips->{$feature};
160 2440         8461 $Features{$feature}->($class, $caller);
161             }
162              
163             # autodie needs a bit more convincing
164 111 100       132665 if( !$skips->{autodie} ) {
165 110         459 @_ = ( $class, ":all" );
166 110         798 goto &autodie::import;
167             }
168             }
169              
170 1     1   160424 sub unimport { $^H{perl5i} = 0 }
171              
172             # fix die so that it always returns 255
173             sub perl5i_die {
174             # Leave a single ref be
175 21     21 0 1004358 local $! = 255;
176 21 100 100     121 return CORE::die(@_) if @_ == 1 and ref $_[0];
177              
178 18         39 my $error = join '', @_;
179 18 100       58 unless ($error =~ /\n$/) {
180 15         41 my ($file, $line) = (caller)[1,2];
181 15         43 $error .= " at $file line $line.\n";
182             }
183              
184 18         148 local $! = 255;
185 18         94 return CORE::die($error);
186             }
187              
188              
189             # File::stat does not play nice in list context
190             sub stat {
191 3 100   3 0 1788 return CORE::stat(@_) if wantarray;
192 2         13 return File::stat::stat(@_);
193             }
194              
195             sub lstat {
196 1 50   1 0 19 return CORE::lstat(@_) if wantarray;
197 0         0 return File::stat::lstat(@_);
198             }
199              
200              
201             sub capture(&;@) {
202 16     16 0 2796400 my($code, %opts) = @_;
203              
204             # valid options
205 16         47 state $valid_options = { map { $_ => 1 } qw(merge tee) };
  6         24  
206              
207 16         95 for my $key (keys %opts) {
208 2 50       13 croak "$key is not a valid option to capture()" unless $valid_options->{$key};
209             }
210              
211 16         83 my $opts = join "/", sort { $a cmp $b } grep { $opts{$_} } keys %opts;
  0         0  
  2         15  
212              
213             # Translate option combinations into Capture::Tiny functions
214 16         1981 require Capture::Tiny;
215 16         35911 state $captures = {
216             "" => \&Capture::Tiny::capture,
217             "tee" => \&Capture::Tiny::tee,
218             "merge" => \&Capture::Tiny::capture_merged,
219             "merge/tee" => \&Capture::Tiny::tee_merged
220             };
221              
222 16         50 my $func = $captures->{$opts};
223 16         553 return $func->($code);
224             }
225              
226              
227             sub force_list_context(@) {
228 6     6 0 3723 return @_;
229             }
230              
231             1;