leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I love Smart::Comments, use it a bunch. I got greedy and wanted to simply switch it on and off using a variable/optin whatnot.

Thing is , it's a 'source filter'- it does stuff to your code that changes the behaviour- pre compile. Very witty. So.. the following does not work:

#!/usr/bin/perl -w use strict; use Smart::Comments '###'; use Getopt::Std; my $o = {}; getopts('d', $o); ### smart comments are on unless ($o->{d}) { print STDERR 'debug is not on. no smart comments..'; no Smart::Comments; } ### i'd like this to output if -d is used

But of course every comment after no Smart::Comments; will not be parsed to output.

I tried an eval of 'no Smart::Comments' - but that must run before the whole script is compiled- so ... no cigar. I tried some variations of eval and require too.

I also tried a wasteful pre compile to set Smart::Comments to on- but it won't fly through to the rest of the script.

Any suggestions? This is mainly curiosity- seems like an interesting problem. Would there be a way to programmatically (that's a word?) turn off or on a source filter to the same calling source?- ach- head hurts :-)

Replies are listed 'Best First'.
Re: Smart::Comments source filter on or off .. post compile?
by diotalevi (Canon) on Jan 09, 2007 at 22:43 UTC

    Use the if module. It comes with your perl. You'll need to know your conditional at compiletime so you might need to do your getopt() during some kind BEGIN block.

    use if $ENV{DEBUG}, 'Smart::Comments';

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Smart::Comments source filter on or off .. post compile?
by Fletch (Bishop) on Jan 09, 2007 at 19:41 UTC

    Write a wrapper script foo which runs exec perl /some/lib/foo.plx "$@" or exec perl -MSmart::Comments='###' /some/lib/foo.plx "$@" according to the option in question.