Re: [PATCH] Attribute::Handlers lexical refcount skew (was Re: lexical with attribute...
Richard Clamp [Mon, 28 Jan 2002 02:17:55 +0000 (02:17 +0000)]
Message-ID: <20020128021755.GA28344@mirth.demon.co.uk>

p4raw-id: //depot/perl@14488

lib/Attribute/Handlers.pm
lib/Attribute/Handlers/t/multi.t

index d4cbfff..78acbdb 100644 (file)
@@ -145,11 +145,18 @@ sub _gen_handler_AH_() {
                            _apply_handler_AH_($decl,$gphase)
                                if $global_phases{$gphase} <= $global_phase;
                        }
-                       # if _gen_handler_AH_ is being called after CHECK it's
-                       # for a lexical, so we don't want to keep a reference 
-                       # around
-                       push @declarations, $decl
-                               if $global_phase == 0;
+                       if ($global_phase != 0) {
+                               # if _gen_handler_AH_ is being called after 
+                               # CHECK it's for a lexical, so make sure
+                               # it didn't want to run anything later
+                       
+                               local $Carp::CarpLevel = 2;
+                               carp "Won't be able to apply END handler"
+                                       if $phase{$handler}{END};
+                       }
+                       else {
+                               push @declarations, $decl
+                       }
                }
                $_ = undef;
            }
@@ -805,6 +812,12 @@ Something is rotten in the state of the program. An attributed
 subroutine ceased to exist between the point it was declared and the point
 at which its attribute handler(s) would have been called.
 
+=item C<Won't be able to apply END handler>
+
+You have defined an END handler for an attribute that is being applied
+to a lexical variable.  Since the variable may not be available during END
+this won't happen.
+
 =back
 
 =head1 AUTHOR
index c327b39..7bcb284 100644 (file)
@@ -165,3 +165,11 @@ sub dummy_our { our $banjo : Dummy; }
 $applied = 0;
 dummy_our(); dummy_our();
 ok( $applied == 0, 51 );
+
+sub UNIVERSAL::Stooge :ATTR(END) {};
+eval {
+       local $SIG{__WARN__} = sub { die @_ };
+       my $groucho : Stooge;
+};
+my $match = $@ =~ /^Won't be able to apply END handler/; 
+ok( $match, 52 );