package HTML::pageParser; use strict; use Carp; use vars qw($VERSION @ISA @EXPORT); use Exporter; @ISA = qw(Exporter); # Make some utility functions available if asked for @EXPORT = (&loadAndParse &loadFile &parseData $result $debug); # this creates our class/object sub new { my $proto = shift; my $class = ref($proto) || $proto; my ($sysLoc) = my @orig_args = @_; my $self = {}; $self->{%tokens} = []; $self->{sysLoc} = $sysLoc; bless ($self, $class); $debug .= "new class has been run
"; return $self; } # this file reads a text file and loads the contents into an array sub loadFile($) { my $self = shift; my ($filename) = @_; if(open this, $filename) { while ($line = ) { $body .= $line; } close this; } else die("cannot open the file:" $filename); $debug .= "loadFile has been run
"; return($body); } # this function parses through the contents of a scalar variable and does a search/replace on the tokens # the only thing left to figure out in this function is to eliminate the duplicate matches found # before doing the replacing of any tokens. This will be a minor decrease in processor resources. sub parseData($) { my $self = shift; my ($data) = @_; my %tokens = $self->{tokens}; foreach my $match($data =~ /\%(\S+?)\%/g) { if($tokens{$match}) { $data =~ s/%$match%/$tokens{$match}/g; } else { $data =~ s/%$match%//g; } } $debug .= "parseData has been run
"; return($data); } # this is a quick way to both load a flat file and parse it's contents for tokens. sub loadAndParse($%) { my $self = shift; my ($filename,%tokens) = @_; $data = loadFile($filename); $body = parseData($data); $debug .= "parseData has been run
"; return($body); } 1; # so the require or use succeeds #### # this is just a test file to see if my new module is functioning properly use HTML::pageParser; #we need to make sure that we enter the path to our active directory here $sysLoc = "E:\\test\\"; print "Content-Type: text/html\n\n"; # here are just some test tokens to make sure that this works $myPage = pageParser->new($sysLoc); $tokens{fName} = "Brandon"; $tokens{lName} = "Smith"; $data = $myPage->loadAndParse("parseThis.txt",%tokens); print $result;