File Coverage

blib/lib/perl5i/0.pm
Criterion Covered Total %
statement 110 131 83.9
branch 1 16 6.2
condition 0 3 0.0
subroutine 26 31 83.8
pod 0 4 0.0
total 137 185 74.0


line stmt bran cond sub pod time code
1             # vi: set ts=4 sw=4 ht=4 et :
2             package perl5i::0;
3              
4 1     1   4196 use 5.010;
  1         4  
  1         43  
5              
6 1     1   6 use strict;
  1         2  
  1         33  
7 1     1   5 use warnings;
  1         3  
  1         30  
8 1     1   962 use Module::Load;
  1         1495  
  1         9  
9 1     1   1185 use IO::Handle;
  1         8670  
  1         53  
10 1     1   8 use Carp;
  1         2  
  1         55  
11 1     1   2120 use perl5i::0::DateTime;
  1         5  
  1         36  
12 1     1   1726 use perl5i::0::SCALAR;
  1         6  
  1         51  
13 1     1   3065 use perl5i::0::ARRAY;
  1         4  
  1         205  
14 1     1   2452 use perl5i::0::HASH;
  1         3  
  1         81  
15 1     1   11794 use perl5i::0::DEFAULT;
  1         7  
  1         35  
16 1     1   1396 use Want;
  1         3774  
  1         82  
17 1     1   1480 use Try::Tiny;
  1         5265  
  1         202  
18 1     1   2147 use perl5i::0::Meta;
  1         4  
  1         32  
19 1     1   7 use autobox;
  1         2  
  1         9  
20 1     1   4149 use Encode ();
  1         15785  
  1         33  
21              
22 1     1   600 use perl5i::VERSION; our $VERSION = perl5i::VERSION->VERSION;
  1         3  
  1         221  
23              
24             our $Latest = 'perl5i::0';
25              
26              
27             # This works around their lexical nature.
28 1     1   11 use parent 'autodie';
  1         4  
  1         12  
29             # List::Util needs to be before Core to get the C version of sum
30 1     1   28699 use parent 'autobox::List::Util';
  1         192  
  1         11  
31 1     1   2386 use parent 'autobox::Core';
  1         2  
  1         5  
32 1     1   12574 use parent 'autobox::dump';
  1         3  
  1         8  
33 1     1   2111 use parent 'autovivification';
  1         2  
  1         6  
34 1     1   2195 use parent 'utf8';
  1         4  
  1         7  
35 1     1   1505 use parent 'open';
  1         2  
  1         6  
36              
37             ## no critic (Subroutines::RequireArgUnpacking)
38             sub import {
39 1     1   16 my $class = shift;
40              
41 1         1555 require File::stat;
42              
43 1         12003 require Modern::Perl;
44 1         5178 Modern::Perl->import;
45              
46 1         303 my $caller = caller;
47              
48             # Modern::Perl won't pass this through to our caller.
49 1         12 require mro;
50 1         8 mro::set_mro( $caller, 'c3' );
51              
52 1         14 load_in_caller( $caller => (
53             ["CLASS"], ["File::chdir"],
54             [English => qw(-no_match_vars)],
55             ["Want" => qw(want)], ["Try::Tiny"], ["Perl6::Caller"],
56             ) );
57              
58             # Have to call both or it won't work.
59 1         9 autobox::import($class);
60 1         384 autobox::List::Util::import($class);
61 1         509 autobox::Core::import($class);
62 1         609 autobox::dump::import($class);
63 1         733 autovivification::unimport($class);
64 1         27 utf8::import($class);
65              
66 1         7 open::import($class, ":encoding(utf8)");
67 1         1627 open::import($class, ":std");
68              
69             # Export our gmtime() and localtime()
70 1         196 (\&{$Latest .'::DateTime::dt_gmtime'})->alias($caller, 'gmtime');
  1         26  
71 1         2 (\&{$Latest .'::DateTime::dt_localtime'})->alias($caller, 'localtime');
  1         9  
72 1         2 (\&{$Latest .'::DateTime::dt_time'})->alias($caller, 'time');
  1         8  
73 1         6 (\&alias)->alias( $caller, 'alias' );
74 1         5 (\&stat)->alias( $caller, 'stat' );
75 1         6 (\&lstat)->alias( $caller, 'lstat' );
76 1         5 (\&utf8_open)->alias($caller, 'open');
77              
78             # fix die so that it always returns 255
79             *CORE::GLOBAL::die = sub {
80             # Leave a single ref be
81 0     0   0 local $! = 255;
82 0 0 0     0 return CORE::die(@_) if @_ == 1 and ref $_[0];
83              
84 0         0 my $error = join '', @_;
85 0 0       0 unless ($error =~ /\n$/) {
86 0         0 my ($file, $line) = (caller)[1,2];
87 0         0 $error .= " at $file line $line.\n";
88             }
89              
90 0         0 local $! = 255;
91 0         0 return CORE::die($error);
92 1         7 };
93              
94              
95             # utf8ify @ARGV
96 1         3 $_ = Encode::decode('utf8', $_) for @ARGV;
97              
98              
99 1         4 $^H{perl5i} = 1;
100              
101             # autodie needs a bit more convincing
102 1         3 @_ = ( $class, ":all" );
103 1         6 goto &autodie::import;
104             }
105              
106 0     0   0 sub unimport { $^H{perl5i} = 0 }
107              
108             sub utf8_open(*;$@) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
109 0     0 0 0 my $ret;
110 0 0       0 if( @_ == 1 ) {
111 0         0 $ret = CORE::open $_[0];
112             }
113             else {
114 0         0 $ret = CORE::open $_[0], $_[1], @_[2..$#_];
115             }
116              
117             # Don't try to binmode an unopened filehandle
118 0 0       0 return $ret unless $ret;
119              
120 0         0 my $h = (caller 1)[10];
121 0 0       0 binmode $_[0], ":encoding(utf8)" if $h->{perl5i};
122 0         0 return $ret;
123             }
124              
125              
126             sub load_in_caller {
127 1     1 0 3 my $caller = shift;
128 1         3 my @modules = @_;
129              
130 1         5 for my $spec (@modules) {
131 6         24 my( $module, @args ) = @$spec;
132              
133 6         24 load($module);
134             ## no critic (BuiltinFunctions::ProhibitStringyEval)
135 6 50       26751 eval qq{
136             package $caller;
137             \$module->import(\@args);
138             1;
139             } or die "Error while perl5i loaded $module => @args: $@";
140             }
141              
142 1         4 return;
143             }
144              
145              
146             # File::stat does not play nice in list context
147             sub stat {
148 0 0   0 0   return CORE::stat(@_) if wantarray;
149 0           return File::stat::stat(@_);
150             }
151              
152             sub lstat {
153 0 0   0 0   return CORE::lstat(@_) if wantarray;
154 0           return File::stat::lstat(@_);
155             }
156              
157             1;
158