1 package # hide from pause
7 # load Carp early to prevent tickling of the ::Internal stash being
8 # interpreted as "Carp is already loaded" by some braindead loader
10 $Carp::Internal{ (__PACKAGE__) }++;
15 # There are cases out there where a user provides a can() that won't actually
16 # work as perl intends it. Since this is a reporting library, we *have* to be
17 # extra paranoid ( e.g. https://rt.cpan.org/Ticket/Display.html?id=90715 )
20 local $SIG{__DIE__} if $SIG{__DIE__};
24 $cref = $_[0]->can( $_[1] );
26 # in case the can() isn't an actual UNIVERSAL::can()
27 die "Return value of $_[0]" . "->can(q($_[1])) is true yet not a code reference...\n"
28 if $cref and Scalar::Util::reftype($cref) ne 'CODE';
34 # can not use DBIC::_Util::emit_loud_diag - it uses us internally
36 "\n$0: !!! INTERNAL PANIC !!!\nClass '%s' implements or inherits a broken can() - PLEASE FIX ASAP!: %s\n\n",
37 ( length ref $_[0] ? ref $_[0] : $_[0] ),
46 my ($skip_pattern, $class) = @_;
48 my $skip_class_data = $class->_skip_namespace_frames
49 if ($class and __safe_can($class, '_skip_namespace_frames') );
51 $skip_pattern = qr/$skip_pattern|$skip_class_data/
54 my $fr_num = 1; # skip us and the calling carp*
57 while (@f = CORE::caller($fr_num++)) {
60 ( $f[3] eq '(eval)' or $f[3] =~ /::__ANON__$/ );
63 $f[3] =~ /^ (.+) :: ([^\:]+) $/x
67 #############################
68 # Need a way to parameterize this for Carp::Skip
69 $1 !~ /^(?: DBIx::Class::Storage::BlockRunner | Context::Preserve | Try::Tiny | Class::Accessor::Grouped | Class::C3::Componentised | Module::Runtime | Sub::Uplevel )$/x
71 $2 !~ /^(?: throw_exception | carp | carp_unique | carp_once | dbh_do | txn_do | with_deferred_fk_checks | __delicate_rollback | dbic_internal_try )$/x
72 #############################
76 __safe_can( $f[0], '_skip_namespace_frames' )
78 my $extra_skip = $f[0]->_skip_namespace_frames
80 $skip_pattern = qr/$skip_pattern|$extra_skip/;
83 last if $f[0] !~ $skip_pattern;
86 my $site = @f # if empty - nothing matched - full stack
87 ? "at $f[1] line $f[2]"
94 # cargo-cult from Carp::Clan
95 ! defined $origin ? ''
96 : $origin =~ /::/ ? "$origin(): "
103 my ($ln, @warn) = @_;
104 @warn = "Warning: something's wrong" unless @warn;
106 # back-compat with Carp::Clan - a warning ending with \n does
107 # not include caller info
110 $warn[-1] =~ /\n$/ ? '' : " $ln\n"
115 my (undef, $skip_pattern) = @_;
118 $skip_pattern = $skip_pattern
119 ? qr/ ^ $into $ | $skip_pattern /x
125 *{"${into}::carp"} = sub {
127 __find_caller($skip_pattern, $into),
133 *{"${into}::carp_once"} = sub {
134 return if $fired->{$_[0]};
138 __find_caller($skip_pattern, $into),
144 *{"${into}::carp_unique"} = sub {
145 my ($ln, $calling) = __find_caller($skip_pattern, $into);
146 my $msg = join ('', $calling, @_);
148 # unique carping with a hidden caller makes no sense
151 return if $seen->{$ln}{$msg};
152 $seen->{$ln}{$msg} = 1;
162 die (__PACKAGE__ . " does not implement unimport yet\n");
171 DBIx::Class::Carp - Provides advanced Carp::Clan-like warning functions for DBIx::Class internals
175 Documentation is lacking on purpose - this an experiment not yet fit for
176 mass consumption. If you use this do not count on any kind of stability,
177 in fact don't even count on this module's continuing existence (it has
178 been noindexed for a reason).
180 In addition to the classic interface:
182 use DBIx::Class::Carp '^DBIx::Class'
184 this module also supports a class-data based way to specify the exclusion
185 regex. A message is only carped from a callsite that matches neither the
186 closed over string, nor the value of L</_skip_namespace_frames> as declared
187 on any callframe already skipped due to the same mechanism. This is to ensure
188 that intermediate callsites can declare their own additional skip-namespaces.
190 =head1 CLASS ATTRIBUTES
192 =head2 _skip_namespace_frames
194 A classdata attribute holding the stringified regex matching callsites that
195 should be skipped by the carp methods below. An empty string C<q{}> is treated
196 like no setting/C<undef> (the distinction is necessary due to semantics of the
197 class data accessors provided by L<Class::Accessor::Grouped>)
199 =head1 EXPORTED FUNCTIONS
201 This module export the following 3 functions. Only warning related C<carp*>
202 is being handled here, for C<croak>-ing you must use
203 L<DBIx::Class::Schema/throw_exception> or L<DBIx::Class::Exception>.
207 Carps message with the file/line of the first callsite not matching
208 L</_skip_namespace_frames> nor the closed-over arguments to
209 C<use DBIx::Class::Carp>.
213 Like L</carp> but warns once for every distinct callsite (subject to the
214 same ruleset as L</carp>).
218 Like L</carp> but warns only once for the life of the perl interpreter
219 (regardless of callsite).
221 =head1 FURTHER QUESTIONS?
223 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
225 =head1 COPYRIGHT AND LICENSE
227 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
228 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
229 redistribute it and/or modify it under the same terms as the
230 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.