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

Problem with attributes not running from do()

Thread Next
Daisuke Maki
October 6, 2003 00:08
Problem with attributes not running from do()
Message ID:
Hi, I posted this elsewhere and was suggested that I send it to p5p to
see if this behavior can be improved.

Basically I want to put a wrapper around functions using attributes,
somewhat like the Attribute::Deprecated [1], where you would say

  sub myfunc : myattr
     # blah blah

And the attribute would cause a handler like this to be invoked:

  sub myattr : ATTR(CODE)
     my($pkg, $symbol, $referent ...) = @_;
     *{$symbol} = sub {
        # additional code
        goto &$referent;

Such that it effectively redefines the original function to have a wrapper.

The only problem with this is that the code that intended to use this
attribute is sourced in via do(). And code included from the likes of
do() do not trigger CHECK and INIT blocks to be run. Yikes.

That means that the attribute handler can only be run at BEGIN or END
block -- but that doesn't help. Running at END block would be too late,
and running at BEGIN block doesn't work either because at BEGIN time the
subroutine doesn't yet have a name where the new subroutine can be
installed ($symbol is a string = 'ANON') [2]

...Which kind of rendered my whole scheme useless, and to me renders the
whole attribute thing useless because most of the code that I currently
deal with *has* to go through do(), or some sort of inclusion mechanism
that doesn't trigger CHECK blocks.

I'm not sure what the reasoning is for the behavior difference -- why is
this? Can this be changed, or can the attributes mechanism changed
somehow so that it also works for code included from do()?


P.S. I'm currently using perl5.6.1 at the moment, but I believe it
hasn't changed in 5.8.1 either?


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About