File Coverage

blib/lib/perl5i/1.pm
Criterion Covered Total %
statement 88 109 80.7
branch 1 16 6.2
condition 0 3 0.0
subroutine 20 25 80.0
pod 0 4 0.0
total 109 157 69.4


line stmt bran cond sub pod time code
1             # vi: set ts=4 sw=4 ht=4 et :
2             package perl5i::1;
3              
4 2     2   34717 use 5.010;
  2         8  
  2         95  
5              
6 2     2   11 use strict;
  2         7  
  2         71  
7 2     2   10 use warnings;
  2         4  
  2         76  
8 2     2   3117 use Module::Load;
  2         3920  
  2         19  
9 2     2   4792 use IO::Handle;
  2         20003  
  2         112  
10 2     2   158 use Carp;
  2         5  
  2         127  
11 2     2   1632 use perl5i::1::DateTime;
  2         7  
  2         65  
12 2     2   1925 use Want;
  2         4408  
  2         145  
13 2     2   1965 use Try::Tiny;
  2         15747  
  2         118  
14 2     2   1163 use perl5i::1::Meta;
  2         7  
  2         71  
15 2     2   2654 use Encode ();
  2         36520  
  2         52  
16 2     2   18 use perl5i::1::autobox;
  2         5  
  2         22  
17              
18 2     2   2835 use perl5i::VERSION; our $VERSION = perl5i::VERSION->VERSION;
  2         6  
  2         132  
19              
20             our $Latest = 'perl5i::1';
21              
22              
23             # This works around their lexical nature.
24 2     2   15 use parent 'autodie';
  2         5  
  2         22  
25 2     2   41262 use parent 'perl5i::1::autobox';
  2         6  
  2         15  
26 2     2   147 use parent 'autovivification';
  2         4  
  2         9  
27 2     2   4425 use parent 'utf8';
  2         4  
  2         15  
28 2     2   2816 use parent 'open';
  2         6  
  2         13  
29              
30             ## no critic (Subroutines::RequireArgUnpacking)
31             sub import {
32 2     2   23 my $class = shift;
33              
34 2         2102 require File::stat;
35              
36 2         21810 require Modern::Perl;
37 2         9837 Modern::Perl->import;
38              
39 2         363 my $caller = caller;
40              
41             # Modern::Perl won't pass this through to our caller.
42 2         19 require mro;
43 2         12 mro::set_mro( $caller, 'c3' );
44              
45 2         23 load_in_caller( $caller => (
46             ["CLASS"], ["File::chdir"],
47             [English => qw(-no_match_vars)],
48             ["Want" => qw(want)], ["Try::Tiny"], ["Perl6::Caller"], ["Carp"]
49             ) );
50              
51             # Have to call both or it won't work.
52 2         26 perl5i::1::autobox::import($class);
53 2         1137 autovivification::unimport($class);
54 2         63 utf8::import($class);
55              
56 2         15 open::import($class, ":encoding(utf8)");
57 2         3535 open::import($class, ":std");
58              
59             # Export our gmtime() and localtime()
60 2         365 (\&{$Latest .'::DateTime::dt_gmtime'})->alias($caller, 'gmtime');
  2         52  
61 2         3 (\&{$Latest .'::DateTime::dt_localtime'})->alias($caller, 'localtime');
  2         16  
62 2         3 (\&{$Latest .'::DateTime::dt_time'})->alias($caller, 'time');
  2         16  
63 2         12 (\&stat)->alias( $caller, 'stat' );
64 2         13 (\&lstat)->alias( $caller, 'lstat' );
65 2         12 (\&utf8_open)->alias($caller, 'open');
66              
67             # fix die so that it always returns 255
68             *CORE::GLOBAL::die = sub {
69             # Leave a single ref be
70 0     0   0 local $! = 255;
71 0 0 0     0 return CORE::die(@_) if @_ == 1 and ref $_[0];
72              
73 0         0 my $error = join '', @_;
74 0 0       0 unless ($error =~ /\n$/) {
75 0         0 my ($file, $line) = (caller)[1,2];
76 0         0 $error .= " at $file line $line.\n";
77             }
78              
79 0         0 local $! = 255;
80 0         0 return CORE::die($error);
81 2         19 };
82              
83              
84             # utf8ify @ARGV
85 2         9 $_ = Encode::decode('utf8', $_) for @ARGV;
86              
87              
88 2         8 $^H{perl5i} = 1;
89              
90             # autodie needs a bit more convincing
91 2         7 @_ = ( $class, ":all" );
92 2         15 goto &autodie::import;
93             }
94              
95 0     0   0 sub unimport { $^H{perl5i} = 0 }
96              
97             sub utf8_open(*;$@) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
98 0     0 0 0 my $ret;
99 0 0       0 if( @_ == 1 ) {
100 0         0 $ret = CORE::open $_[0];
101             }
102             else {
103 0         0 $ret = CORE::open $_[0], $_[1], @_[2..$#_];
104             }
105              
106             # Don't try to binmode an unopened filehandle
107 0 0       0 return $ret unless $ret;
108              
109 0         0 my $h = (caller 1)[10];
110 0 0       0 binmode $_[0], ":encoding(utf8)" if $h->{perl5i};
111 0         0 return $ret;
112             }
113              
114              
115             sub load_in_caller {
116 2     2 0 5 my $caller = shift;
117 2         8 my @modules = @_;
118              
119 2         6 for my $spec (@modules) {
120 14         48 my( $module, @args ) = @$spec;
121              
122 14         57 load($module);
123             ## no critic (BuiltinFunctions::ProhibitStringyEval)
124 14 50       28409 eval qq{
125             package $caller;
126             \$module->import(\@args);
127             1;
128             } or die "Error while perl5i loaded $module => @args: $@";
129             }
130              
131 2         8 return;
132             }
133              
134              
135             # File::stat does not play nice in list context
136             sub stat {
137 0 0   0 0   return CORE::stat(@_) if wantarray;
138 0           return File::stat::stat(@_);
139             }
140              
141             sub lstat {
142 0 0   0 0   return CORE::lstat(@_) if wantarray;
143 0           return File::stat::lstat(@_);
144             }
145              
146             1;
147