| 1 | # vi: set ts=4 sw=4 ht=4 et : | ||
| 2 | package perl5i::0; | ||
| 3 | |||
| 4 | use 5.010; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Module::Load; | ||
| 9 | use IO::Handle; | ||
| 10 | use Carp; | ||
| 11 | use perl5i::0::DateTime; | ||
| 12 | use perl5i::0::SCALAR; | ||
| 13 | use perl5i::0::ARRAY; | ||
| 14 | use perl5i::0::HASH; | ||
| 15 | use perl5i::0::DEFAULT; | ||
| 16 | use Want; | ||
| 17 | use Try::Tiny; | ||
| 18 | use perl5i::0::Meta; | ||
| 19 | use autobox; | ||
| 20 | use Encode (); | ||
| 21 | |||
| 22 | use perl5i::VERSION; our $VERSION = perl5i::VERSION->VERSION; | ||
| 23 | |||
| 24 | our $Latest = 'perl5i::0'; | ||
| 25 | |||
| 26 | |||
| 27 | # This works around their lexical nature. | ||
| 28 | use parent 'autodie'; | ||
| 29 | # List::Util needs to be before Core to get the C version of sum | ||
| 30 | use parent 'autobox::List::Util'; | ||
| 31 | use parent 'autobox::Core'; | ||
| 32 | use parent 'autobox::dump'; | ||
| 33 | use parent 'autovivification'; | ||
| 34 | use parent 'utf8'; | ||
| 35 | use parent 'open'; | ||
| 36 | |||
| 37 | ## no critic (Subroutines::RequireArgUnpacking) | ||
| 38 | sub import { | ||
| 39 | my $class = shift; | ||
| 40 | |||
| 41 | require File::stat; | ||
| 42 | |||
| 43 | require Modern::Perl; | ||
| 44 | Modern::Perl->import; | ||
| 45 | |||
| 46 | my $caller = caller; | ||
| 47 | |||
| 48 | # Modern::Perl won't pass this through to our caller. | ||
| 49 | require mro; | ||
| 50 | mro::set_mro( $caller, 'c3' ); | ||
| 51 | |||
| 52 | 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 | autobox::import($class); | ||
| 60 | autobox::List::Util::import($class); | ||
| 61 | autobox::Core::import($class); | ||
| 62 | autobox::dump::import($class); | ||
| 63 | autovivification::unimport($class); | ||
| 64 | utf8::import($class); | ||
| 65 | |||
| 66 | open::import($class, ":encoding(utf8)"); | ||
| 67 | open::import($class, ":std"); | ||
| 68 | |||
| 69 | # Export our gmtime() and localtime() | ||
| 70 | (\&{$Latest .'::DateTime::dt_gmtime'})->alias($caller, 'gmtime'); | ||
| 71 | (\&{$Latest .'::DateTime::dt_localtime'})->alias($caller, 'localtime'); | ||
| 72 | (\&{$Latest .'::DateTime::dt_time'})->alias($caller, 'time'); | ||
| 73 | (\&alias)->alias( $caller, 'alias' ); | ||
| 74 | (\&stat)->alias( $caller, 'stat' ); | ||
| 75 | (\&lstat)->alias( $caller, 'lstat' ); | ||
| 76 | (\&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 | local $! = 255; | ||
| 82 | return CORE::die(@_) if @_ == 1 and ref $_[0]; | ||
| 83 | |||
| 84 | my $error = join '', @_; | ||
| 85 | unless ($error =~ /\n$/) { | ||
| 86 | my ($file, $line) = (caller)[1,2]; | ||
| 87 | $error .= " at $file line $line.\n"; | ||
| 88 | } | ||
| 89 | |||
| 90 | local $! = 255; | ||
| 91 | return CORE::die($error); | ||
| 92 | }; | ||
| 93 | |||
| 94 | |||
| 95 | # utf8ify @ARGV | ||
| 96 | $_ = Encode::decode('utf8', $_) for @ARGV; | ||
| 97 | |||
| 98 | |||
| 99 | $^H{perl5i} = 1; | ||
| 100 | |||
| 101 | # autodie needs a bit more convincing | ||
| 102 | @_ = ( $class, ":all" ); | ||
| 103 | goto &autodie::import; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub unimport { $^H{perl5i} = 0 } | ||
| 107 | |||
| 108 | sub utf8_open(*;$@) { ## no critic (Subroutines::ProhibitSubroutinePrototypes) | ||
| 109 | my $ret; | ||
| 110 | if( @_ == 1 ) { | ||
| 111 | $ret = CORE::open $_[0]; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | $ret = CORE::open $_[0], $_[1], @_[2..$#_]; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Don't try to binmode an unopened filehandle | ||
| 118 | return $ret unless $ret; | ||
| 119 | |||
| 120 | my $h = (caller 1)[10]; | ||
| 121 | binmode $_[0], ":encoding(utf8)" if $h->{perl5i}; | ||
| 122 | return $ret; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub load_in_caller { | ||
| 127 | my $caller = shift; | ||
| 128 | my @modules = @_; | ||
| 129 | |||
| 130 | for my $spec (@modules) { | ||
| 131 | my( $module, @args ) = @$spec; | ||
| 132 | |||
| 133 | load($module); | ||
| 134 | ## no critic (BuiltinFunctions::ProhibitStringyEval) | ||
| 135 | eval qq{ | ||
| 136 | package $caller; | ||
| 137 | \$module->import(\@args); | ||
| 138 | 1; | ||
| 139 | } or die "Error while perl5i loaded $module => @args: $@"; | ||
| 140 | } | ||
| 141 | |||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | # File::stat does not play nice in list context | ||
| 147 | sub stat { | ||
| 148 | return CORE::stat(@_) if wantarray; | ||
| 149 | return File::stat::stat(@_); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub lstat { | ||
| 153 | return CORE::lstat(@_) if wantarray; | ||
| 154 | return File::stat::lstat(@_); | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||
| 158 |