I have been using Perl for a while but is bothered by one syntax problem. In some packages, a method can return an array. For example
$root->getlist();
Will return an array. Now I want to get the first element of the result. Of course I can do this:
my @results = $root->getlist();
if($results[0] =~ /wow/) {
print "Qualified result";
}
However, this is very troublesome. Is there a way that I can combine the first line with second line? I tried this but failed.
if(${$root->getlist()}[0] =~ /wow/) {
print "Qualified result";
}
Is there a way to do this quick?
A better example: Consider this following package:
package Try;
sub new {
my $package = shift;
return bless({}, $package);
}
sub getList {
return (1,2,3,4,5);
}
1;
Now I have a user pl file like this:
use lib '.';
use Try;
use strict;
my $obj = Try->new();
print ($obj->getList())[0];
Trying to run this script will result in:
syntax error at perlarrayaccess.pl line 6, near ")["
Execution of perlarrayaccess.pl aborted due to compilation errors.
print. Tryprint $obj->getList()[0];without the extra parenthesis and see if it works.use strict, you shoulduse warnings. If you did, you'd see that you are trying to callprintas a function in the last example.