| 1 | # vi: set ts=4 sw=4 ht=4 et : | ||
| 2 | package perl5i::2; | ||
| 3 | |||
| 4 | use 5.010; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | #This should come first | ||
| 10 | use perl5i::2::RequireMessage; | ||
| 11 | |||
| 12 | use Carp::Fix::1_25; | ||
| 13 | use perl5i::2::autobox; | ||
| 14 | use Import::Into; | ||
| 15 | |||
| 16 | use perl5i::VERSION; our $VERSION = perl5i::VERSION->VERSION; | ||
| 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 | use parent 'autodie'; | ||
| 131 | |||
| 132 | ## no critic (Subroutines::RequireArgUnpacking) | ||
| 133 | sub import { | ||
| 134 | my $class = shift; | ||
| 135 | my %import = @_; | ||
| 136 | |||
| 137 | my $caller = caller; | ||
| 138 | |||
| 139 | # Read the skip list and turn it into a hash | ||
| 140 | my $skips = delete $import{-skip} || []; | ||
| 141 | $skips = { map { $_ => 1 } @$skips }; | ||
| 142 | |||
| 143 | # Any remaining import parameters are unknown | ||
| 144 | if( keys %import ) { | ||
| 145 | croak sprintf "Unknown parameters '%s' in import list", | ||
| 146 | join(", ", map { "$_ => $import{$_}" } keys %import); | ||
| 147 | } | ||
| 148 | |||
| 149 | # Check all the skipped features are valid | ||
| 150 | for my $f ( grep { !exists $Features{$_} } keys %$skips ) { | ||
| 151 | croak "Unknown feature '$f' in skip list"; | ||
| 152 | } | ||
| 153 | |||
| 154 | # Current lexically active major version of perl5i. | ||
| 155 | $^H{perl5i} = 2; | ||
| 156 | |||
| 157 | # Load all the features. | ||
| 158 | for my $feature (keys %Features) { | ||
| 159 | next if $skips->{$feature}; | ||
| 160 | $Features{$feature}->($class, $caller); | ||
| 161 | } | ||
| 162 | |||
| 163 | # autodie needs a bit more convincing | ||
| 164 | if( !$skips->{autodie} ) { | ||
| 165 | @_ = ( $class, ":all" ); | ||
| 166 | goto &autodie::import; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | 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 | local $! = 255; | ||
| 176 | return CORE::die(@_) if @_ == 1 and ref $_[0]; | ||
| 177 | |||
| 178 | my $error = join '', @_; | ||
| 179 | unless ($error =~ /\n$/) { | ||
| 180 | my ($file, $line) = (caller)[1,2]; | ||
| 181 | $error .= " at $file line $line.\n"; | ||
| 182 | } | ||
| 183 | |||
| 184 | local $! = 255; | ||
| 185 | return CORE::die($error); | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | # File::stat does not play nice in list context | ||
| 190 | sub stat { | ||
| 191 | return CORE::stat($_[0]) if wantarray; | ||
| 192 | return File::stat::stat(@_); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub lstat { | ||
| 196 | return CORE::lstat($_[0]) if wantarray; | ||
| 197 | return File::stat::lstat(@_); | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | sub capture(&;@) { | ||
| 202 | my($code, %opts) = @_; | ||
| 203 | |||
| 204 | # valid options | ||
| 205 | state $valid_options = { map { $_ => 1 } qw(merge tee) }; | ||
| 206 | |||
| 207 | for my $key (keys %opts) { | ||
| 208 | croak "$key is not a valid option to capture()" unless $valid_options->{$key}; | ||
| 209 | } | ||
| 210 | |||
| 211 | my $opts = join "/", sort { $a cmp $b } grep { $opts{$_} } keys %opts; | ||
| 212 | |||
| 213 | # Translate option combinations into Capture::Tiny functions | ||
| 214 | require Capture::Tiny; | ||
| 215 | 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 | my $func = $captures->{$opts}; | ||
| 223 | return $func->($code); | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | sub force_list_context(@) { | ||
| 228 | return @_; | ||
| 229 | } | ||
| 230 | |||
| 231 | 1; |