APM: OO Perl Question

Hrunting hrunting at texas.net
Fri Jun 28 10:26:42 CDT 2002


On Thu, 27 Jun 2002, David Bluestein II wrote:

: 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;

package Object;

use constant LIST => qw| a b c d |;


sub new {
  my ( $class, %opts ) = @_;
  # do work, bless object, etc.
}

sub list { return LIST; }


package main;

my $object = new Object( 'ID' => $q->param('id') );
my @list = $object->list();


I'm tempted to say that you can also just do 'my @list =
$object->LIST;' but I think that constant prototypes the 'LIST'
function to be (), which means perl should throw an error when the
object is passed in as the first argument.

In any case, the cleaner method in this case is to setup the class or
object method and have the class or object method return the array.




More information about the Austin mailing list