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

Hi, WARNING: If you run these scripts, you will probably
get character corruption of your terminal, which can be
cleared with "reset". My first question: is there a simple
print code that I can print as the last line of my script to
reset the screen without losing the data printout?
If I type clear or reset after the script runs, it wipes
out my previous output. Is there some way of printing
a last line, say
print '[[asc', "done\n" ;
or something similar that will just reset my next line only?

Question 2:
I'm playing with a homemade virusscanner.
I've learned to do binary regexes, learned the value
of precompiling regexes, and now I'm working on
storing the precompiled binary regexes to a file
using Storable.

What I can do sucessfully, is convert hex signatures
to binary values, and store them in a hash via Storable.
I can retreive the hash, and do regexes sucessfully on
binary files.

What I want to do, is precompile the binary regexes and
store them with Storable. This isn't working for me, and
I'm stumped.
I've tried everything in my small toolbox, to get
$sigs{$name}= qr/\Q$valbin\E/;
to be accurately recovered.
Tried
"qr/\Q$valbin\E/";
and
quotemeta qr/\Q$valbin\E/;
with differing results.

2 scripts below demonstrate my roadblock.
########################################
#!/usr/bin/perl #create the Storable file use warnings; use Storable; my %sigs; while (<DATA>){ chomp; ($name,$value) = split(/=/,$_); $valbin = pack 'H*', $value; $sigs{$name}= $valbin; # this one works # $sigs{$name}= qr/\Q$valbin\E/; # this one dosn't } foreach $name (keys %sigs){print "$name\t$sigs{$name}\n";} store(\%sigs, 'z1.bin') or die "Can't store %a in z1.bin !\n"; exit; __DATA__ 10 past 3 (B)=ec020e1ff3a4b82125061fbab300cd21 10 past 3 (C)=b840008ed8a11300b106d3e02d00088e 100-Years=fe3a558bec50817e0400c0730c2ea147 1024-PrScr #1=8cc0488ec026a103002d800026a30300 1024-PrScr #2=a172041f3df0f07505a10301cd0526a1
##############################################

#############################################
#!/usr/bin/perl #read the Storable file #use strict; use warnings; use Storable; my %sigs = %{retrieve('z1.bin')} or die "Unable to retrieve from z1.bi +n:$!\n" ; foreach $name (keys %sigs){print "$name\t$sigs{$name}\n";}
#################################################

Edit by tye to escape [s

Replies are listed 'Best First'.
•Re: storing pre-compiled binary regexes
by merlyn (Sage) on Jun 24, 2002 at 21:10 UTC
    What I want to do, is precompile the binary regexes and store them with Storable.
    I want to be 20 pounds thinner, 6 inches taller, and be debt-free, and no longer a Felon.

    I won't get my wishes within this lifetime. You probably won't get your wishes until at least Perl 5.10. {grin}

    -- Randal L. Schwartz, Perl hacker

      Six years on and 5.10 is with us. Is it now possible to serialise and store the binary compiled version of the regex ?

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!

        OK, not sure if I am doing something wrong but from my testing Perl 5.10 can not do this either

        This is perl, v5.10.0 built for MSWin32-x86-multi-thread (with 3 registered patches, see perl -V for more detail)
        Here is an attempt to store some compiled regex
        #!/usr/bin/perl #create the Storable file use warnings; use Storable; use Data::Dumper; my %sigs; while (<DATA>){ chomp; ($name,$value) = split(/\s+=\s+/,$_); next unless $value; $sigs{$name}= qr/$value/; } store(\%sigs, 'z1.bin') or die "Can't store %a in z1.bin !\n"; print Dumper \%sigs; exit;
        And here I try to read it back again
        #!/usr/bin/perl #read the Storable file #use strict; use warnings; use Storable; use Data::Dumper; my %sigs = %{retrieve('z1.bin')} or die "Unable to retrieve from z1.bi +n:$!\n" ; print Dumper \%sigs; exit; __DATA__ 1_Friday = Friday 2_random = [Rr]andom 3_catch any = .*
        These are the results of the two dumps:
        C:\Perl_5_10\play>..\bin\perl store.pl $VAR1 = { '3_catch any' => qr/(?-xism:.*)/, '2_random' => qr/(?-xism:[Rr]andom)/, '1_Friday' => qr/(?-xism:Friday)/ }; C:\Perl_5_10\play>..\bin\perl read.pl $VAR1 = { '3_catch any' => qr/Regexp=SCALAR(0x1a6bca4)/, '2_random' => qr/Regexp=SCALAR(0x1a7c90c)/, '1_Friday' => qr/Regexp=SCALAR(0x1c47c7c)/ };
        if I try to use the read back regex they fail to match

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!
Re: storing pre-compiled binary regexes
by Joost (Canon) on Jun 25, 2002 at 09:19 UTC
    What I want to do, is precompile the binary regexes and store them with Storable.

    Like Randall said, this is very likely impossible in perl (though if you succeed in writing an XS module that accomplishes this please put it on CPAN (-: )

    I think the best option here is to write out a perl-module that sets up your regexes and then require it.

    maybe something like:

    print REGEX_MODULE_HANDLE "$REGEX{$re_name} = qr/$re_string/;";

    This will likely run a bit slower, but your development time will be a LOT shorter :-)

    -- Joost downtime n. The period during which a system is error-free and immune from user input.