Tracking down reference error

L

lvirden

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?
 
B

Ben Morrow

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?

If you add

BEGIN {
require Carp;
$SIG{__DIE__} = sub { Carp::confess $_[0] };
}

to the top of your program, any 'die' messages will be replaced with a
stack trace.

Ben
 
S

Sherm Pendley

Is there a way that I can test to see if $ref is a scalar reference,

perldoc -f ref

For example:

if (!defined $ref) {
# undef
} elsif (ref($ref) eq 'SCALAR') {
# scalar reference
} elsif (ref($ref)) {
# some other kind of reference
} else {
# Defined but not a reference
}

sherm--
 
J

Jay Tilton

(e-mail address removed) 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 }
 
P

pkent

Not a SCALAR reference at Base.pm line 1750. ....
return undef unless defined $$ref;

Now, certainly I can add some code in before this line to print
$ref .

You can use the ref() function to find out what kind of thing $ref is,
and something like Data::Dumper to print $ref out.

Usually I check that a thing is at least defined before dereferencing
it, in cases where I cannot be reasonably sure that it would be defined.

But I'm also going to to figure out who is calling the function
with the errant piece of code.

Sounds like the caller() function - perldoc -f caller

Also look at Carp - especially the clucks() and confess() routines to
get a stacktrace. This might be even more useful because it doesn't just
tell you the caller - it tells you the whole stack of callers.

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?

basically:
# is ref($ref) equal to 'SCALAR'?
# if it is then that's great - deref it and use it
# elsif if it's not defined, Carp::cluck('$ref is not defined!')
# elsif it's not a reference - 'not ref($ref)' - Carp::cluck("it has
scalar value $ref")
# else, print Data::Dumper::Dumper($ref)
# Carp::cluck("It's a reference of type ".ref($ref))

# that's just the steps to take, not actual code :)

Carp::cluck() just emits text to standard error, but doesn't throw an
exception.

P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top