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

OK, so I am a PHP programmer, and I am just now learning Perl. I am trying to create my first module, yet I am having some trouble. I'm still trying to get a grasp on OOP in Perl. The following is the basis of my module:
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<br>"; 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); $debug .= "loadFile has been run<br>"; 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; } } $debug .= "parseData has been run<br>"; 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<br>"; return($body); } 1; # so the require or use succeeds
Here's the test code i wrote that 'use's the module.
# this is just a test file to see if my new module is functioning prop +erly use HTML::pageParser; #we need to make sure that we enter the path to our active directory h +ere $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;
When the test code is executed, it returns absolutely nothing... I am assuming that I am missing some really basic concept. But I have been pulling out my hair trying to figure it out, and am now turning to the wise ones for assistance. ANY fixes, suggestions or remarks are quite welcome. Thanks.

Replies are listed 'Best First'.
Re: Perl Modules
by kvale (Monsignor) on Apr 23, 2004 at 20:38 UTC
    A couple of comments:
    • OO modules generally don't use Exporter, as the methods are attached to objects, rather than being used as general subroutines:
      package HTML::pageParser; use warnings; # could take this out in production code use strict; use Carp; use vars qw($VERSION);
    • Unless you want to explicitly make new a copy constructor as well, just say
      my $proto = shift; my $class = $proto; my $sysLoc = shift; my $self = {}; $self->{tokens} = []; $self->{sysLoc} = $sysLoc; bless ($self, $class); $debug .= "new class has been run<br>"; return $self;
      where I have fixed the tokens hash key and gooten rid of @orig_args, as you don't use it.
    • It is traditonal to denote filehandles using capitals (THIS vs this) to avoid collisions with lowercase subroutines and keywords.
    • In the constructor, you create $self->{tokens} as an anonymous array, but in ParseData, you assign to a hash. You instead probably want something like
      $self->{tokens} = {}; # in new() my %tokens = %{ $self->{tokens} }; # in ParseData()

    -Mark

      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.
Re: Perl Modules
by Belgarion (Chaplain) on Apr 23, 2004 at 20:19 UTC

    One thing I see quickly is that your pass your hash of tokens into loadAndParse, but that function does not pass them to any other function, nor does it set the $self->{tokens} attribute. Basically, the rest of your functions have nothing to work on. I don't think this will solve all the problems though.

    Update:
    Also, in your new function, I believe you have your %tokens call defined incorrectly. It looks like you want $self->{tokens} to be a hash containing the token as the key, and it's replacement as it's value. You would need something like:

    sub new { ... $self->{tokens} = \%tokens; ... }
    This would define the token attribute of $self to be a hashref containing your mapping of token names to token values. However, rather than relying on %tokens being defined globally, I would recommend that you pass it into either your constructor, or propagate it through your parseData function.

    Update II:
    You could also do away with the $self->{tokens} part of your module all together. The tokens are only used in one place throughout your module---the parseData function. So you could change the code to:

    sub parseData($) { my $self = shift; my ($data, %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<br>"; return($data); }
    and change the loadAndParse function to:
    sub loadAndParse($%) { my $self = shift; my ($filename,%tokens) = @_; $data = loadFile($filename); $body = parseData($data, %tokens); $debug .= "parseData has been run<br>"; return($body); }

      BTW, prototypes are useless on methods (perl doesn't even check them)
Re: Perl Modules
by eric256 (Parson) on Apr 23, 2004 at 20:20 UTC

    Add use strict; use warnings; to the top of your test code and then run it. It appears that you should be doing $myPage = HTML::pageParser->new($sysLoc); Your also printing $result which i don't see anywhere else in your program.


    ___________
    Eric Hodges
Re: Perl Modules
by Belgarion (Chaplain) on Apr 23, 2004 at 20:39 UTC

    In addition to my previous comment, you seem to be reinventing the wheel here. Why not look into HTML::Template or Template? There are other templating modules in CPAN as well.

      As I said, I am a PHP programmer and I am forced to recode alot of my existing codes in Perl. I need to know how to code OOP in Perl, as well as a few other techniques, as I will be converting complex PHP scripts. I realize that this is a very simple code; what better way to start learning perl?? I still wouldn't learn a thing if i just went and used the other template modules.
        For most parts, Perl is very much like PHP. You can easily make 90% of most PHP code work in Perl with only trivial changes, such as splitting up PHP's arrays into Perl's arrays and hashes. Note that an array in PHP is like a Perl's array- and hash-refs, not plain arrays/hashes. And you'll have to translate those built-in keywords in PHP without any equivalent in Perl, into calls into modules.

        OO is one area where Perl and PHP are quite different. For a very gentle intro to Perl OOP, check out perlboot.