develooper Front page | perl.perl5.porters | Postings from April 2003

Re: [perl #21827] Carp::confess() reports wrong name for aliased subs

Thread Previous | Thread Next
From:
Antti Lankila
Date:
April 7, 2003 08:43
Subject:
Re: [perl #21827] Carp::confess() reports wrong name for aliased subs
Message ID:
Pine.A32.3.96.1030407173150.924064K-100000@tokka.elma.fi
On 7 Apr 2003, Rafael Garcia-Suarez wrote:

> > Should be foo at -e line 1
> >          main::bar() called at -e line 1
> 
> I think that's intended, or at least useful, behaviour.
> Consider the following script:
> 
> #!perl
> package Foo;
> use Carp;
> use Exporter;
> BEGIN {
>     our @ISA = 'Exporter';
>     our @EXPORT = 'bar';
> }
> sub bar { confess("Mea culpa") }
> package main;
> BEGIN { Foo->import(); }
> bar();
> __END__
> 
> It outputs:
> 
> Mea culpa at /home/rafael/x line 9
> 	Foo::bar() called at /home/rafael/x line 12
> 
> with the full name (as opposed to the aliased name) of the offending
> function. It looks more useful to the programmer.

It depends. Since writing that initial complaint, I've discovered that perl
seems to store the name of the subroutine along with the code reference itself.
Perl's caller() apparently digs the name up there. So, in general as long as
the code reference is the same, the name visible will always be the name of the
original function, regardless of the name I used to call the function.

Originally this happened in the context of an OO program where I had this kind
of situation:

package A;

sub foo { ... }

*bar = \&foo;

package A::B;
use base 'A';

sub foo { ... }


Now, notice I didn't overload bar(), but I was calling bar() anyway in style
A::B->bar(). That was a bug in my program. Perl correctly called A::bar() in
this case, and then the program died in a very obscure error message, with the
stack trace attached that pointed out that I had actually called A->foo().

Now, I lied a bit. That was my deduction. You know that the stack trace doesn't
say "While calling A::B->foo", it says more like "while calling
A::foo('A::B')", but my point being, I *had* overloaded foo() and I *thought* I
was calling A::B->foo, because I thought the stack trace said so.

After I spent 15 minutes scratching my head, I discovered the nature of the
error, and I still maintain it *is* a bug, because the stack trace should
contain the name I called, not the name of the function inside class which
just happens to implement the name I called. This is not big deal, in the scale
of things, but it seems like one of the minor annoyances you keep on hitting
with Perl. That is why I suggested that a cleaner way to rename a subroutine
was given, one that would report the actual name for the aliased function.

But I agree that your point is solid. Perhaps, in order to get the best of both
worlds, we should really see the both names in case of Exporting and typeglob
aliasing...  Just my thoughts on the issue.

-- 
alankila@elma.net (Antti Lankila, P. +358 50 386 6217)
Platform Manager // Elma Oyj Electronic Trading


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About