X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2F_Util.pm;h=e6cf2a9bee1d421fade0f18a178ba9aa488b81d0;hb=a9da9b6a57a597bc7e52c7e7ad7221eaa7ee6d14;hp=d4760bcb2e048ada16a5c8b1f8290ef0ffafc012;hpb=1439bf153439341cdba3ed1e53141ed5a0575705;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/_Util.pm b/lib/DBIx/Class/_Util.pm index d4760bc..e6cf2a9 100644 --- a/lib/DBIx/Class/_Util.pm +++ b/lib/DBIx/Class/_Util.pm @@ -7,9 +7,10 @@ use strict; use constant SPURIOUS_VERSION_CHECK_WARNINGS => ($] < 5.010 ? 1 : 0); use Carp; +use Scalar::Util qw(refaddr weaken); use base 'Exporter'; -our @EXPORT_OK = qw(modver_gt_or_eq); +our @EXPORT_OK = qw(modver_gt_or_eq fail_on_internal_wantarray); sub modver_gt_or_eq { my ($mod, $ver) = @_; @@ -31,4 +32,47 @@ sub modver_gt_or_eq { eval { $mod->VERSION($ver) } ? 1 : 0; } +{ + my $list_ctx_ok_stack_marker; + + sub fail_on_internal_wantarray { + return if $list_ctx_ok_stack_marker; + + if (! defined wantarray) { + croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard'); + } + + my $cf = 1; + while ( ( (caller($cf+1))[3] || '' ) =~ / :: (?: + + # these are public API parts that alter behavior on wantarray + search | search_related | slice | search_literal + + | + + # these are explicitly prefixed, since we only recognize them as valid + # escapes when they come from the guts of CDBICompat + CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all ) + + ) $/x ) { + $cf++; + } + + if ( + (caller($cf))[0] =~ /^(?:DBIx::Class|DBICx::)/ + ) { + my $obj = shift; + + DBIx::Class::Exception->throw( sprintf ( + "Improper use of %s(0x%x) instance in list context at %s line %d\n\n\tStacktrace starts", + ref($obj), refaddr($obj), (caller($cf))[1,2] + ), 'with_stacktrace'); + } + + my $mark = []; + weaken ( $list_ctx_ok_stack_marker = $mark ); + $mark; + } +} + 1;