piss off. -- mst
[gitmo/Moo.git] / lib / Moo / _Utils.pm
CommitLineData
b1eebd55 1package Moo::_Utils;
6c74d087 2
0fe2ad8c 3no warnings 'once'; # guard against -w
4
119014a7 5sub _getglob { \*{$_[0]} }
5ed7d68a 6sub _getstash { \%{"$_[0]::"} }
119014a7 7
575ba24c 8use constant lt_5_8_3 => ( $] < 5.008003 ) ? 1 : 0;
9use constant can_haz_subname => eval { require Sub::Name };
2215d4b9 10
6c74d087 11use strictures 1;
cf62c989 12use Module::Runtime qw(require_module);
6c74d087 13use base qw(Exporter);
3c739397 14use Moo::_mro;
6c74d087 15
3c739397 16our @EXPORT = qw(
17 _getglob _install_modifier _load_module _maybe_load_module
575ba24c 18 _get_linear_isa _getstash _install_coderef _name_coderef
19e0e749 19 _in_global_destruction
3c739397 20);
6c74d087 21
f57f1133 22sub _in_global_destruction ();
19e0e749 23
6c74d087 24sub _install_modifier {
6c74d087 25 my ($into, $type, $name, $code) = @_;
a165a07f 26
dccea57d 27 if (my $to_modify = $into->can($name)) { # CMM will throw for us if not
7568ba55 28 require Sub::Defer;
dccea57d 29 Sub::Defer::undefer_sub($to_modify);
30 }
a165a07f 31
6c74d087 32 Class::Method::Modifiers::install_modifier(@_);
33}
34
daa05b62 35our %MAYBE_LOADED;
36
fb5074f6 37sub _load_module {
fb5074f6 38 (my $proto = $_[0]) =~ s/::/\//g;
5ed7d68a 39 return 1 if $INC{"${proto}.pm"};
40 # can't just ->can('can') because a sub-package Foo::Bar::Baz
41 # creates a 'Baz::' key in Foo::Bar's symbol table
2a577e53 42 my $stash = _getstash($_[0])||{};
43 return 1 if grep +(!ref($_) and *$_{CODE}), values %$stash;
cf62c989 44 require_module($_[0]);
fb5074f6 45 return 1;
46}
47
daa05b62 48sub _maybe_load_module {
49 return $MAYBE_LOADED{$_[0]} if exists $MAYBE_LOADED{$_[0]};
50 (my $proto = $_[0]) =~ s/::/\//g;
59812c87 51 local $@;
daa05b62 52 if (eval { require "${proto}.pm"; 1 }) {
53 $MAYBE_LOADED{$_[0]} = 1;
54 } else {
55 if (exists $INC{"${proto}.pm"}) {
56 warn "$_[0] exists but failed to load with error: $@";
57 }
58 $MAYBE_LOADED{$_[0]} = 0;
59 }
60 return $MAYBE_LOADED{$_[0]};
61}
62
3c739397 63sub _get_linear_isa {
575ba24c 64 return mro::get_linear_isa($_[0]);
65}
66
67sub _install_coderef {
eda5c714 68 no warnings 'redefine';
575ba24c 69 *{_getglob($_[0])} = _name_coderef(@_);
70}
71
72sub _name_coderef {
67a95e30 73 shift if @_ > 2; # three args is (target, name, sub)
575ba24c 74 can_haz_subname ? Sub::Name::subname(@_) : $_[1];
3c739397 75}
76
59812c87 77sub STANDARD_DESTROY {
78 my $self = shift;
79
80 my $e = do {
81 local $?;
82 local $@;
83 eval {
19e0e749 84 $self->DEMOLISHALL(_in_global_destruction);
59812c87 85 };
86 $@;
87 };
88
89 no warnings 'misc';
90 die $e if $e; # rethrow
91}
92
f57f1133 93if (eval { require_module('Devel::GlobalDestruction') }) {
94 *_in_global_destruction = \&Devel::GlobalDestruction::in_global_destruction;
95} elsif (defined ${^GLOBAL_PHASE}) {
96 eval 'sub _in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }';
19e0e749 97} else {
98 eval <<'PP_IGD' or die $@;
99
100my ($in_global_destruction, $before_is_installed);
101
f57f1133 102sub _in_global_destruction () { $in_global_destruction }
19e0e749 103
104END {
105 # SpeedyCGI runs END blocks every cycle but somehow keeps object instances
106 # hence lying about it seems reasonable...ish
107 $in_global_destruction = 1 unless $CGI::SpeedyCGI::i_am_speedy;
108}
109
110# threads do not execute the global ENDs (it would be stupid). However
111# one can register a new END via simple string eval within a thread, and
112# achieve the same result. A logical place to do this would be CLONE, which
113# is claimed to run in the context of the new thread. However this does
114# not really seem to be the case - any END evaled in a CLONE is ignored :(
115# Hence blatantly hooking threads::create
116
117if ($INC{'threads.pm'}) {
118 my $orig_create = threads->can('create');
119 no warnings 'redefine';
120 *threads::create = sub {
121 { local $@; eval 'END { $in_global_destruction = 1 }' };
122 goto $orig_create;
123 };
124 $before_is_installed = 1;
125}
126
127# just in case threads got loaded after us (silly)
128sub CLONE {
129 unless ($before_is_installed) {
130 require Carp;
131 Carp::croak("You must load the 'threads' module before @{[ __PACKAGE__ ]}");
132 }
133}
134
1351; # keep eval happy
136
137PP_IGD
138
139}
140
6c74d087 1411;