Tracking down reference error

Discussion in 'Perl Misc' started by lvirden@yahoo.com, Jan 13, 2004.

  1. Guest

    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: mailto: > <URL: http://www.purl.org/NET/lvirden/ >
    , Jan 13, 2004
    #1
    1. Advertising

  2. Ben Morrow Guest

    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.
    Ben Morrow, Jan 13, 2004
    #2
    1. Advertising

  3. 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--
    Sherm Pendley, Jan 13, 2004
    #3
  4. Jay Tilton Guest

    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 }
    Jay Tilton, Jan 13, 2004
    #4
  5. pkent Guest

    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::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

    --
    pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
    Remove the tea to reply
    pkent, Jan 14, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Amada
    Replies:
    4
    Views:
    536
    Ben Amada
    Aug 18, 2004
  2. =?Utf-8?B?bWFyZWFs?=

    Tracking down threads created within a ASP.NET app

    =?Utf-8?B?bWFyZWFs?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    409
    Scott Allen
    Mar 4, 2005
  3. MKoool

    Tracking down memory leaks?

    MKoool, Feb 12, 2006, in forum: Python
    Replies:
    15
    Views:
    515
    Christos Georgiou
    Feb 14, 2006
  4. Replies:
    9
    Views:
    2,846
  5. Knute Johnson

    Problem tracking down an error

    Knute Johnson, Dec 2, 2009, in forum: Java
    Replies:
    15
    Views:
    874
    Arne Vajhøj
    Jan 3, 2010
Loading...

Share This Page