APM: OO Perl Question

Brian Clarkson brian_clarkson at yahoo.com
Fri Jun 28 02:02:30 CDT 2002


--- David Bluestein II <dbii at mudpuddle.com> 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;
> 

so the code you're using in the Object package is something like this, i
assume:

package Object;

use vars ( @LIST );
@LIST = qw/ foo bar baz /;

sub new {

   my ( $proto, %args ) = @_;
   my $class = ref( $proto ) || $proto;
   my $self  = {};

   bless $self, $class;

   $self->{_ID} = $args{ID};
   return $self;

}

## whatever other behavior you have ... 


> 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');

construction of class variable given above ...

use vars ( @LIST );
@LIST = qw/ foo bar baz /;

you can access it in any number of ways.  one of the 'downfalls' of OO
Perl is that encapsulation *can* be difficult to ensure ... Damian
Conway's OO Perl book has a few ways to do it ( in standard Perl fashion )

accessing the list could be as simple as copying it into your object (
once instantiated ).  so, in the constructor:

   $self->{_LIST} = \@LIST;

would give each object a private copy.  

accessing the list ( read-only ) would just look like this ( from the
calling program ):

my $obj= Object->new( ID => $id );
my $list = $obj->{_LIST};  ## no accessor written, not best idea

# with accessor giving back arrayref:

my $list = $obj->list();

and the accompanying accessor ( in Object.pm )

sub list {
 
    my $self = shift;
    return $self->{_LIST};

}


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

can't do the double variable interpolation here because there's nothing to
interpolate.  

even if you have a class variable, you can get to it like this:

my $obj = Object->new( %args );
my @list = $obj::LIST;

remember that @LIST is *blessed* into the package namespace ... 

+++

it's a bit late, and my brain's not on at 100% ... but the code above
should work ... i'm just not sure of the original intention ...

brian

=====
"When it's good, it's really good; and when it's bad I'm not abusive."
     --- David Bowie, "Candidate", _Diamond Dogs_

"Just remember, technological innovation means class war." 
	--- acceptance speech at the 2000 Webbys

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com



More information about the Austin mailing list