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