How can I use Smart :: Comments in a module loaded without changing its source?

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

For example, my script includes

use Neu::Image; 

I would like to download Smart::Comments for Neu::Image , but specifying

 $ perl -MSmart :: Comments script.pl

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

This behavior is described in Smart :: Documentation Comments :

If you are debugging an application, you can also call it using a module from the command line:

 perl -MSmart::Comments $application.pl 

Of course, this only allows comments in the application file not in any modules loading the application.

A few other things that I have already looked at:

SOLUTION As gbacon mentions, Smart :: Comments provides an environment variable parameter that allows you to enable or disable it. However, I would like to be able to enable it without changing the original source, if possible.

+7
command-line module perl
source share
2 answers

You almost certainly want to add use Smart::Comments to modules containing such ones, and then flip the switch in your environment by setting $Smart_Comments accordingly.

Stamping, import hijacking monkey-patching is crazy.

But maybe you believe that. Let's say you have Foo.pm :

 package Foo; use Exporter 'import'; our @EXPORT = qw/ foo /; #use Smart::Comments; sub foo { my @result; for (my $i = 0; $i < 5; $i++) { ### $i push @result => $i if $i % 2 == 0; } wantarray ? @result : \@result; } 1; 

Common use:

  $ perl -MFoo -e 'print foo, "\ n"'
 024 

Normal is boring and boring, of course. With run-foo we take bold, dashing steps!

 #! /usr/bin/perl use warnings; use strict; BEGIN { unshift @INC => \&inject_smart_comments; my %direct; open my $fh, "<", $0 or die "$0: open: $!"; while (<$fh>) { ++$direct{$1} if /^\s*use\s+([AZ][:\w]*)/; } close $fh; sub inject_smart_comments { my(undef,$path) = @_; s/[\/\\]/::/g, s/\.pm$// for my $mod = $path; if ($direct{$mod}) { open my $fh, "<", $path or die "$0: open $path: $!"; return sub { return 0 unless defined($_ = <$fh>); s{^(\s*package\s+[AZ][:\w]*\s*;\s*)$} {$1 use Smart::Comments;\n}; return 1; }; } } } use Foo; print foo, "\n"; 

(Please excuse the compactness: I squeezed it so that everything was in an unprotected block.)

Output:

  $ ./run-foo

 ### $ i: 0

 ### $ i: 1

 ### $ i: 2

 ### $ i: 3

 ### $ i: 4
 024 

& iexcl ;! Viva

With @INC hooks, we can replace our own or modified sources. The code monitors attempts to require modules directly used by the program. When hit, inject_smart_comments returns an iterator that yields one row at a time. When this tricky, sophisticated iterator sees the package declaration, it adds the invisible use Smart::Comments view to the piece, making it look as if it were in the module source.

When trying to parse Perl code with regular expressions, the code will break if the package declaration is not, for example, a separate line. Season to taste.

+10
source share

This idea seems to make no sense. If you use Smart::Comments in a module, why don't you want to use Smart::Comments in this module source? Even if you can get Smart::Comments to apply to all modules loaded into the script via -M , this probably won't be a good idea, because:

  • You confuse the fact that your modules use smart comments, not including the use string in their source.
  • You might be able to introduce weird behavior from the modules you use in the script that seem to look like smart comments, but actually they are not. If the module does not contain intelligent comments, you should not force their throats.

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

In addition, he said, it’s still possible to do this with some insanity, “Stash-munging, import-hijacking monkey-patching” insanity, but it works a lot. I don’t think that someone is going to make an effort to give you a decision in these areas, if first of all this is not a good idea.

+2
source share

All Articles