Outaspace has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a problem with a debugger that I have written:

In the DB::sub routine I want to print the arguments of a method. This works fine for most data, but is crashing (recursion called over and over again) when the "" operator is overloaded to call a sub, since this calls DB::sub again and from here on forever. Is there a way to prevent this? I need only a way to check this and continue if DB::sub is not called by an overloaded "". And then there is the question what to print instead of the "" that would call the sub.

Andre

Replies are listed 'Best First'.
Re: overload and the perl debugger (StrVal)
by tye (Sage) on Aug 27, 2007 at 14:43 UTC

    overload::StrVal($var)

    Update: I figured that'd be XS code and so wouldn't trigger DB::sub, but it is actually this rather simple Perl code (from overload.pm):

    sub AddrRef { my $package = ref $_[0]; return "$_[0]" unless $package; require Scalar::Util; my $class = Scalar::Util::blessed($_[0]); my $class_prefix = defined($class) ? "$class=" : ""; my $type = Scalar::Util::reftype($_[0]); my $addr = Scalar::Util::refaddr($_[0]); return sprintf("$class_prefix$type(0x%x)", $addr); }

    So you can just use that same technique yourself. Yes, reftype() and refaddr() are XS unless Scalar::Util fails to load its XS component, in which case it uses the Perl replacements found in Util.pm, which you could also copy (they rebless to avoid overload magic).

    - tye        

      There's little sense in copying out of overload.pm - that module is core and is always available.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        For one, "core" does not actually mean "always available". For another, copying what boils down to two lines of code isn't something I sweat much about.

        But, perhaps most importantly, if one can't even use "$x" because it calls a subroutine, then I'm not sure one can, um, call a subroutine. I haven't tried to write a debugger but perldebguts says:

        When execution of the program reaches a subroutine call, a call to &DB::sub(args) is made instead, with $DB::sub holding the name of the called subroutine. (This doesn't happen if the subroutine was compiled in the DB package.)

        (emphasis added). So I think you might need to recompile those couple of lines of code into a different package in this case.

        And I wouldn't be surprised if understanding the few key concepts presented in those several lines of code from the 3 subroutines wouldn't lead to a better way to integrate the concepts into the OP's code then would be possible by just calling those routines.

        - tye        

Re: overlaod and the perl debugger
by TOD (Friar) on Aug 27, 2007 at 13:38 UTC
    it's just a guess, and maybe i'm awfully wrong. but what if you declare a package global flag and test against its value?
    --------------------------------
    masses are the opiate for religion.
      I didn't understand exactly when I should set this flag and where, can you be a little more specific. The problem is that I didn't found a way yet to tell if a sub is called recursively from overloaded "".