--- Nicole Seitz <[EMAIL PROTECTED]> wrote:
> Hi there!
> 
> I've just started to learn OOP with Perl.With the help of perltoot I wrote a 
> class Person and some methods(see below).

'perltoot' is great, but there are a few things that can be done differently.  
Admittedly, it's
aimed at beginners, so I can't take *too* much issue with it :)

First, here's what I think is a cleaner way of writing the constructor:

sub new {
        my $class = shift;
        my %self = map {$_ => undef} qw(CNAME SNAME STREET CITY PHONE EMAIL);
        return bless \%self, $class;
}

Note how that compares to the following:

> sub new {
>     my $self = {};
>     my $proto = shift;
>     my $class = ref($proto) || $proto;
>     $self->{CNAME}           = undef;
>     $self->{SNAME}           = undef;
>     $self->{STREET}          = undef;
>     $self->{CITY}            = undef;
>     $self->{PHONE}           = undef;
>     $self->{EMAIL}           = undef;
> 
>     bless ($self, $class);
>     return $self;
> }

The "ref($proto) || $proto" line, unfortunately, should *not* be there.  All it does 
is allow you
to call a constructor on an instance of an object.  Typically, that's useful if you 
want to clone
an object (create a new object with the same properties), but that's not being done 
here and
should be left out.

> sub cname {
>     my $self = shift;
>     if (@_) {$self->{CNAME} = shift }
>     return $self->{CNAME};
> }

[snip]

> To store some data in my object I did the follwing:
> 
> use Person;
> 
> $myPerson = Person->new();
> $myPerson->cname("John");
> $myPerson->sname("Smith");
> $myPerson->street("Euston Road");
> $myPerson->city("London");
> $myPerson->phone("414 3344");
> 
> Do I really need all these methods?

Probably.  You can rewrite your constructor to handle these properties, but later if a 
person
changes their phone number, you'll want a way to change it.  Another way to write the 
constructor
is to assign the arguments when you run the program:

  sub new {
    my $class = shift;
    my %self = map { $_ => @_ ? shift : undef } 
      qw(CNAME SNAME STREET CITY PHONE EMAIL);
    return bless \%self, $class;
  }

That's bad, though, because there's no data validation.  It's just an example.  Change 
the
constructor as above and you can test it:

  use Data::Dumper;
  $myPerson = Person->new(
    'John',
    'Public',
    '123 Main Street',
    'Portland',
    'Oregon',
    '[EMAIL PROTECTED]'
  );
  print Dumper $myPerson;

In reality, I would use named arguments to the constructor and test them for validity 
before
assigning them.  They should be tested for validity in the constructor, though, and 
*not* in your
code that calls them.  That way, when you reuse the object, you don't need to worry 
about whether
or not you forgot to test your args.

Cheers,
Ovid

=====
"Ovid" on http://www.perlmonks.org/
Web Programming with Perl:  http://users.easystreet.com/ovid/cgi_course/
Silence Is Evil: http://users.easystreet.com/ovid/philosophy/decency.txt

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to