APM: RE: OO Perl Question
Mando Escamilla
mando at mando.org
Fri Jun 28 08:41:09 CDT 2002
Well, you could do something like this:
package Object;
sub new {
my $class = shift;
my $self = {};
# ID that you sent in via instantiation
my %ARGS = shift;
my $id = $ARGS{ID};
$self->{ID} = $id;
# Database fields
my @LIST = qw ( first_field second_field third_field );
$self->{LIST} = \@LIST;
# Return blessed object
return bless($self, $class);
}
Now, when you instantiate your object thusly:
my $obj = Object->new( ID => $id );
$object will have two components: ID and LIST. You can access them like
this:
my $id = $obj->{ID};
my $list = $obj->{LIST};
Here, ID is a scalar and $list is an array reference.
Hope this helps,
--
Mando
-----Original Message-----
From: austin-admin at mail.pm.org [mailto:austin-admin at mail.pm.org]On
Behalf Of David Bluestein II
Sent: Thursday, June 27, 2002 9:50 AM
To: austin-pm at pm.org
Subject: APM: OO Perl Question
Okay, I've been coding too much this week and I've got an OO Perl question
as I'm trying to expand my horizons (now that I kind of understand $self
:).
In my object, I want a list, @Object::LIST, which does not change during
runtime (it contains a list of fields in a database). In my main program, I
create an OBJECT:
my $id = $q->param('id');
# create object
my $object = new Object( ID => $id );
# create reference to the list of fields
my $listref = \@Object::LIST;
I want to make this simpler though, and not involve the @Object::variable
syntax.
Questions are:
1) How do I define an @LIST in my Object.pm, and how do I construct the
syntax to access it in my main program, such that I can change the above
code to:
my $id = $q->param('id');
# create object
my $object = new Object( ID => $id );
# Set the variable @list equal to the @LIST in the $object. Syntax is wrong
here (I tried it)
my @list = $object->@LIST;
Thanks-
David
----------
David H. Bluestein II President & Lead Developer
dbii at mudpuddle.com ii, inc.
http://www.interaction.net
- Specializing in Designing Interactive Websites -
- and Searchable Internet Databases -
_______________________________________________
Austin mailing list
Austin at mail.pm.org
http://mail.pm.org/mailman/listinfo/austin
More information about the Austin
mailing list