Subject: | Enhance Request |
Date: | Mon, 14 Aug 2006 12:47:27 -0400 |
To: | <bug-smart-comments [...] rt.cpan.org> |
From: | "Felix Li" <fli [...] autoweb.net> |
Hi,
This is an enhancement request.
Suppose I have two scripts, namely AlreadyWorking.pl and
NotYetWorking.pl. These scripts have several modules in common say AA.pm
and BB.pm. If I "use Smart::Comments;" in AA.pm and BB.pm (so I can
debug NotYetWorking.pl) then an execution of AlreadyWorking.pl will be
"accompanied" by "unwanted" printing.
To illustrate:
Let's say AlreadyWorking.pl is
[code]
#!
# Hi, I'm a working app!
use strict;
use warnings;
#use Foo; # I'm in production!
use Foo::Bar;
use AA;
use BB;
# Code
exit;
[/code]
and NotYetWorking.pl is
[code]
#!
# Hi, I'm still being debugged
use strict;
use warnings;
use Foo; # I'm being debugged!
use Foo::Bar;
use AA;
use BB;
# Code
exit;
[/code].
And AA.pm and BB.pm look something like
[code]
package AA;
use Bar;
use Filter::tee '>AA.txt';
use strict;
use warnings;
# Code for package AA ...
#BEGIN { print "package AA\n" };
sub A_One {
# We want this to print when AA is used in NotYetWorking.pl but
not print when used in Working.pl
#! This is from package AA
return "A_One";
};
1
[/code][code]
package BB;
use Bar;
use Filter::tee '>BB.txt';
# Code for package BB ...
use strict;
use warnings;
#BEGIN { print "package BB\n" };
sub B_One {
# We want this to print when BB is used in NotYetWorking.pl but
not print when used in Working.pl
#! This is from package BB
return "B_One";
};
1
[/code]
I want to create Foo.pm and Foo::Bar.pm so that Bar.pm does nothing if
Foo isn't used.
Digression: Damian Conway's Smart::Comments allows one to create
"comments that work". But if one uses smart comments into a module,
their "state" depends upon the state of the "use Smart::Comments"
statement in THAT module. So if one has two systems, one working and one
being debugged --- one goes around flipping the "use Smart::Comments;"
statements like burgers at MacDonalds. This isn't very practical.
Well, if Foo looks like
[code]
package Foo;
use Exporter;
@ISA=("Exporter");
@EXPORT=qw();
use strict;
use warnings;
our($FILTER);
BEGIN {
$FILTER='1';
};
1
[/code]
and Foo::Bar.pm looks like
[code]
package Foo::Bar;
use Exporter;
@ISA=("Exporter");
@EXPORT=qw();
use Readonly;
use strict;
use warnings;
use Filter::Simple;
############################################################
# The following smartens the comments "#! ...", "#? ...", "#;-( ..."
########################################################################
########
# Horizontal whitespace ...
Readonly::Scalar my $HWS=>qr/[^\S\n]/;
# A simple note, begins with a "#!"
Readonly::Scalar my $NoteIntro=>qr/#!/;
# ... drastically shortened
FILTER { # Shamelessly stolen from Damian Conway
###############################
unless (defined $Foo::FILTER) {
#print "\$Foo::FILTER NOT defined!\n";
return;
};
shift;
s/\r\n/\n/g;
# A note to be inserted
s{ ^ $HWS* $NoteIntro [ \t]+ (.+) $HWS* $ }
{ print qq{$1}."\n"; }gmx;
# ... also drastically shortened!
}; # FILTER: done
# Subs:
{
sub QuietEval__ { # Shamelessly stolen from Damian Conway
################
local $SIG{__WARN__} = sub{};
print "QuietEval : '$_[0]'\n";
return scalar eval shift;
}; # QuietEval: done
}; # Subs: done
1;
[/code]
then we'll get a more elegant behavior namely
[code]
C:\FooBar>perl Working.pl
A_One
B_One
C:\FooBar>perl NotYetWorking.pl
From "NotYetWorking"
This is from package AA
A_One
This is from package BB
B_One
[/code]
Your truly,
Felix