wrote:
: I'm trying to track down a perl bug, and could use some debugging tips
: from the experts.
:
: The error I am getting says:
:
: Not a SCALAR reference at Base.pm line 1750.
:
: And that particular line is about the first thing in the function,
: and says:
:
: return undef unless defined $$ref;
:
: Now, certainly I can add some code in before this line to print
: $ref . But I'm also going to to figure out who is calling the function
: with the errant piece of code.
:
: Is there a way that I can test to see if $ref is a scalar reference,
: display the bad reference value, and then invoke the caller function to
: get the stack trace, without actually raising a perl error?
Ben has mentioned Carp::confess(), which shows a call stack trace before
dying. You might also find Carp::cluck() useful, which does the same
thing without dying.
use Carp 'cluck';
unless( ref($ref) eq 'SCALAR' ) {
cluck( "'$ref' is not a scalar reference." );
return undef; # questionable practice
}
return undef unless defined $$ref; # ditto
But writing "return undef" instead of a simple "return" is usually the
Wrong Thing. Its correctness depends on whether the caller anticipates
receiving one value or nothing when the sub is called in list context.
print "foo() failed in scalar context.\n" unless $test = foo();
print "foo() failed in list context.\n" unless @test = foo();
print "bar() failed in scalar context.\n" unless $test = bar();
print "bar() failed in list context.\n" unless @test = bar();
sub foo { return }
sub bar { return undef }