File: _Utils.pm

package info (click to toggle)
libmoo-perl 2.005005-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 840 kB
  • sloc: perl: 2,506; makefile: 2
file content (288 lines) | stat: -rw-r--r-- 6,528 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package Moo::_Utils;
use strict;
use warnings;

{
  no strict 'refs';
  no warnings 'once';
  sub _getglob { \*{$_[0]} }
  sub _getstash { \%{"$_[0]::"} }
}

BEGIN {
  my ($su, $sn);
  $su = $INC{'Sub/Util.pm'} && defined &Sub::Util::set_subname
    or $sn = $INC{'Sub/Name.pm'}
    or $su = eval { require Sub::Util; } && defined &Sub::Util::set_subname
    or $sn = eval { require Sub::Name; };

  *_subname = $su ? \&Sub::Util::set_subname
            : $sn ? \&Sub::Name::subname
            : sub { $_[1] };
  *_CAN_SUBNAME = ($su || $sn) ? sub(){1} : sub(){0};

  *_WORK_AROUND_BROKEN_MODULE_STATE = "$]" < 5.009 ? sub(){1} : sub(){0};
  *_WORK_AROUND_HINT_LEAKAGE
    = "$]" < 5.011 && !("$]" >= 5.009004 && "$]" < 5.010001)
      ? sub(){1} : sub(){0};

  my $module_name_rx = qr/\A(?!\d)\w+(?:::\w+)*\z/;
  *_module_name_rx = sub(){$module_name_rx};
}

use Exporter ();
BEGIN { *import = \&Exporter::import }
use Config ();
use Scalar::Util qw(weaken);
use Carp qw(croak);

# this should be empty, but some CPAN modules expect these
our @EXPORT = qw(
  _install_coderef
  _load_module
);

our @EXPORT_OK = qw(
  _check_tracked
  _getglob
  _getstash
  _install_coderef
  _install_modifier
  _install_tracked
  _load_module
  _maybe_load_module
  _module_name_rx
  _name_coderef
  _set_loaded
  _unimport_coderefs
  _linear_isa
  _in_global_destruction
  _in_global_destruction_code
);

my %EXPORTS;

