Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: debugging during compile

by armstd (Friar)
on Aug 18, 2011 at 06:26 UTC ( #920866=note: print w/replies, xml ) Need Help??

in reply to debugging during compile

I've seen pretty good direct debugging suggestions already, so I'll go a bit off topic here...

You've mentioned you have other BEGIN blocks already. What do your BEGIN blocks DO? And why? I'll also point out that code in a module that isn't confined to a sub in a module will execute at "compile time" for the caller, just after the BEGIN blocks in the current module. It's important to understand that Perl can (and usually does) have multiple "compile time" phases interspersed with "run time" phases.

Do you have any code that isn't in BEGIN blocks or sub blocks in your modules? Do you realize that will also execute at your caller's "compile time" (actually at the module's run time, but prior to the 'use Module;' returns to the caller)? The standard "1;" we put at the bottom of a Module is executed in that module's run time, for instance.

Generally I don't recommend doing anything at all at compile time other than:

  • declare package/code structure - package namespace, base class, exports, pragmas
  • declare package globals - 'my' wherever possible, 'our' if absolutely necessary, initialized undef
  • declare/initialize package global constants
  • declare subs
Anything not on that list... is either extremely exceptional, or violates one of my design principles. Anything that involves doing anything beyond simple declaration... save it for runtime. Your methods will know when runtime is happening, because they won't get called at any other time. Do object initialization at first method call. Do package initialization at first object initialization. If no method is ever called, be glad you saved all that time and effort.

So my big question is, what are your modules doing outside of that list of declarations? Does it really need to be done at "compile time"? I've seen cases where the designer had modular concrete subclasses "registering" themselves with their abstract class at compile time, as one example. Perl allows other ways to do that kind of thing though. Compile time in Perl is precious, and should not be abused. It delays response-time in interactive programs and CGIs. In my experience, the less that gets done at compile time in Perl, the better.

Do any of your Modules use shift() or pop() outside of a sub? In the package global space, @ARGV can work for shift/pop like @_ does inside a sub, as a convenience. Here's an example of a badly behaved module:

$ cat package Module; our $global = shift(); # stole the first arg already... while( $arg = shift() ){ print "Module stealing arg: $arg\n"; }; 1; $ $ perl -e 'BEGIN { print "\nbefore using my module: ".join(", ",@ARGV) +."\n" } use Module; BEGIN { print "\nafter using my module: ".join(", + ",@ARGV)."\n" } print "Module::global: $Module::global\n";' one two +three before using my module: one, two, three Module stealing arg: two Module stealing arg: three after using my module: Module::global: one $

I would recommend breaking down your modules. Start over, recreating them from the ground up, cut&pasting bit by bit. Create a main caller that only looks at @ARGV and compiles the modules like mine does above. Reconstruct the modules from scratch, Starting with just package structure (package declaration, base class, exports) and subs. See what happens. Add package global my/our declarations. See what happens. What's left, and why? Does it need to be done at compile time?


Replies are listed 'Best First'.
Re^2: debugging during compile
by pileofrogs (Priest) on Aug 18, 2011 at 18:12 UTC

    Ladies and gentlemen, we have a winner!

    I hadn't realized the code in the module would be run after compile and before the calling script continued compiling.

    I put some print statements in the main body of my module and sure enough, ARGV was being altered in there. I tracked it down with a few more prints and found it.

    Somehow in refactoring a sub, I had left a

    my $self = shift; my $item = shift;

    sitting naked in the main body of the module. Whee!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://920866]
usemodperl gained... A NEW CAR! Thank you vroom :-)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-06-21 01:38 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.