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

Shay Harding sharding at ccbill.com
Sat Dec 22 16:46:45 CST 2001


Hello all,

Have a question for anyone that might be familiar with Attribute::Handlers 
module. Even if you're not familiar maybe you can provide insight into why 
the following is happening...

Ok, first BEGIN blocks should be evaluated before the rest of the script, 
FIFO order. So the following:


#!/usr/bin/perl

print "Top of script\n";

BEGIN{
    print "BEGIN 1\n";
}

print "Middle\n";

BEGIN{
    print "BEGIN 2\n";
}


will print:

BEGIN 1
BEGIN 2
Top of script
Middle


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.


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.

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






More information about the Phoenix-pm mailing list