I've recently found the need to unravel, debug, and refactor old code that breaks almost every rule in the book (think "Perl Worst Practices" ;). This has presented "interesting" challenges, particularly the need to understand the source of symbols spewed into the main namespace from all manner of careless EXPORTs.

One of the things I want to do is have a running trace (in Apache logs, for example) of subroutine calls with their fully-qualified names. I want to do more than print sub names, however. I want to wrap them using Hook::LexWrap to provide general flexibility for inspecting a multitude of conditions surrounding each call (H::L allows you to install pre- and post- wrappers for subs while preserving the expected behavior of caller()).

I looked in the Devel:: namespace and found some modules that do similar things. However, none of them seem to do exactly what I want:

Toward this end, I'm creating a module, Devel::WrapExports, that behaves as explained above. If, for example, I am facing a monster CGI that 'use's twenty libraries, each of which forcefully exports all kinds of symbols, I can simply say 'use Devel::WrapExports' at the top of the CGI and cause each subroutine that has been (perhaps inadvertently) imported into the CGI to be wrapped with default pre- and post-wrappers to trace entry and exit. I can pass my own callbacks to 'use' and they will be installed as wrappers to do all manner of custom tracing, logging, etc.

I'd like to share the module on CPAN, but I wanted to ask first -- there are lots of Devel:: modules that do tracing, so I don't want to commit my own form of namespace pollution by adding a duplicate. Have I overlooked an existing module that provides the functionality I've described, or have I come up with something novel?

Thanks!

  • Comment on [RFC: Module proposal] Tracing sneaky EXPORTs using wrappers

Replies are listed 'Best First'.
Re: [RFC: Module proposal] Tracing sneaky EXPORTs using wrappers
by lodin (Hermit) on Apr 10, 2008 at 16:57 UTC

    This seems like a good idea. I'd like to know a little more though:

    • How does the synopsis section of the POD look?
    • Is it compatible with non-Exporter.pm exporters?
    • Do you trace only subroutines?
    • How is the actual tracing done?
    I think that if you're going to release this to CPAN, you should put some extra thought into making it potentially useful not just for the task your doing, but also other similar tasks, without making the original idea any less convenient. In fact, I think you shouldn't implement anything you don't need, but design so that it's easy to add and put a comment in the documentation saying that you're open to suggestions and patches.

    A synopsis and a brief summary of the implementation would really help.

    lodin

      Here's the synopsis:
      SYNOPSIS # Wrap all subs in default code to warn on entry/exit, displaying +args and return values... use Devel::WrapExports; # The same thing, but ignore the specified packages and subs... use Devel::WrapExports exempt_pkgs => [ qw( Devel Carp CGI ) ], exempt_subs => [ qw( Nevermind::me ) ]; # Use my own subs for tracing instead of the defaults... use Devel::WrapExports pre => q{ sub { "custom pre-wrapper..." } }, post => q{ sub { "custom post-wrapper..." } }; # Run as script to list fully-qualified names of each symbol (sub +or otherwise) in the top-level scope... $ perl /path/to/Devel/WrapExports.pm <perl-source-file>
      Some more background:

      Initially, I only wanted to know where to go to find the source of hundreds of imported subs in code that I was looking at. Then, I realized that if I could automatically wrap all imported subs with warnings on entry/exit, I could get a nice trace in Apache error logs.

      Currently, the technique is to look at all packages that are explicitly 'use'd or 'require'd by the caller (these are found by examining the caller's source code), then find all symbols that each of those packages export (this is done by looking at the contents of @EXPORT and @EXPORT_OK in each of these packages' symbol tables, so it's not compatible with non-Exporter.pm exporters). For each such symbol that is a sub, exploit Hook::LexWrap to wrap the sub (the one already imported into the caller's package) with "tracing code" as implied in the SYNOPSIS above (e.g. default or custom tracing subs).

      Your question about handling non-Exporter.pm exporters is a good one. I hadn't considered that in my focused mindset. I plan on adding that support. It will be a matter of looking at the caller's symbol table instead of the caller's source (which is almost certainly a better approach anyhow).

      So, in summary:
      • I'm planning on supporting non-Exporter.pm exporters.
      • Only subs are traced (by design), using Hook::LexWrap (but calling the module as a program shows all types of symbols in the package of the argument).

        The name
        If you plan to support only subroutines I don't think it should be called Devel::WrapExports. That name sound more generic and the way I interpret it is that the exporter mechanism will be wrapped to allow me to inspect everything that was exported. I don't know what a better name would be, but the uncommonly long Devel::WrapImportedSubs doesn't seem to bad after all. Imperfect as it may be the concept is clearly communicated. "Devel" - it's to help development; "wrap" - you do something upon each call; "imported" - only imported subroutines are of interest; "subs" - only subroutines are of interest. Note that I suggest "imported" over "exported" because you do it from a caller perspective. The package doing "use Devel::...;</c> later imports from the modules it uses. "Export" makes sense from the module author's perspective (like used as Exporter.pm), e.g. if a module Foo wanted to register every case of use Foo;.

        The interface
        I think that you have a good interface. However, I think you should open up to other ways of wrapping. Hook::LexWrap is convenient, but it can also break code that relies on caller(). It's possible (and simple) to do a completely transparent pre-hook, so another way to install the hook is quite important.

        The implementation
        Good luck. I think you'll need it. :-) It's quite a complicated task; there are several tricky cases. I'm looking forward to seeing how you solved it.

        lodin