sub _install_modifier {
  my $target = $_[0];
  my $type = $_[1];
  my $code = $_[-1];
  my @names = @_[2 .. $#_ - 1];

  @names = @{ $names[0] }
    if ref($names[0]) eq 'ARRAY';

  my @tracked = _check_tracked($target, \@names);

  if ($INC{'Sub/Defer.pm'}) {
    for my $name (@names) {
      # CMM will throw for us if it doesn't exist
      if (my $to_modify = $target->can($name)) {
        Sub::Defer::undefer_sub($to_modify);
      }
    }
  }

  require Class::Method::Modifiers;
  Class::Method::Modifiers::install_modifier(@_);

  if (@tracked) {
    my $exports = $EXPORTS{$target};
    weaken($exports->{$_} = $target->can($_))
      for @tracked;
  }

  return;
}

sub _install_tracked {
  my ($target, $name, $code) = @_;
  my $from = caller;
  weaken($EXPORTS{$target}{$name} = $code);
  _install_coderef("${target}::${name}", "${from}::${name}", $code);
}

sub Moo::_Util::__GUARD__::DESTROY {
  delete $INC{$_[0]->[0]} if @{$_[0]};
}

sub _require {
  my ($file) = @_;
  my $guard = _WORK_AROUND_BROKEN_MODULE_STATE
    && bless([ $file ], 'Moo::_Util::__GUARD__');
  local %^H if _WORK_AROUND_HINT_LEAKAGE;
  if (!eval { require $file; 1 }) {
    my $e = $@ || "Can't locate $file";
    my $me = __FILE__;
    $e =~ s{ at \Q$me\E line \d+\.\n\z}{};
    return $e;
  }
  pop @$guard if _WORK_AROUND_BROKEN_MODULE_STATE;
  return undef;
}

sub _load_module {
  my ($module) = @_;
  croak qq{"$module" is not a module name!}
    unless $module =~ _module_name_rx;
  (my $file = "$module.pm") =~ s{::}{/}g;
  return 1
    if $INC{$file};

  my $e = _require $file;
  return 1
    if !defined $e;

  croak $e
    if $e !~ /\ACan't locate \Q$file\E /;

  # can't just ->can('can') because a sub-package Foo::Bar::Baz
  # creates a 'Baz::' key in Foo::Bar's symbol table
  my $stash = _getstash($module)||{};
  no strict 'refs';
  return 1 if grep +exists &{"${module}::$_"}, grep !/::\z/, keys %$stash;
  return 1
    if $INC{"Moose.pm"} && Class::MOP::class_of($module)
    or Mouse::Util->can('find_meta') && Mouse::Util::find_meta($module);

  croak $e;
}

our %MAYBE_LOADED;
sub _maybe_load_module {
  my $module = $_[0];
  return $MAYBE_LOADED{$module}
    if exists $MAYBE_LOADED{$module};
  (my $file = "$module.pm") =~ s{::}{/}g;

  my $e = _require $file;
  if (!defined $e) {
    return $MAYBE_LOADED{$module} = 1;
  }
  elsif ($e !~ /\ACan't locate \Q$file\E /) {
    warn "$module exists but failed to load with error: $e";
  }
  return $MAYBE_LOADED{$module} = 0;
}

BEGIN {
  # optimize for newer perls
  require mro
    if "$]" >= 5.009_005;

  if (defined &mro::get_linear_isa) {
    *_linear_isa = \&mro::get_linear_isa;
  }
  else {
    my $e;
    {
      local $@;
      eval <<'END_CODE' or $e = $@;
sub _linear_isa($;$) {
  my $class = shift;
  my $type = shift || exists $Class::C3::MRO{$class} ? 'c3' : 'dfs';

  if ($type eq 'c3') {
    require Class::C3;
    return [Class::C3::calculateMRO($class)];
  }

  my @check = ($class);
  my @lin;

  my %found;
  while (defined(my $check = shift @check)) {
    push @lin, $check;
    no strict 'refs';
    unshift @check, grep !$found{$_}++, @{"$check\::ISA"};
  }

  return \@lin;
}

1;
END_CODE
    }
    die $e if defined $e;
  }
}

BEGIN {
  my $gd_code
    = "$]" >= 5.014
      ? q[${^GLOBAL_PHASE} eq 'DESTRUCT']
    : _maybe_load_module('Devel::GlobalDestruction::XS')
      ? 'Devel::GlobalDestruction::XS::in_global_destruction()'
      : 'do { use B (); ${B::main_cv()} == 0 }';
  *_in_global_destruction_code = sub () { $gd_code };
  eval "sub _in_global_destruction () { $gd_code }; 1"
    or die $@;
}

sub _set_loaded {
  (my $file = "$_[0].pm") =~ s{::}{/}g;
  $INC{$file} ||= $_[1];
}

sub _install_coderef {
  my ($glob, $code) = (_getglob($_[0]), _name_coderef(@_));
  no warnings 'redefine';
  if (*{$glob}{CODE}) {
    *{$glob} = $code;
  }
  # perl will sometimes warn about mismatched prototypes coming from the
  # inheritance cache, so disable them if we aren't redefining a sub
  else {
    no warnings 'prototype';
    *{$glob} = $code;
  }
}

sub _name_coderef {
  shift if @_ > 2; # three args is (target, name, sub)
  _CAN_SUBNAME ? _subname(@_) : $_[1];
}

sub _check_tracked {
  my ($target, $names) = @_;
  my $stash = _getstash($target);
  my $exports = $EXPORTS{$target}
    or return;

  $names = [keys %$exports]
    if !$names;
  my %rev =
    map +($exports->{$_} => $_),
    grep defined $exports->{$_},
    keys %$exports;

  return
    grep {
      my $g = $stash->{$_};
      $g && defined &$g && exists $rev{\&$g};
    }
    @$names;
}

sub _unimport_coderefs {
  my ($target) = @_;

  my $stash = _getstash($target);
  my @exports = _check_tracked($target);

  foreach my $name (@exports) {
    my $old = delete $stash->{$name};
    my $full_name = join('::',$target,$name);
    # Copy everything except the code slot back into place (e.g. $has)
    foreach my $type (qw(SCALAR HASH ARRAY IO)) {
      next unless defined(*{$old}{$type});
      no strict 'refs';
      *$full_name = *{$old}{$type};
    }
  }
}

if ($Config::Config{useithreads}) {
  require Moo::HandleMoose::_TypeMap;
}

1;