Commit | Line | Data |
b1dbf716 |
1 | package # hide from PAUSE |
2 | DBIx::Class::_Util; |
3 | |
4 | use warnings; |
5 | use strict; |
6 | |
7 | use constant SPURIOUS_VERSION_CHECK_WARNINGS => ($] < 5.010 ? 1 : 0); |
8 | |
37873f78 |
9 | BEGIN { |
10 | package # hide from pause |
11 | DBIx::Class::_ENV_; |
12 | |
13 | use Config; |
14 | |
15 | use constant { |
16 | |
17 | # but of course |
18 | BROKEN_FORK => ($^O eq 'MSWin32') ? 1 : 0, |
19 | |
20 | HAS_ITHREADS => $Config{useithreads} ? 1 : 0, |
21 | |
22 | # ::Runmode would only be loaded by DBICTest, which in turn implies t/ |
23 | DBICTEST => eval { DBICTest::RunMode->is_author } ? 1 : 0, |
24 | |
25 | # During 5.13 dev cycle HELEMs started to leak on copy |
26 | PEEPEENESS => |
27 | # request for all tests would force "non-leaky" illusion and vice-versa |
28 | defined $ENV{DBICTEST_ALL_LEAKS} ? !$ENV{DBICTEST_ALL_LEAKS} |
29 | # otherwise confess that this perl is busted ONLY on smokers |
30 | : eval { DBICTest::RunMode->is_smoker } && ($] >= 5.013005 and $] <= 5.013006) ? 1 |
31 | # otherwise we are good |
32 | : 0 |
33 | , |
34 | |
35 | ASSERT_NO_INTERNAL_WANTARRAY => $ENV{DBIC_ASSERT_NO_INTERNAL_WANTARRAY} ? 1 : 0, |
36 | |
37 | IV_SIZE => $Config{ivsize}, |
00882d2c |
38 | |
39 | OS_NAME => $^O, |
37873f78 |
40 | }; |
41 | |
42 | if ($] < 5.009_005) { |
43 | require MRO::Compat; |
44 | constant->import( OLD_MRO => 1 ); |
45 | } |
46 | else { |
47 | require mro; |
48 | constant->import( OLD_MRO => 0 ); |
49 | } |
50 | } |
51 | |
841efcb3 |
52 | # FIXME - this is not supposed to be here |
53 | # Carp::Skip to the rescue soon |
54 | use DBIx::Class::Carp '^DBIx::Class|^DBICTest'; |
55 | |
56 | use Carp 'croak'; |
bf302897 |
57 | use Scalar::Util qw(weaken blessed reftype); |
3705e3b2 |
58 | use List::Util qw(first); |
59 | use overload (); |
b1dbf716 |
60 | |
61 | use base 'Exporter'; |
3705e3b2 |
62 | our @EXPORT_OK = qw( |
63 | sigwarn_silencer modver_gt_or_eq fail_on_internal_wantarray |
64 | refcount hrefaddr is_exception |
65 | is_plain_value is_literal_value |
66 | ); |
052a832c |
67 | |
bf302897 |
68 | sub sigwarn_silencer ($) { |
052a832c |
69 | my $pattern = shift; |
70 | |
71 | croak "Expecting a regexp" if ref $pattern ne 'Regexp'; |
72 | |
73 | my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) }; |
74 | |
75 | return sub { &$orig_sig_warn unless $_[0] =~ $pattern }; |
76 | } |
b1dbf716 |
77 | |
bf302897 |
78 | sub hrefaddr ($) { sprintf '0x%x', &Scalar::Util::refaddr } |
79 | |
80 | sub refcount ($) { |
dac7972a |
81 | croak "Expecting a reference" if ! length ref $_[0]; |
82 | |
83 | require B; |
84 | # No tempvars - must operate on $_[0], otherwise the pad |
85 | # will count as an extra ref |
86 | B::svref_2object($_[0])->REFCNT; |
87 | } |
88 | |
841efcb3 |
89 | sub is_exception ($) { |
90 | my $e = $_[0]; |
91 | |
a0414138 |
92 | # this is not strictly correct - an eval setting $@ to undef |
93 | # is *not* the same as an eval setting $@ to '' |
94 | # but for the sake of simplicity assume the following for |
95 | # the time being |
96 | return 0 unless defined $e; |
97 | |
841efcb3 |
98 | my ($not_blank, $suberror); |
99 | { |
100 | local $@; |
101 | eval { |
102 | $not_blank = ($e ne '') ? 1 : 0; |
103 | 1; |
104 | } or $suberror = $@; |
105 | } |
106 | |
107 | if (defined $suberror) { |
108 | if (length (my $class = blessed($e) )) { |
109 | carp_unique( sprintf( |
bf302897 |
110 | 'External exception object %s=%s(%s) implements partial (broken) ' |
841efcb3 |
111 | . 'overloading preventing it from being used in simple ($x eq $y) ' |
112 | . 'comparisons. Given Perl\'s "globally cooperative" exception ' |
113 | . 'handling this type of brokenness is extremely dangerous on ' |
114 | . 'exception objects, as it may (and often does) result in silent ' |
115 | . '"exception substitution". DBIx::Class tries to work around this ' |
116 | . 'as much as possible, but other parts of your software stack may ' |
117 | . 'not be even aware of this. Please submit a bugreport against the ' |
118 | . 'distribution containing %s and in the meantime apply a fix similar ' |
119 | . 'to the one shown at %s, in order to ensure your exception handling ' |
120 | . 'is saner application-wide. What follows is the actual error text ' |
121 | . "as generated by Perl itself:\n\n%s\n ", |
122 | $class, |
123 | reftype $e, |
bf302897 |
124 | hrefaddr $e, |
841efcb3 |
125 | $class, |
126 | 'http://v.gd/DBIC_overload_tempfix/', |
127 | $suberror, |
128 | )); |
129 | |
130 | # workaround, keeps spice flowing |
131 | $not_blank = ("$e" ne '') ? 1 : 0; |
132 | } |
133 | else { |
134 | # not blessed yet failed the 'ne'... this makes 0 sense... |
135 | # just throw further |
136 | die $suberror |
137 | } |
138 | } |
139 | |
140 | return $not_blank; |
141 | } |
142 | |
bf302897 |
143 | sub modver_gt_or_eq ($$) { |
b1dbf716 |
144 | my ($mod, $ver) = @_; |
145 | |
146 | croak "Nonsensical module name supplied" |
147 | if ! defined $mod or ! length $mod; |
148 | |
149 | croak "Nonsensical minimum version supplied" |
150 | if ! defined $ver or $ver =~ /[^0-9\.\_]/; |
151 | |
052a832c |
152 | local $SIG{__WARN__} = sigwarn_silencer( qr/\Qisn't numeric in subroutine entry/ ) |
153 | if SPURIOUS_VERSION_CHECK_WARNINGS; |
b1dbf716 |
154 | |
56270bba |
155 | croak "$mod does not seem to provide a version (perhaps it never loaded)" |
156 | unless $mod->VERSION; |
157 | |
b1dbf716 |
158 | local $@; |
159 | eval { $mod->VERSION($ver) } ? 1 : 0; |
160 | } |
161 | |
3705e3b2 |
162 | sub is_literal_value ($) { |
163 | ( |
164 | ref $_[0] eq 'SCALAR' |
165 | or |
166 | ( ref $_[0] eq 'REF' and ref ${$_[0]} eq 'ARRAY' ) |
167 | ) ? 1 : 0; |
168 | } |
169 | |
170 | # FIXME XSify - this can be done so much more efficiently |
171 | sub is_plain_value ($) { |
172 | no strict 'refs'; |
173 | ( |
174 | # plain scalar |
175 | (! length ref $_[0]) |
176 | or |
177 | ( |
178 | blessed $_[0] |
179 | and |
180 | # deliberately not using Devel::OverloadInfo - the checks we are |
181 | # intersted in are much more limited than the fullblown thing, and |
182 | # this is a relatively hot piece of code |
183 | ( |
184 | # either has stringification which DBI prefers out of the box |
185 | #first { *{$_ . '::(""'}{CODE} } @{ mro::get_linear_isa( ref $_[0] ) } |
186 | overload::Method($_[0], '""') |
187 | or |
188 | # has nummification and fallback is *not* disabled |
189 | ( |
190 | $_[1] = first { *{"${_}::(0+"}{CODE} } @{ mro::get_linear_isa( ref $_[0] ) } |
191 | and |
192 | ( ! defined ${"$_[1]::()"} or ${"$_[1]::()"} ) |
193 | ) |
194 | ) |
195 | ) |
196 | ) ? 1 : 0; |
197 | } |
198 | |
a9da9b6a |
199 | { |
200 | my $list_ctx_ok_stack_marker; |
201 | |
202 | sub fail_on_internal_wantarray { |
203 | return if $list_ctx_ok_stack_marker; |
204 | |
205 | if (! defined wantarray) { |
206 | croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard'); |
207 | } |
208 | |
209 | my $cf = 1; |
210 | while ( ( (caller($cf+1))[3] || '' ) =~ / :: (?: |
211 | |
212 | # these are public API parts that alter behavior on wantarray |
213 | search | search_related | slice | search_literal |
214 | |
215 | | |
216 | |
217 | # these are explicitly prefixed, since we only recognize them as valid |
218 | # escapes when they come from the guts of CDBICompat |
219 | CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all ) |
220 | |
221 | ) $/x ) { |
222 | $cf++; |
223 | } |
224 | |
225 | if ( |
226 | (caller($cf))[0] =~ /^(?:DBIx::Class|DBICx::)/ |
227 | ) { |
228 | my $obj = shift; |
229 | |
230 | DBIx::Class::Exception->throw( sprintf ( |
bf302897 |
231 | "Improper use of %s(%s) instance in list context at %s line %d\n\n\tStacktrace starts", |
232 | ref($obj), hrefaddr($obj), (caller($cf))[1,2] |
a9da9b6a |
233 | ), 'with_stacktrace'); |
234 | } |
235 | |
236 | my $mark = []; |
237 | weaken ( $list_ctx_ok_stack_marker = $mark ); |
238 | $mark; |
239 | } |
240 | } |
241 | |
b1dbf716 |
242 | 1; |