This is my first attempt at Perl so I know this code is ugly. Some of its from not knowing what I'm doing and some of its from troubleshooting various issues. What I'm trying to do is search a file(samplefile.txt) for various information (the 9 parse_updates functions) and it works fine unless the order changes. For example if one samplefile has Certificate Bundle before Botnet Definitions then it will fail to find the Certificate Bundle information. I expected each function to start searching the samplefile "fresh" but that doesn't seem to be the case and I can't figure out why. Not including a samplefile as the code post is already long enough and I think the problem is in my function logic.
use strict;
use warnings;
use diagnostics;
use File::Slurp;
my @autoupdate;
my $autoupdate;
my $av_regex;
my @av_updates;
my $avdev_regex;
my @avdef_updates;
my $ipsatt_regex;
my @ipsatt_updates;
my $attdef_regex;
my @attdef_updates;
my $ipsmal_regex;
my @ipsmal_updates;
my $flowav_regex;
my @flowav_updates;
my $botnet_regex;
my @botnet_updates;
my $appdef_regex;
my @appdef_updates;
my $ipgeo_regex;
my @ipgeo_updates;
my $certbun_regex;
my @certbun_updates;
my $str1;
my $str2;
my $str3;
my $str4;
my $str5;
my $str6;
my $str7;
my $str8;
my $str9;
parse_updates1(); #AV Engine
parse_updates2(); #Virus Defs
parse_updates3(); #IPS Attack Engine
parse_updates4(); #Attack Defs
parse_updates5(); #IPS Mal URL DB
parse_updates6(); #Flow virus Defs
parse_updates7(); #Botnet Defs
parse_updates8(); #IP Geo DB
parse_updates9(); #Cert Bundle
sub parse_updates1{
print "\nTHIS IS AV Engine Section!!\n\n";
read_file('samplefile.txt', buf_ref => \$str1);
my $av_regex =qr/(AV Engine)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str1 =~ /$av_regex/g ) {
#putting each regex group into the array
push @av_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @av_updates;
print "$_\n" for @av_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@av_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @av_updates;
}
}
sub parse_updates2{
read_file('samplefile.txt', buf_ref => \$str2);
print "\nTHIS IS Virus Definitions Section!!\n\n";
my $avdef_regex =qr/(Application Definitions)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str2 =~ /$avdef_regex/g ) {
#putting each regex group into the array
push @avdef_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @avdef_updates;
print "$_\n" for @avdef_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@avdef_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @avdef_updates;
}
}
sub parse_updates3{
read_file('samplefile.txt', buf_ref => \$str3);
print "\nTHIS IS IPS Attack Engine Section!!\n\n";
my $ipsatt_regex =qr/(IPS Attack Engine)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str3 =~ /$ipsatt_regex/g ) {
#putting each regex group into the array
push @ipsatt_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @ipsatt_updates;
print "$_\n" for @ipsatt_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@ipsatt_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @ipsatt_updates;
}
}
sub parse_updates4{
read_file('samplefile.txt', buf_ref => \$str4);
print "\nTHIS IS Attack Definitions Section!!\n\n";
my $attdef_regex =qr/(Attack Definitions)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str4 =~ /$attdef_regex/g ) {
#putting each regex group into the array
push @attdef_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @attdef_updates;
print "$_\n" for @attdef_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@attdef_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @attdef_updates;
}
}
sub parse_updates5{
read_file('samplefile.txt', buf_ref => \$str5);
print "\nTHIS IS IPS Malicious URL Database Section!!\n\n";
my $ipsmal_regex =qr/(IPS Malicious URL Database)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str5 =~ /$ipsmal_regex/g ) {
#putting each regex group into the array
push @ipsmal_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @ipsmal_updates;
print "$_\n" for @ipsmal_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@ipsatt_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @ipsatt_updates;
}
}
sub parse_updates6{
read_file('samplefile.txt', buf_ref => \$str6);
print "\nTHIS IS Flow-Based Virus Definitions Section!!\n\n";
my $flowav_regex =qr/(Flow-based Virus Definitions)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str6 =~ /$flowav_regex/g ) {
#putting each regex group into the array
push @flowav_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @flowav_updates;
print "$_\n" for @flowav_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@flowav_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @flowav_updates;
}
}
sub parse_updates7{
read_file('samplefile.txt', buf_ref => \$str7);
print "\nTHIS IS Botnet Definitions Section!!\n\n";
my $botnet_regex =qr/(Botnet Definitions)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str7 =~ /$botnet_regex/g ) {
#putting each regex group into the array
push @botnet_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @botnet_updates;
print "$_\n" for @botnet_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@botnet_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @botnet_updates;
}
}
sub parse_updates8{
read_file('samplefile.txt', buf_ref => \$str8);
print "\nTHIS IS IP geography DB Section!!\n\n";
my $ipgeo_regex =qr/(IP Geography DB)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str8 =~ /$ipgeo_regex/g ) {
#putting each regex group into the array
push @ipgeo_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @ipgeo_updates;
print "$_\n" for @ipgeo_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@ipgeo_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @ipgeo_updates;
}
}
sub parse_updates9{
read_file('samplefile.txt', buf_ref => \$str9);
print "\nTHIS IS Certificate Bundle Section!!\n\n";
my $certbun_regex =qr/(Certificate Bundle)(.*\n)*?(Version:)(.*\n)*?(Contract Expiry Date:)(.*\n)*?(Last Updated using )(.*\n)*?(Last Update Attempt: )(.*\n)*?(Result: )(.*\n).*/p;
if ( $str9 =~ /$certbun_regex/g ) {
#putting each regex group into the array
push @certbun_updates, $1, $2, $3 ,$4, $5, $6, $7, $8, $9, $10, $11, $12;
#Removing new linefeeds
chomp @certbun_updates;
print "$_\n" for @certbun_updates;
}
else {
print "\n\nGot Nothing!\n\n";
@certbun_updates = qw(notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound notfound);
print "$_\n" for @certbun_updates;
}
# End of sub parse_updates
}