Reputation: 185
Problem : I want to have a list of objects stored so that i can call the corresponding methods at latter point in time
my @tc = ("TC_1","TC_2");
my %obj_list = ();
foreach my $test (@tc) {
$obj_list{$test} = Test->new($test);
}
In the same module file at latter stage where i need to call the corresponding methods of those objects
foreach my $test (keys %obj_list) {
if (some specific condition is satisfied for a test) {
1 --> $obj_list->$test->action();
2 --> $obj_list{$test}->action();
}
}
I tried 1 and 2 and they are not working. Could some one tell me what i could be doing wrong here.Any inputs would be of great help.
Upvotes: 0
Views: 162
Reputation: 69224
Your code is basically correct - other than a few syntax errors.
# Use ( ... ) to initialise an array.
my @tc = ("TC_1","TC_2");
my %obj_list = ();
foreach my $test (@tc) {
$obj_list{$test} = Test->new($test);
}
foreach (keys %obj_list) {
if (some specific condition is satisfied for a test) {
# This version is incorrect
# $obj_list->$key->action();
# This version will work, except you have the
# key in $_, not $key.
$obj_list{$_}->action();
}
}
Adding use strict
and use warnings
to your code would have helped you find some of these problems.
Upvotes: 3