APM: OO Perl Question

Mike South msouth at fulcrum.org
Fri Jun 28 00:00:36 CDT 2002

>To: austin-pm at pm.org
>From: dbii at mudpuddle.com (David Bluestein II)
>Subject: APM: OO Perl Question
>Date: Thu, 27 Jun 2002 08:49:36 -0500
>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
>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;

Hi David,

I'm not sure if what I'm doing will help, but I thought I would 
try to create a minimal implementation of what I think you 
are trying to do.

I have created an Object.pm class which has an array that is 
visible anywhere in the Object class.  Although you could 
address this array from outside the class, you are correct
in not wanting to do this--access should be through the 
defined interface in case the underlying implementation 
changes, yadda yadda yadda.

So, you create an instance of an Object, and you use a 
method to set the values for the list, and then any other
instance of an Object can see those values, the methods
of Object can see those values, etc.

Now, as I understand your situation, I could have
made this example one step simpler by 
simply hardcoding the values of LIST in Object.pm
rather than creating a method to set those values.
Hopefully that will not confuse the issue too much.

So, here is the code that uses the Object class:

======== begin use_Object.pl ==============
#!/usr/bin/perl -w
use lib '.';
use Object;

# make an Object
my $obj1 = new Object;

# set the values of LIST in the object class.  Note
# that if we change the name of LIST in Object.pm
# to GLOBAL_FIELDS, this code will not have to
# change.  Also, note that you may not need
# this particular capability in your application
# if the values of LIST never need to be changed
# on the fly.
$obj1->set_Object_list_vals( 'foo', 'bar', 'baz');

# make another Object to be sure it works
my $obj2 = new Object;

# obtain the values of the Object package's LIST array
# through our second Object
# Once again, this code will not have to change if
# you change the name of LIST to something else.  Only
# the method "get_Object_list_vals" has to know the
# details of where the values are stored.  Later
# they might be in a config file on the system or
# retrieved from a database
my @list = $obj2->get_Object_list_vals;

# print them out
print "@list\n";
========== end of use_Object.pl ==============

Ok, here's the file that defines that class.

========== beginning of Object.pm ============
package Object;
use strict;

# here's the array.  You may want to actually 
# assign values here in your application
my @LIST = ();

# i just used a generic new and returned an
# empty hash to make things uncomplicated
sub new {
	my $proto = shift;
	my $self = {};
	return bless $self, ref($proto) || $proto;

# here is the method that sets the 
# values of LIST.  you might not need this
sub set_Object_list_vals {
	# try commenting out this line and
	# see what happens
	my $self = shift;
	@LIST = @_;

# here's the ticket to not having @Object::LIST
# referred to directly in the code that uses this
# class.
sub get_Object_list_vals {
	return @LIST;
========== end of Object.pm ============

If you put these files next to each other and run use_Object.pl
you should see "foo bar baz" as the output.


More information about the Austin mailing list