Hi Siegfried,

Somebody hopefully will provide the appropriate perldocs to consult and 
correct me if I'm wrong.

Am Sonntag, 1. Mai 2005 06.34 schrieb Siegfried Heintze:
> I'm trying to write a screen scraper and I need to use inheritance
> (according to a response to an earlier post).
>
> Here is the original piece of code:
>   my $parser = HTML::Parser->new(api_version => 3);
>
> Now how do I change this so it is creating an instance of my custom class?
> Here is my attempt to make a new class:
>
>
> package CrawlCompanyJobPage;

add:

 use strict;
 use warnings; 

 use HTML::Parser; # import base class

> our @ISA = HTML::Parser;

@ISA is an _array_ and its contents class _names_, so the line must be

 our @ISA=('HTML::Parser');

or shorter, using the pragma class 'base':

 use base 'HTML::Parser';

> sub new {
>
>   my $invocant = shift;
>   my $class = ref($invocant) || $invocant;

Above lines (intention: object creation possible with class constructor or 
from an instance) is deprecated for some people (including me) because it's 
not always clear if (potentially complex) attributes are (or should be) 
copied shallow or deep.

Instead, you should maybe just use

 my $class=shift; # class name as string
 # die "use class constructor" if ref($class); # eventually.
 my [EMAIL PROTECTED]; # additional (attrname, attrvalue) pairs

>   my $self = {  @_ };

Your $self must be derived from the base class, the above line should be (2nd 
line follows):

 my $self=$class::SUPER->new(%params);

(this way, the object is initialized from the base (or super) class. To 
determine which that is, @ISA is inspected)

Now, you add the additional attributes specific to your class (to avoid using 
attributes with the same name (but different semantics) as in the base class, 
you have to know the attribut layout of the base class).
   Here is a short and potentially incorrect way without checking the 
additional params, without providing default values, and without making the 
difference between actual/base class attributes.

 $self={%$self, %params};

(the whole inheritance thing here is based on the base class being a perl 
hash, which is mostly, but not always the case)

>   bless($self, $class);
>   return $self;

Can be shortened:

 return bless $self, $class;

> }

I suppose you define some subclass methods here?

> I don't think I have the ISA statement correct.
> How do I pass api_version=>3 along to my ancestor?

my $obj=CrawlCompanyJobPage->new(api_version=>3);

It is handled by the base class.

> Thanks,
> Siegfried

Hoping its helpfully
joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to