Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Tracking down reference error

Reply
Thread Tools

Tracking down reference error

 
 
lvirden@yahoo.com
Guest
Posts: n/a
 
      01-13-2004

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?

--
<URL: http://wiki.tcl.tk/ > In God we trust.
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.
<URL: private.php?do=newpm&u= > <URL: http://www.purl.org/NET/lvirden/ >
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      01-13-2004

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?


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

--
We do not stop playing because we grow old;
we grow old because we stop playing.

 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      01-13-2004
On Tue, 13 Jan 2004 17:39:02 +0000, lvirden wrote:

> 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--
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      01-13-2004
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 }

 
Reply With Quote
 
pkent
Guest
Posts: n/a
 
      01-14-2004
In article <bu1afm$pgj$>, wrote:

> 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:umper 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:umper:umper($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

--
pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem tracking down an error Knute Johnson Java 15 01-03-2010 02:38 AM
Tracking down undefined reference errors. walkeraj@gmail.com C++ 9 11-16-2007 07:36 PM
Tracking down a client's port Matt White Cisco 4 08-25-2005 04:34 PM
Tracking Someone Tracking Me Edw. Peach Computer Security 4 07-07-2005 05:50 PM
Tracking down error occurring with "Request" object Ben Amada ASP .Net 4 08-18-2004 02:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57