SPUG:Object property question

Skahan, Vince vince.skahan at boeing.com
Thu May 1 11:36:54 CDT 2003

I did a project a number of years ago where all that
was necessary was to follow the example at p298 in the
perl5 (blue) camel book on  Method Autoloading.


The basic idea was to have a class that looked like the
characteristics of a majordomo mailing list from the
administrative side (list name, description, moderators, etc.)
but the idea's the same.

---------- Vince.Skahan at boeing.com ---------
   Connexion by Boeing - Cabin Network

-----Original Message-----
From: Jeremy Kahn [mailto:kahn at cpan.org]
Sent: Wednesday, April 30, 2003 7:31 PM
To: Jay at scherrer.com
Cc: Dan Ebert; Seattle Perl Users
Subject: Re: SPUG:Object property question

Jay, Dan --

don't forget that you want to build for the future. You may want to 
subclass Member someday (temporary members, super-members, moderators, 

Jay Scherrer wrote:

>I would do something like:
>package Member;
>sub new
>my $this = {};
>    bless $this;
>    return $this;
I would rather do it like this:

package Member;

# take the type as an argument, so that this method can be inherited.
# this can be more compact, but it works and is clearer
sub new {
  my $class = shift;  # $class will be 'Member' (but *not* when you 
subclass it)
  my $instance = bless ({}, $class);
  $instance->init(@_); # any remaining arguments should be passed to init
  return $instance;

>sub init_newMember
>my $this = shift;
>$this{id} = shift;
>$this{FirstName} = shift;
>$this{LastName} = shift;
# we know it's a Member now, but this method might be used for a subclass
# too, so let's just call the method 'init'
sub init {
  my $self = shift;
  my ($id, $fn, $ln) = @_;
  $self->{'Member_id'} = $id;
  $self->{'Member_firstname'} = $fn;
  $self->{'Member_lastname'}  = $ln;


>Then in your script:
>$member = Member::new();
>$id = 0;
>$firstName = "John";
>$lastName = "Doe";
>$member->Member::init_newMember($id, $firstName, $lastName);
No! This method of calling new() can't be inherited -- you're naming the 
package explicitly, but you'd like to leave yourself the option to 
re-implement Member so that it borrows a 'new' method from some *other* 
parent, someday (this is called "refactoring" and is very hip :-/).

Furthermore, with the new() as I suggested (calling the init() method 
from within itself) you can pass the arguments right in:

my ($id, $fn, $ln) = (0, 'John', 'Doe');
$member = Member->new($id, $fn, $ln);
# isn't that pretty? $member is all done and ready to be used; no need 
for a second call.

The key to the object->method() syntax is to remember that when a method 
is called like this, it passes the object as an implicit first argument 
(it's unshifted onto @_).

This applies even when it's a literal (like a classname) which is why we 
get away with Member->new(@args):
the method Member::new is called with the argument list ('Member', @args).

Note also that if Member::new isn't found, then it checks to see if 
@Member::ISA has any entries; if it does, it searches those ancestor 
classes recursively.

This is all *very* powerful, and kinda hacked together. I strongly 
recommend The Damian's book "Object Oriented Perl" -- the first five or 
six chapters are a great introduction to this subject.

>On Wednesday 30 April 2003 03:24 pm, Dan Ebert wrote:
>>The Setup:
>>I'm working on a module to manage a membership database.  I have it set
>>up so that there is a Member object with properties you can access like
>>my $firstname = $member->firstname();
>>and change with
>>$member->firstname('new first name');
>>when a new member is created:
>>$member->initialize(firstname => 'John',
>>                    lastname  => 'Doe');
>>I insert the data into a table and get the member ID from an
>>auto-incrementing field.
>>The Question:
>>I want to set a new property of the Member object (the ID).  So that you
>>can retrieve the ID with
>>$id = $member->id();
>>In my module I tried:
>>$member->{id} = $id
>>but that didn't work.
>>The only thing I've gotten to work is to return the object ... so that
>>you call the create method like this:
>>$member = $member->create();
>>which seems a little awkward.
>>Is there a better way?
>Seattle Perl Users Group Mailing List  
>POST TO: spug-list at mail.pm.org
>ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
>MEETINGS: 3rd Tuesdays, U-District, Seattle WA
>WEB PAGE: www.seattleperl.org

Seattle Perl Users Group Mailing List  
POST TO: spug-list at mail.pm.org
ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
MEETINGS: 3rd Tuesdays, U-District, Seattle WA
WEB PAGE: www.seattleperl.org

More information about the spug-list mailing list