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

Is there a penalty for using the DB package. As in the example of the caller function taken from Carp.
my @a; while ( do { { package DB; @a = caller($i++) } } ) { my @args = @DB::args; # odd results for some data arguments }

It seems that this would give a penalty since there is now a copy of all arguments in @DB::args - unless @DB::args are just special pointers to the actual data with magic lookups upon access. Anyway - anybody have insight?

my @a=qw(random brilliant braindead); print $a[rand(@a)];

Replies are listed 'Best First'.
Re: package DB penalty?
by hv (Prior) on May 15, 2003 at 00:54 UTC

    The @DB::args array is populated by copying the pointers from the actual @_ array, so the cost is a pretty negligible fraction of a caller() invocation - grep for the reference to PL_dbargs in pp_ctl.c to find the 20-odd line block of code involved (within pp_caller()).

    I should stress that this is not intended for everyday use, and would suspect that overly imaginative use of this facility is likely to lead you to coredumps and JAPHs.

    Hugo
      Thank you. This is exactly the type of answer I was looking for.

      I agree with the "not intended for everyday use." Simple assignment of @args = @DB::args or @args = map {Carp::format_arg($_)} @DB::args can cause a blow up. A case we see a lot is when there is a single argument with a value of undef. It happens occasionally - but it does happen. Kind of frustrating when a cluck that is supposed to debug info takes the entire script down. Wish I had an easily duplicatable code snippet for you - but usually there is a bit of a caller trace involved.

      Thanks again.

      my @a=qw(random brilliant braindead); print $a[rand(@a)];
Re: package DB penalty?
by bobn (Chaplain) on May 15, 2003 at 01:52 UTC
    Even if @args had been a deep copy, rather than a shallow one, of @DB::args, it stopped existing after the while {} loop ended, since it was lexical.

    Hence, no penalty whatsoever.


    Bob Niederman, http://bob-n.com
      OOOPS!

      The variable goes out of scope, but, at least under Linux (redhat 7.2), the process still holds onto the RAM.

      Never mind.

      Bob Niederman, http://bob-n.com
Re: package DB penalty?
by PodMaster (Abbot) on May 14, 2003 at 23:57 UTC
    You're not using the DB package (DB - programmatic interface to the Perl debugging API).

    You code snippet makes no sense. Where does @DB::args come from? What are you doing with @DB::a? What are you doing with my @a? my @args?

    @foo = @bar; copies @bar into the variable @foo. Read perldoc perldata.

    update: ah, I see. Still the @a part makes little sense, as does the concern about penalty.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      The reference to @DB::args comes from perldoc -f caller.

      Note that the OP's snippet comes from the Carp module as shipped with an oldish version of perl; that effect is achieved with rather different code in more recent versions (but using the same underlying mechanism) within Carp::caller_info() in Carp::Heavy.

      Hugo
        Interesting - I didn't know they had changed it on 5.8.0. Perl 5.6.0 still used it the same old way. The new way still actually uses the exact same mechanism - just using different programming technique.

        my @a=qw(random brilliant braindead); print $a[rand(@a)];