On Fri, 2002-04-26 at 14:51, Jason Frisvold wrote:
> Hrm..  I'll run some tests with it and see what I find out...  Sounds
> promising if it really is a preprocessor...  So does it just do
> something along the lines of a cat <file> | grep -v <stuff> and then
> execute the output?  (I realize that is simplifying it by a great deal)
> 
> I'm just trying to wrap my head around this...  The program (actually
> programs) that I've been writing runs anywhere from once every 5 minutes
> to once a night...  I know I can make it faster by porting to C, but
> it's that much easier to do in perl.  However, I don't want to waste
> cycles running through a preprocessor every time, or running through a
> debug routing every time when it's not needed...
> 
> Hrm...  more thought needed..  *grin*
> 
> ---------------------------
> Jason H. Frisvold
> Senior ATM Engineer
> Engineering Dept.
> Penteledata
> CCNA Certified - CSCO10151622
> [EMAIL PROTECTED]
> ---------------------------
> "Imagination is more important than knowledge. Knowledge is limited.
> Imagination encircles the world." -- Albert Einstein [1879-1955]

It will run through the filter every time it is executed so a program
with a filter will be slower than a program without a filter.  Filters
work by using BEGIN blocks and some really neat backend magic.  Here is
an example filter (with new comments) I wrote based on your earlier
email.  In fact, come to think of it, if you reverse the import and
unimport subs, reverse substitution (s//^#/), and remember to comment
out the debug code your production code will run a top speed and your
debug code will be a little slower instead of the other way around.

#name the module
package DEBUG;

#all of the magic comes from this module
use Filter::Util::Call;

#minor magic that makes warns and dies report as if the code
#were in the calling file
use Carp;

#unimport gets called when you say "no MODULENAME;"
#I am using it here for my main filter because I
#want to be able to say "no DEBUG;" instead of 
#"use DEDEBUG;" or other negative name.
sub unimport {
        #grab the args passed into the module (ignore)
        my ($type, @other_args) = @_;

        #this variable determines whether we are in
        #a chunk of code that needs to be commented
        my $in_debug      = 0;

        #filter_add sets up the filter.  There are two
        #ways to use it, I am using the closure method
        #because I think closures rock.  The anonymous 
        #subroutine I define here will be called until
        #there is no more data is left in the file, an
        #error occurs, or the filter_del sub is called
        filter_add (sub {

                #this command reads a line from the source
                #and stores it in $_.  $status is set to 0
                #if there are no more lines, 1 for success
                #or to a negative number for failure 
                my $status = filter_read;

                #If we are in a #DEBUG ON block and the file
                #ends then we have a problem; better to fail
                #than to screw up the script
                croak "unbalanced '#DEBUG'" if $status == 0 and $in_debug;

                #if the line starts with #DEBUG ON then
                #we need to start commenting out lines
                if (/^\s*#DEBUG ON/) {
                        #warn the user that nesting #DEBUG ONs 
                        #can be dangerous (bad things can happen
                        #if you combine "use DEBUG;", "no DEBUG;",
                        #and nested #DEBUG ONs).  We increment
                        #$in_debug so that we know how deeply we 
                        #are nested (there must be a matching 
                        #number of #DEBUG OFFs).
                        carp "nested #DEBUG ON is dangerous" if ++$in_debug > 1;
                }

                #else if the line contains #DEBUG OFF then we need to stop
                #commenting out lines.  We decrement $in_debug for the
                #same reason we incremented it above.
                elsif (/^\s*#DEBUG OFF/) {
                        #if there are more #DEBUG OFFs than #DEBUG ONs
                        #then we have a serious problem.
                        croak "unbalanced '#DEBUG'" if --$in_debug < 0;
                }

                #else if we are currently in a debug block then prepend
                #a comment character to the line
                elsif ($in_debug) {
                        s/^/#/;
                }

                #return the status
                return $status;
        });
}

#the import subroutine gets called when you "use MODULENAME;".
#I am using it here to turn off the filter.
sub import {
        filter_del();
}

#modules must return true.
1;

-- 
Today is Sweetmorn the 43rd day of Discord in the YOLD 3168
Grudnuk demand sustenance!

Missile Address: 33:48:3.521N  84:23:34.786W


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to