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


i create objects don't know how use them once created (do set/get , run methods). code below works cannot @ object use how do this?

# array of coffeecup names @coffeecupnames = ("espresso", "sumatran", "java");  # create array hold objects once created @objects = ();  # create new coffee cup object name of coffee cup foreach (@coffeecupnames) {     push @objects, new virtualcoffeeobject("$_"); }  # how @ espresso coffee cup object? 

i agree hints other answers give don't see why nobody posted obvious solution keep track of created objects.

of course can store objects in dedicated scalar variables:

my $espresso = virtualcoffee->new("espresso"); $sumatran = virtualcoffee->new("sumatran"); ... 

as draw coffee types array you'll not want use fixed variable names coffees. tool of choice hash in perl.

my @coffee_cup_names = ("espresso", "sumatran", "java");  # create virtualcoffee object , store name hash key %coffees = map {$_ => virtualcoffee->new($_)} @coffee_cup_names;  # how objects: @coffee_sorts = keys   %coffees; @all_coffees  = values %coffees; $espresso = $coffees{espresso}; 

to learn more hashes in perl, recommend modern perl "book" (read online).

i allowed myself adapt variable names common perl style.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -