Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List of Objects in Perl?

Status
Not open for further replies.

hagbardc

Programmer
Mar 13, 2002
4
US
So, I coded a package which describes an object, and I'm having a couple of issues. Primarily, I want to know if I can make a list, each entry of which is an object (or a reference to an object). i.e.:

for ($i=0; $i<10; $i++) {
$list[$i] = Object->new();
}

Is this valid? And if so, how do I access the data this object later on? I'm relatively new to perl and using references in general.

Also, in each of these objects, I have an entry in an anonymous hash which itself is a list (of scalars). This seems to work, but it definitely seems a little shady to me. The new method looks something like:

sub new
{
my $Class = shift;
my $self = {};

$self->{Direction};
bless $self, $Class;
}

Where $self->{Direction} is an array of scalars. Once I have the list of objects, how do I then access an element in the {Direction} list of an element in the list of objects?

Any info anyone could give me on this would be greatly appreciated. TIA -

hagbardc
 
Your first question, I would do this in two steps:

for ($i=0; $i<10; $i++) {
my $new_obj = Object->new();
$list[$i] = \$new_obj;
}

The 'my' will ensure a new reference is created each time.

Then, however, I'm struggling. Have you tried calling these objects like this...?

for ($i=0; $i<10; $i++) {
$list[$i]->SomeMethod;
}

I'm only just starting to use references... goBoating? Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top