murphyjason170
murphyjason170

Reputation: 3

Perl - create objects but how to get at the individual object?

I create objects but then don't know how to easily use them once they are created (do set/get and run methods). The code below works but then I cannot get at the object to use it How do I do this?

# My array of coffeecup names
my @coffeeCupNames = ("Espresso", "Sumatran", "Java");

# Create an array to hold my objects once they are created
my @objects = ();

# Create a new coffee cup object with the name of the coffee cup
foreach (@coffeeCupNames)
{
    push @objects, new virtualCoffeeObject("$_");
}

# How do I get at the Espresso coffee cup object?

Upvotes: 0

Views: 83

Answers (4)

Daniel Böhmer
Daniel Böhmer

Reputation: 15411

I agree with all hints the other answers give but I don't see why nobody posted the obvious solution to keep track of the created objects.

Of course you can store your objects in dedicated scalar variables:

my $espresso = VirtualCoffee->new("Espresso");
my $sumatran = VirtualCoffee->new("Sumatran");
...

As you draw your coffee types from an array you'll probably not want to use fixed variable names for all your coffees. The tool of choice for this is a hash in Perl.

my @coffee_cup_names = ("Espresso", "Sumatran", "Java");

# create a VirtualCoffee object and store it with its name as hash key
my %coffees = map {$_ => VirtualCoffee->new($_)} @coffee_cup_names;

# how to get the objects:
my @coffee_sorts = keys   %coffees;
my @all_coffees  = values %coffees;
my $espresso = $coffees{Espresso};

To learn more about hashes in Perl, I recommend the Modern Perl "book" (read online).

I allowed myself to adapt your variable names to common Perl style.

Upvotes: 2

Sinan Ünür
Sinan Ünür

Reputation: 118166

First of all, keep in mind that Perl is not Java.

So, as appealing as it looks to you, do not use new Class. That is called indirect object notation. It looks cute and familiar, but it will bite you.

I am assuming virtualCoffeeObject is class which has accessors for the coffee type it represents. Did I mention, Perl is not Java?

Let's say you have the following barebones class:

package My::Coffee;

sub new {
   my $class = shift;
   bless { name => $_[0] } => $class;
}

sub name {
   my $self = shift;
   $self->{name};
}

And, given the names below:

# My array of coffeecup names
my @names = qw(Espresso Sumatran Java);

You want to create an array of My::Coffee objects with the corresponding names. In Perl, you would do:

 my @coffees = map My::Coffee->new($_), @names;

How do I get at the Espresso coffee cup object?

There is no reason to presume there is only a single My::Coffee instance with name Espresso:

 my @espressos = grep $_->name eq 'Espresso', @coffees;

You can invoke methods on the elements of either array:

 say $_->name for @coffees;

or

 say $_->name for @espressos;

Did I mention Perl is not Java?

Upvotes: 2

choroba
choroba

Reputation: 242218

Your objects are members of the array.

my $o = $objects[0];
$o->method(@args);

or, shortly:

$objects[0]->method(@args);

Upvotes: 2

beasy
beasy

Reputation: 1223

first note that best practices are to call the "new" method (and all methods) with -> instead of like a subroutine, i.e., $object = Class->new();

for (@objects) {
   my $object = $_;

   #call methods on the object
   $object->method();

   #assign vars to method call result
   my $var = $object->method();

   #access the objects attributes directly (not advised)
   $var = $object->{attribute};
}

Upvotes: 0

Related Questions