bleadperl adds a new kind of block, UNITCHECK. It is a new hook that runs after your code has been compiled. It fixes architectural issues with the hooks CHECK and INIT but avoids altering those and thus avoids causing problems for CHECK or INIT using code.

The following is an attempt to be get 5.10's UNITCHECK blocks to fall back to CHECK on less capable perls. This proof of concept works. There's also Check::UnitCheck which I'm sure works just fine. It might even be preferred but I wanted to see if I could get my same-syntax/pure-perl version working.

BEGIN { if ( $] < 5.009004 ) { my $src = <<'UNITCHECK'; print "Inventing UNITCHECK for $]\n"; require B; # This is a stub function. It exists just to capture the b +lock that will # follow it as a parameter. Magic will find this later and + call its argument. sub UNITCHECK (&) {} CHECK { # Read all the values in the main_cv's PADLIST. This p +robably doesn't work # for files that aren't the "main" script. for my $cv ( B::main_cv->PADLIST->ARRAY->ARRAY ) { # Find all anonymous subroutines. next unless $cv->isa( 'B::CV' ); # Hey! I'm missing the check to see that I'm in a +UNITCHECK block! # WTF!? Was the author on crack?! This is promiscu +ous as hell! # Call the anonymous function. my $sub = $cv->object_2svref; $sub->(); } } 1; UNITCHECK eval $src or die "$@\n$src"; } } UNITCHECK { print "Ok!\n"; }

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re: A fail-over UNITCHECK for non-bleadperl
by smammy (Novice) on Nov 18, 2006 at 19:43 UTC
    How about using a source filter? Maybe something like:
    use Filter::Simple; FILTER_ONLY code_no_comments => sub { $]<5.009004 && s/\bUNITCHECK\b/CHECK/g };

      No. That's just asking for trouble.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