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

Re: Problem with attributes not running from do()

Thread Previous | Thread Next
Daisuke Maki
October 6, 2003 10:13
Re: Problem with attributes not running from do()
Message ID:

>>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. 
> I'm not sure I fully understand your problem. Why not putting your code
> in a BEGIN block at the end of the file you're sourcing via do() ?

That's certainly a workaround, and in fact now that I know I can't use 
attributes, I just created a function that does the equivalent of:

   package MyPackage;
   use SomeOtherPackage qw(make_wrapper);
   sub foo { ... };

Yes, that certainly works.

But the behavior difference was just a surprise when I initially tried 
doing that with attributes. When I was first testing my attribute on a 
simple script it worked with no problem, and then I plugged it into the 
production environment and the attributes didn't even run, simply 
because of the do() call. Most everything else works exactly the same 
when you just launch a program from the main perl interpreter or source 
it in via do(), why should this be different?

Also, there's the By default Attribute::Handlers runs your attributes at 
CHECK time unless otherwise specified -- so you need to write your 
attributes differently depending on if it's expected to be used from 
do() or not? That just seems strange...

My question is, why can't this behavior be standardized across the 
board? Symbol names are pretty darn useful, so it makes sense to run the 
handler at CHECK time, so why can't code sourced in via do also run 
CHECK blocks? This is especially confusing because BEGIN blocks are run, 
even for do().

# I guess if this is a issue in perl that cannot be fixed, it just
# may be a good idea to include a big warning that CHECK and INIT
# blocks are not run from do() and others, as well as on the
# Attributes::Handler POD. It took me quite a while to find out
# that that was the culprit.


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