Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Your BEGIN, CHECK, INIT, and END blocks are present in the arrays Perl_(begin|check|init|end)av[]. You will need to set some variable like Perl_savebegin or somesuch to have anything to look at in Perl_beginav[].

    There might not be an array for UNITCHECK.

    Manip::END lets you muck with END blocks from perl-space. There's another one which does other blocks but I forget its name.
    • Aw, crap. My XS skills are week. However, the code itself doesn't look too difficult. I'll play with it. Right now it appears to work, but the code itself isn't too useful because of the 'naked' code reference:

      #!/usr/bin/perl -l

      use strict;
      use warnings;

      use Data::Dumper;
      use Manip::END;
      use Sub::Information;

      {
          package Foo;

          END { print 'end!' }
      }

      my $ends = Manip::END->new;
      print Dumper($ends);
      print $ends->[0]->();
      print inspect( $ends->[0] )->code;

      That produces:

      $VAR1 = bless( [
                       undef
                     ], 'Manip::END' );

      end!
      1
      Use of uninitialized value in string eq at /usr/local/share/perl/5.8.8/Sub/Information.pm line 246.
      Bizarre copy of CODE in sassign at /usr/share/perl/5.8/Carp/Heavy.pm line 39.
      end!

      (Where does that '1' come from?)

      You can't assign the code ref to anything and it's virtually useless for my needs. If I were to somehow store this in a scalar and return an array of scalars, it sounds like it would be more useful. I think what I really need is something like this pseudo-code:

      AV *get_end_array() {
          int i;
          AV *end_array;
          SV **elem;
          SV *scalar_with_code_ref;

          if (!PL_endav) {
              PL_endav = newAV();
          }

          av_extend( end_array, av_len(PL_endav) );
          for ( i = 0; i < av_len(PL_endav); i++ ) {
              elem = av_fetch(PL_endav, i, 0);

              // store elem in scalar_with_code_ref
              av_store( end_array, i, scalar_with_code_ref );
          }

          return end_array;
      }

      Unfortunately, my knowledge of perlguts is even worse than my knowledge of C. I'm sure the above is broken (well, I know it is because of the comment) :)