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 1     1   19728 use 5.010;
  1         3  
  1         32  
5              
6 1     1   4 use strict;
  1         1  
  1         23  
7 1     1   8 use warnings;
  1         2  
  1         27  
8 1     1   497 use Module::Load;
  1         880  
  1         5  
9 1     1   679 use IO::Handle;
  1         5560  
  1         59  
10 1     1   8 use Carp;
  1         1  
  1         48  
11 1     1   454 use perl5i::1::DateTime;
  1         4  
  1         41  
12 1     1   513 use Want;
  1         1466  
  1         57  
13 1     1   528 use Try::Tiny;
  1         2042  
  1         49  
14 1     1   368 use perl5i::1::Meta;
  1         2  
  1         28  
15 1     1   538 use Encode ();
  1         10272  
  1         121  
16 1     1   13 use perl5i::1::autobox;
  1         1  
  1         12  
17              
18 1     1   818 use perl5i::VERSION; our $VERSION = perl5i::VERSION->VERSION;
  1         1  
  1         47  
19              
20             our $Latest = 'perl5i::1';
21              
22              
23             # This works around their lexical nature.
24 1     1   5 use parent 'autodie';
  1         1  
  1         6  
25 1     1   14148 use parent 'perl5i::1::autobox';
  1         1  
  1         5  
26 1     1   59 use parent 'autovivification';
  1         1  
  1         3  
27 1     1   1332 use parent 'utf8';
  1         2  
  1         3  
28 1     1   579 use parent 'open';
  1         1  
  1         5  
29              
30             ## no critic (Subroutines::RequireArgUnpacking)
31             sub import {
32 1     1   7 my $class = shift;
33              
34 1         457 require File::stat;
35              
36 1         5864 require Modern::Perl;
37 1         2518 Modern::Perl->import;
38              
39 1         203 my $caller = caller;
40              
41             # Modern::Perl won't pass this through to our caller.
42 1         6 require mro;
43 1         3 mro::set_mro( $caller, 'c3' );
44              
45 1         8 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 1         7 perl5i::1::autobox::import($class);
53 1         432 autovivification::unimport($class);
54 1         29 utf8::import($class);
55              
56 1         6 open::import($class, ":encoding(utf8)");
57 1         1098 open::import($class, ":std");
58              
59             # Export our gmtime() and localtime()
60 1         135 (\&{$Latest .'::DateTime::dt_gmtime'})->alias($caller, 'gmtime');
  1         19  
61 1         1 (\&{$Latest .'::DateTime::dt_localtime'})->alias($caller, 'localtime');
  1         6  
62 1         3 (\&{$Latest .'::DateTime::dt_time'})->alias($caller, 'time');
  1         6  
63 1         4 (\&stat)->alias( $caller, 'stat' );
64 1         3 (\&lstat)->alias( $caller, 'lstat' );
65 1         4 (\&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 1         6 };
82              
83              
84             # utf8ify @ARGV
85 1         4 $_ = Encode::decode('utf8', $_) for @ARGV;
86              
87              
88 1         5 $^H{perl5i} = 1;
89              
90             # autodie needs a bit more convincing
91 1         3 @_ = ( $class, ":all" );
92 1         5 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 1     1 0 2 my $caller = shift;
117 1         3 my @modules = @_;
118              
119 1         2 for my $spec (@modules) {
120 7         13 my( $module, @args ) = @$spec;
121              
122 7         19 load($module);
123             ## no critic (BuiltinFunctions::ProhibitStringyEval)
124 7 50       8104 eval qq{
125             package $caller;
126             \$module->import(\@args);
127             1;
128             } or die "Error while perl5i loaded $module => @args: $@";
129             }
130              
131 1         4 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