views:

265

answers:

2

How can I specify that Smart::Comments be loaded for my original script, as well as for any of the modules it directly loads. However, since it is a source-filter, it would probably wreck havoc if applied to every module loaded by every other loaded module.

For example, my script includes

use Neu::Image;

I would like to load Smart::Comments for Neu::Image as well, but specifying

$ perl -MSmart::Comments script.pl

does not load Smart::Comments for Neu::Image.

This behavior is described in the Smart::Comments documentation:

If you're debugging an application you can also invoke it with the module from the command-line:

perl -MSmart::Comments $application.pl

Of course, this only enables smart comments in the application file itself, not in any modules that the application loads.

A few other things that I've looked at already:

WORKAROUND As gbacon mentions, Smart::Comments provides an environment variable option that would allow turning it on or off. However, I would like to be able to turn it on without modifying the original source, if possible.

+7  A: 
Greg Bacon
Thanks. I forgot about the environment switch.
molecules
Wow! I mean, WOW! Kudos to gbacon, the Perl Master!
molecules
Thanks for the references to @INC hooks. I really didn't want to ask for you or anyone else to do a lot of work. I thought there might be something simpler. Wow, that is some awesome magic!
molecules
Bug: the module has to be in the current directory. As written, it won't search `@INC`.
Greg Bacon
+2  A: 

It doesn't seem like this idea makes any sense. If you are utilizing Smart::Comments in a module, why would you not want to use Smart::Comments in that module's source? Even if you could get Smart::Comments to apply to all modules loaded in a script via -M, it probably wouldn't be a good idea because:

  • You're obfuscating the fact that your modules are using smart comments by not including the use line in their source.
  • You could potentially introduce bizarre behavior from modules you use in your script which happen to have what look like smart comments, but aren't really. If a module doesn't contain smart comments, you should not force them down its throat.

As gbacon said, the right way to do this is to use the module in each of your modules that make use of it, and then suppress them with an environment variable when you don't want the output.

Also as he said, it's still probably possible to do this with some "Stash-munging, import-hijacking monkey-patching" madness, but that's a lot of work. I don't think anyone is going to put the effort into giving you a solution along those lines when it is not a good idea in the first place.

Adam Bellaire
Thanks for your answer. For me Smart::Comments are only for debugging. From brian's comments I was starting to think that there was another way to do it that wasn't so complicated. I wouldn't ask for anyone to go through a lot of effort.
molecules