SPUG: Regexp::Assemble question

Amit Sett amitsett at gmail.com
Tue Feb 24 15:45:13 PST 2009


I had written some Perl code to go through thousands of files and match for
errors contained in a hash shown in brown (see *Old code* section). I
disliked this approach because I didnt want to alter the core program
everytime I added or removed search strings/expressions. My goal is to read
the list of strings from a text file and create the same hash using a loop
as shown in blue (see *New code* section).

The problem with this approach is that attempts to print the values for any
keys of this hash are unsuccessful and show up as blanks. I ran it through a
debugger and saw this instead with the help of Padwalker:

$erl_hash    HASH(0xa541e88)=...
    ArithmeticException    CODE(0xa5493a8)
    ArrayStoreException    CODE(0xa549a88)

I was expecting to see something more like this:
$erl_hash    HASH(0xa541e88)=...
    ArithmeticException    sub {return ArithmeticException}
    ArrayStoreException    sub {return ArrayStoreException}

Any help would be appreciated. I can share the entire program if need be.

Regards,
Amit



*Old code*
# Create the Hash for matching Errors
my $exception_match = {
    'ArithmeticException' => sub {return "ArithmeticException"},
    'ArrayStoreException' => sub {return "ArrayStoreException"},
};


#Error Matching Section
my $match_err = Regexp::Assemble->new( track => 1 )->add( keys
%$exception_match );

my %error_counter = ();                            #This hash stores the
total count of all errors

foreach my $filename (@logFile_list) {
    # First Determine if we need to use DBM to handle file parsing
    my $filesize = stat($filename)->size;
    $Logger->debug("File name is $filename\tFile Size is $filesize");

    # Next Determine if file is gzip compressed
    my $gzipTest = qx(file $filename);
    if($gzipTest =~ m/gzip/i) {
        open(MYINPUTFILE, "<:gzip", "$filename") or $Logger->logdie("Error
opening file: $!");
    } else {
        open(MYINPUTFILE, "<$filename") or $Logger->logdie("Error opening
file: $!");
    }

# This section sends a message to the log file and STDOUT if an exception is
caught
    my $lines = 0;

    while( <MYINPUTFILE> )
    {
        $lines++;
        chomp;
        if( $match_err->match($_) )
        {
            my ($exception_name) = $exception_match->{ $match_err->matched
}( $match_err->mvar() );
            $Logger->debug("Exception was found: $exception_name\tin file:
$filename at line: $lines");
            $error_counter{$filename}{$exception_name}++;
#Logic for counting the various types of errors
        }

    }
}


*New code*
#This section of code reads the err file and saves the key-value pairs in a
hash for easy retrieval
my $erl_hash = {};
my $erl_file = $ini_hash{'error_list_file'};

open(FILE, "<", $erl_file) or die "Can't open $erl_file: $!\n";

while (my $erl_file_line = <FILE>)
    {
        chomp $erl_file_line;
        if (($erl_file_line !~ m/^[\s]*#/gi) && ($erl_file_line !~
m/^[\s]*$/gi ))
        {
            $erl_file_line =~ m/^[\s]*([\w\S]+)[\s]*$/gi;
            $erl_hash->{$1} = sub {return $1};
            print "Added key: ",$1,"\tCorresponding value:
",$erl_hash->{$1},"\n";
        }
    }

#Dumper (%{$erl_hash});

#Error Matching Section
my $match_err = Regexp::Assemble->new( track => 1 )->add( keys %$erl_hash );


my %error_counter = ();                            #This hash stores the
total count of all errors


foreach my $filename (@logFile_list) {
    # First Determine if we need to use DBM to handle file parsing
    my $filesize = stat($filename)->size;
    $Logger->debug("File name is $filename\tFile Size is $filesize");

    # Next Determine if file is gzip compressed
    my $gzipTest = qx(file $filename);
    if($gzipTest =~ m/gzip/i) {
        open(MYINPUTFILE, "<:gzip", "$filename") or $Logger->logdie("Error
opening file: $!");
    } else {
        open(MYINPUTFILE, "<$filename") or $Logger->logdie("Error opening
file: $!");
    }

    # This section sends a message to the log file and STDOUT if an
exception is caught
    my $lines = 0;

    while( <MYINPUTFILE> )
    {
        $lines++;
        chomp;
        if( $match_err->match($_) )
        {
            my ($exception_name) = $erl_hash->{ $match_err->matched }(
$match_err->mvar() );
            $Logger->debug("Exception was found: $exception_name\tin file:
$filename at line: $lines");
            $error_counter{$filename}{$exception_name}++;
#Logic for counting the various types of errors
        }

    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20090224/1bfef5da/attachment.html>


More information about the spug-list mailing list