in reply to Perl Modules

A couple of comments:

-Mark

Replies are listed 'Best First'.
Re: Re: Perl Modules
by eNtropicChild (Initiate) on Apr 23, 2004 at 21:10 UTC
    OK, i re-worked the code a lil bit, and it's still not displaying anything except for the errors with the token vars in the test script. Here's a new copy of the module code:
    package HTML::pageParser; use strict; use warnings; use Carp; use vars qw($VERSION); # this creates our class/object sub new { my $proto = shift; my $class = ref($proto) || $proto; my ($sysLoc) = my @orig_args = @_; my $self = {}; $self->{tokens} = undef; $self->{sysLoc} = $sysLoc; bless ($self, $class); 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 = <this>) { $body .= $line; } close this; } else die("cannot open the file:" $filename); return($body); } # this function parses through the contents of a scalar variable and d +oes a search/replace on the tokens # the only thing left to figure out in this function is to eliminate t +he duplicate matches found # before doing the replacing of any tokens. This will be a minor decre +ase 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; } } 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) = @_; $data = loadFile($filename); $body = parseData($data); return($body); } 1; # so the require or use succeeds
    Here is the code for the tester script.
    # this is just a test file to see if my new module is functioning prop +erly use HTML::pageParser; use warnings; #we need to make sure that we enter the path to our active directory h +ere $sysLoc = "E:\\webs\\test\\brandon\\"; #print "Content-Type: text/html\n\n"; # here are just some test tokens to make sure that this works $myPage = pageParser->new($sysLoc); my $myPage->{$tokens{fName}} = "Brandon"; #definite problem here, dunn +o how to format this my $myPage->{$tokens{lName}} = "Smith"; #definite problem here, dunn +o how to format this $data = $myPage->loadAndParse("parseThis.txt"); print $data
    Thanks!
      perhaps somebody can explain to me why absolutely NOTHING prints to the screen... even if there are no values defined in the tokens hash, it should still parse the html file and return the contents. even if i use print statements in the module... nothing... I feel there is some very basic logic that i am missing here.

        Well, for one think you didn't follow the suggestions by kvale. Also, your loadFile function is wrong. Try this instead:

        # this file reads a text file and loads the contents into # an array sub loadFile($) { my $self = shift; my ($filename) = @_; open (FILE, "<$filename") or die("cannot open the file: $filename" +); while (<FILE>) { $body .= $_; } close(FILE); return($body); }

        Update:
        Also, your calls to HTML::pageParser are incorrect. They should read:

        my $myPage = HTML::pageParser->new($sysLoc); $myPage->{tokens}{fName} = "Brandon"; #definite problem here, dunno ho +w to format this $myPage->{tokens}{lName} = "Smith"; #definite problem here, dunno ho +w to format this $data = $myPage->loadAndParse("parseThis.txt");
        See if this makes a difference.