Phoenix.pm: Attribute::Handlers and BEGIN blocks -- question(s)

Scott Walters phaedrus at illogics.org
Sat Dec 22 18:04:09 CST 2001


Hi Shay,

On Sat, 22 Dec 2001, Shay Harding wrote:
> 
> Now take the following code:
> 
> #!/usr/bin/perl
>  
>   my $test : Test;  #set an attribute for variable
>  
>   print "Top\n";
>  
>    BEGIN{
>        print "In BEGIN 1\n";
>        use Attribute::Handlers;
>        sub Test($;) : ATTR(SCALAR, BEGIN) {
>            print "In profiled sub\n";
>        }
>    }
>  
>   print "Middle\n";
>  
>   BEGIN{
>       print "In BEGIN 2\n";
>   }
> 
> 
> This gives the following error:
> 
> Invalid SCALAR attribute: Test at t.pl line 3
> BEGIN failed--compilation aborted at t.pl line 3.
> 
I'm going to take a stab in the dark here, even though I don't know
what any of this stuff is ;)
Even though, at line 3, nothing is being executed, it's still being tokenized,
and untill that 'use' is hit, that syntax isn't valid. As soon as the use
is hit, the syntax for ':' is introduced, and it can parse that. I'm
assuming ':' isn't a normal operator and your module here does operator
overloading?
After everything is parsed (and not yet run), BEGIN and use's run, then
everything else runs, if memory serves (mecury relay lines - rusty).


> 
> which I really don't understand as it seems it is parsing '$test : Test;' 
> before the BEGIN blocks? I try to assign $test right after the attribute 
> setting and display it within the BEGIN blocks but under -w gives the 
> following (which I would expect):
> 
> Use of uninitialized value in concatenation (.) at t.pl line 9.

-w doesn't run the normal code path, but does still run BEGIN and use's.

> 
> because $test should not exist within the BEGIN block meaning it hasn't yet 
> been looked at by the compiler.
> 
> Why then do I get the 'Invalid SCALAR arttribute' error? The order of 
> processing in the above code is:
> 
> Attribute::Handlers::import
> BEGIN 1
> BEGIN 2
> script
> 
> Which means the atribute handler Test should be parsed before the assignment 
> of $test...
> 
> I know I can fix it by moving the assignment but I'm just curoius as to why 
> it is happening.
> 
> 
> Shay
> 

Let me know if I've completely missed the point...
-scott




More information about the Phoenix-pm mailing list