Neither of these can fail now that we dep on 5.8.1 (missed in 55087b99)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / _Util.pm
CommitLineData
b1dbf716 1package # hide from PAUSE
2 DBIx::Class::_Util;
3
4use warnings;
5use strict;
6
7use constant SPURIOUS_VERSION_CHECK_WARNINGS => ($] < 5.010 ? 1 : 0);
8
37873f78 9BEGIN {
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
8d73fcd4 20 BROKEN_GOTO => ($] < '5.008003') ? 1 : 0,
21
37873f78 22 HAS_ITHREADS => $Config{useithreads} ? 1 : 0,
23
24 # ::Runmode would only be loaded by DBICTest, which in turn implies t/
25 DBICTEST => eval { DBICTest::RunMode->is_author } ? 1 : 0,
26
27 # During 5.13 dev cycle HELEMs started to leak on copy
6cfb1d2f 28 # add an escape for these perls ON SMOKERS - a user will still get death
29 PEEPEENESS => ( eval { DBICTest::RunMode->is_smoker } && ($] >= 5.013005 and $] <= 5.013006) ),
37873f78 30
1b658919 31 SHUFFLE_UNORDERED_RESULTSETS => $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} ? 1 : 0,
32
37873f78 33 ASSERT_NO_INTERNAL_WANTARRAY => $ENV{DBIC_ASSERT_NO_INTERNAL_WANTARRAY} ? 1 : 0,
34
77c3a5dc 35 ASSERT_NO_INTERNAL_INDIRECT_CALLS => $ENV{DBIC_ASSERT_NO_INTERNAL_INDIRECT_CALLS} ? 1 : 0,
36
2fdeef65 37 STRESSTEST_UTF8_UPGRADE_GENERATED_COLLAPSER_SOURCE => $ENV{DBIC_STRESSTEST_UTF8_UPGRADE_GENERATED_COLLAPSER_SOURCE} ? 1 : 0,
38
f45dc928 39 STRESSTEST_COLUMN_INFO_UNAWARE_STORAGE => $ENV{DBIC_STRESSTEST_COLUMN_INFO_UNAWARE_STORAGE} ? 1 : 0,
40
37873f78 41 IV_SIZE => $Config{ivsize},
00882d2c 42
43 OS_NAME => $^O,
37873f78 44 };
45
46 if ($] < 5.009_005) {
47 require MRO::Compat;
48 constant->import( OLD_MRO => 1 );
49 }
50 else {
51 require mro;
52 constant->import( OLD_MRO => 0 );
53 }
54}
55
841efcb3 56# FIXME - this is not supposed to be here
57# Carp::Skip to the rescue soon
58use DBIx::Class::Carp '^DBIx::Class|^DBICTest';
59
d7d45bdc 60use B ();
841efcb3 61use Carp 'croak';
d7d45bdc 62use Storable 'nfreeze';
bf302897 63use Scalar::Util qw(weaken blessed reftype);
3705e3b2 64use List::Util qw(first);
b1dbf716 65
7f9a3f70 66# DO NOT edit away without talking to riba first, he will just put it back
67# BEGIN pre-Moo2 import block
68BEGIN {
69 my $initial_fatal_bits = (${^WARNING_BITS}||'') & $warnings::DeadBits{all};
cbd7f87a 70
7f9a3f70 71 local $ENV{PERL_STRICTURES_EXTRA} = 0;
cbd7f87a 72 # load all of these now, so that lazy-loading does not escape
73 # the current PERL_STRICTURES_EXTRA setting
74 require Sub::Quote;
75 require Sub::Defer;
76
77 Sub::Quote->import('quote_sub');
7f9a3f70 78 ${^WARNING_BITS} &= ( $initial_fatal_bits | ~ $warnings::DeadBits{all} );
79}
80sub qsub ($) { goto &quote_sub } # no point depping on new Moo just for this
81# END pre-Moo2 import block
82
b1dbf716 83use base 'Exporter';
3705e3b2 84our @EXPORT_OK = qw(
d634850b 85 sigwarn_silencer modver_gt_or_eq modver_gt_or_eq_and_lt
77c3a5dc 86 fail_on_internal_wantarray fail_on_internal_call
8433421f 87 refdesc refcount hrefaddr is_exception
b34d9331 88 quote_sub qsub perlstring serialize
facd0e8e 89 UNRESOLVABLE_CONDITION
3705e3b2 90);
052a832c 91
facd0e8e 92use constant UNRESOLVABLE_CONDITION => \ '1 = 0';
93
bf302897 94sub sigwarn_silencer ($) {
052a832c 95 my $pattern = shift;
96
97 croak "Expecting a regexp" if ref $pattern ne 'Regexp';
98
99 my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) };
100
101 return sub { &$orig_sig_warn unless $_[0] =~ $pattern };
102}
b1dbf716 103
01b25f12 104sub perlstring ($) { q{"}. quotemeta( shift ). q{"} };
105
8433421f 106sub hrefaddr ($) { sprintf '0x%x', &Scalar::Util::refaddr||0 }
107
108sub refdesc ($) {
109 croak "Expecting a reference" if ! length ref $_[0];
110
111 # be careful not to trigger stringification,
112 # reuse @_ as a scratch-pad
113 sprintf '%s%s(0x%x)',
114 ( defined( $_[1] = blessed $_[0]) ? "$_[1]=" : '' ),
115 reftype $_[0],
116 Scalar::Util::refaddr($_[0]),
117 ;
118}
bf302897 119
120sub refcount ($) {
dac7972a 121 croak "Expecting a reference" if ! length ref $_[0];
122
dac7972a 123 # No tempvars - must operate on $_[0], otherwise the pad
124 # will count as an extra ref
125 B::svref_2object($_[0])->REFCNT;
126}
127
b34d9331 128sub serialize ($) {
b34d9331 129 local $Storable::canonical = 1;
d7d45bdc 130 nfreeze($_[0]);
b34d9331 131}
132
841efcb3 133sub is_exception ($) {
134 my $e = $_[0];
135
a0414138 136 # this is not strictly correct - an eval setting $@ to undef
137 # is *not* the same as an eval setting $@ to ''
138 # but for the sake of simplicity assume the following for
139 # the time being
140 return 0 unless defined $e;
141
841efcb3 142 my ($not_blank, $suberror);
143 {
144 local $@;
145 eval {
146 $not_blank = ($e ne '') ? 1 : 0;
147 1;
148 } or $suberror = $@;
149 }
150
151 if (defined $suberror) {
152 if (length (my $class = blessed($e) )) {
153 carp_unique( sprintf(
9bea2000 154 'External exception class %s implements partial (broken) overloading '
155 . 'preventing its instances from being used in simple ($x eq $y) '
841efcb3 156 . 'comparisons. Given Perl\'s "globally cooperative" exception '
157 . 'handling this type of brokenness is extremely dangerous on '
158 . 'exception objects, as it may (and often does) result in silent '
159 . '"exception substitution". DBIx::Class tries to work around this '
160 . 'as much as possible, but other parts of your software stack may '
161 . 'not be even aware of this. Please submit a bugreport against the '
162 . 'distribution containing %s and in the meantime apply a fix similar '
163 . 'to the one shown at %s, in order to ensure your exception handling '
164 . 'is saner application-wide. What follows is the actual error text '
165 . "as generated by Perl itself:\n\n%s\n ",
9bea2000 166 $class,
841efcb3 167 $class,
168 'http://v.gd/DBIC_overload_tempfix/',
169 $suberror,
170 ));
171
172 # workaround, keeps spice flowing
173 $not_blank = ("$e" ne '') ? 1 : 0;
174 }
175 else {
176 # not blessed yet failed the 'ne'... this makes 0 sense...
177 # just throw further
178 die $suberror
179 }
180 }
181
182 return $not_blank;
183}
184
bf302897 185sub modver_gt_or_eq ($$) {
b1dbf716 186 my ($mod, $ver) = @_;
187
188 croak "Nonsensical module name supplied"
189 if ! defined $mod or ! length $mod;
190
191 croak "Nonsensical minimum version supplied"
192 if ! defined $ver or $ver =~ /[^0-9\.\_]/;
193
052a832c 194 local $SIG{__WARN__} = sigwarn_silencer( qr/\Qisn't numeric in subroutine entry/ )
195 if SPURIOUS_VERSION_CHECK_WARNINGS;
b1dbf716 196
56270bba 197 croak "$mod does not seem to provide a version (perhaps it never loaded)"
198 unless $mod->VERSION;
199
b1dbf716 200 local $@;
201 eval { $mod->VERSION($ver) } ? 1 : 0;
202}
203
d634850b 204sub modver_gt_or_eq_and_lt ($$$) {
205 my ($mod, $v_ge, $v_lt) = @_;
206
207 croak "Nonsensical maximum version supplied"
208 if ! defined $v_lt or $v_lt =~ /[^0-9\.\_]/;
209
210 return (
211 modver_gt_or_eq($mod, $v_ge)
212 and
213 ! modver_gt_or_eq($mod, $v_lt)
214 ) ? 1 : 0;
215}
216
a9da9b6a 217{
218 my $list_ctx_ok_stack_marker;
219
e89c7968 220 sub fail_on_internal_wantarray () {
a9da9b6a 221 return if $list_ctx_ok_stack_marker;
222
223 if (! defined wantarray) {
224 croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard');
225 }
226
227 my $cf = 1;
228 while ( ( (caller($cf+1))[3] || '' ) =~ / :: (?:
229
230 # these are public API parts that alter behavior on wantarray
231 search | search_related | slice | search_literal
232
233 |
234
235 # these are explicitly prefixed, since we only recognize them as valid
236 # escapes when they come from the guts of CDBICompat
237 CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all )
238
239 ) $/x ) {
240 $cf++;
241 }
242
e89c7968 243 my ($fr, $want, $argdesc);
244 {
245 package DB;
246 $fr = [ caller($cf) ];
247 $want = ( caller($cf-1) )[5];
248 $argdesc = ref $DB::args[0]
249 ? DBIx::Class::_Util::refdesc($DB::args[0])
250 : 'non '
251 ;
252 };
253
a9da9b6a 254 if (
e89c7968 255 $want and $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/
a9da9b6a 256 ) {
a9da9b6a 257 DBIx::Class::Exception->throw( sprintf (
e89c7968 258 "Improper use of %s instance in list context at %s line %d\n\n Stacktrace starts",
259 $argdesc, @{$fr}[1,2]
a9da9b6a 260 ), 'with_stacktrace');
261 }
262
263 my $mark = [];
264 weaken ( $list_ctx_ok_stack_marker = $mark );
265 $mark;
266 }
267}
268
77c3a5dc 269sub fail_on_internal_call {
270 my ($fr, $argdesc);
271 {
272 package DB;
273 $fr = [ caller(1) ];
274 $argdesc = ref $DB::args[0]
275 ? DBIx::Class::_Util::refdesc($DB::args[0])
276 : undef
277 ;
278 };
279
280 if (
281 $argdesc
282 and
283 $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/
284 and
285 $fr->[1] !~ /\b(?:CDBICompat|ResultSetProxy)\b/ # no point touching there
286 ) {
287 DBIx::Class::Exception->throw( sprintf (
288 "Illegal internal call of indirect proxy-method %s() with argument %s: examine the last lines of the proxy method deparse below to determine what to call directly instead at %s on line %d\n\n%s\n\n Stacktrace starts",
289 $fr->[3], $argdesc, @{$fr}[1,2], ( $fr->[6] || do {
290 require B::Deparse;
291 no strict 'refs';
292 B::Deparse->new->coderef2text(\&{$fr->[3]})
293 }),
294 ), 'with_stacktrace');
295 }
296}
297
b1dbf716 2981;