Slash Boxes
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

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Sunday August 12, 2007
09:17 AM


[ #34086 ]

There are plenty of ways in Perl for the source code of a package to not match the "in memory" version of that code. Usually this is not a problem, but when it causes bugs, it can be very frustrating to debug. Here's one time where it caused me to waste over an hour of debugging:

use Test::More 'no_plan';

    use_ok 'Some::Package';
# many tests
ok foo($x), '... and foo($x) should return true';

What happened was that the use_ok failed, but since I forgot to put an or die modifier after it, I didn't notice that the use statement failed. Many tests scrolled past and I couldn't figure out why my final test was failing.

This can fail when you try to do something like this:

eval "use Some::Package";
my $object = Some::Package->new;

If we forget to check whether or not the eval succeeded, Perl attempts to compile the code and gives up when it can't. This can leave you with what is effectively a partially compiled namespace and strange bugs abound (I was getting a segfault).

To find out if what's in memory matches what you think it should be, I'm writing Devel::Recreate:

use Devel::Recreate 'recreate';
print recreate('Some::Package');

Due to the dynamic nature of Perl, this is very tough to do. Also, strange bugs have been cropping up (some of which I know are fixed in 5.9). Currently, I have it listing the variables defined and the subroutines defined. Now I'm working on the use and require statements. It should probably always be considered experimental as there are too many corner cases I don't know how to work out. I haven't figured out how to capture code outside of subroutines. BEGIN, END, INIT and CHECK blocks don't seem to be coverable (they're not really subroutines), and I can't distinguish between fully qualified package variables and variables declared with our.

All things considered, I'm not sure if this code will be useful or not, but it's an interesting tour of strange bits of Perl.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
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:

  • What’s next? Devel::KickBack? Devel::R’and’R?


    • Next is Devel::Procreate :)

      For the record, I hate the Devel::Recreate namespace. Suggestions welcome.

  • Sounds like the wrong solution. There are lots of valid reasons that the module in memory might not match the one on disk. For instance someone could have created new functions by assigning to a typeglob or calling eval.

    For most cases, a module similar to Apache::Reload is right. Put in a check for which modules have changed on disk since you loaded them, then reload them.

    That doesn't help you in the case you have. But it is easy to create a module that redefines use and require. If you try to use

    • The above example is only one of many. If a module is not behaving the way I expect it to, particularly if "deep magic" is involved, than it's quite reasonable for me to want to see what's actually loaded in memory instead of what I think was loaded. Just a few areas where this would be helpful:

      • Auto-generated classes (such as the many auto-generated classes that ORM folks are fond of)
      • Method generators (Class::Delegator, Class::BuildMethods)
      • Moose!
      • Source filters (right now, debugging them is a
      • I misunderstood. I thought you were proposing this module as a solution to help make sure that what was loaded is the same as the module on disk. You're not. What you're actually proposing doesn't have a simple solution.

        Carry on. :-)

        • Yes, I'm convinced that my module could only be used as a rough debugging guide. There are too many limitations that I cannot figure out how to get around, so it should probably only be a tool that developers can use if they read the docs carefully and really understand what its limitations are.

    • Actually I don’t think it is a bad idea at all. You are right that this is not applicable in all cases in Perl because Perl was not designed to facilitate such usage. However, think Smalltalk, where a lot of “magic” is no problem because what you are browsing is the live in-memory representation of the program and so you see what it looks like after the meta-program parts have taken place.

      This is just the same thing for Perl. Of course it’s not going to work nearly as well in Perl