Calling methods with array variable not allowed!
August 13, 2009
File this under….
Didn’t you know that you can’t do that!
Here’s an example of something I tried the other day and was surprised to find didn’t work:
{ package Baz; use Moose; sub a { 'A' } sub aa { 'AA' } } my $baz = Baz->new; my @methods = qw/a aa/; # OK just to show u that I can call method with normal $a variable my $a = $methods[0]; say $baz->$a; # 'a' # but when I use an array variable instead it wont compile say $baz->$methods[1]; #! syntax error .... near "$methods["
Bit surprising that? The same also happens for a hash variable.
I assume there is a very good reason why the perl parser won’t compile this.
If you want to avoid having to copy into scalar beforehand (as in $a in above) then one (perhaps only?) solution is:
say $baz->${ \$methods[1] }; # 'AA'
/I3az/
PS. Only thing I could find on this was this Perlmonks post:
3 Comments
leave one →
BTW to understand whats going on in the line
$baz->${ \$methods[1] };
have a look at mst wonderful post madness with methods/I3az/
$baz->$_(23) for $methods[1];
would be another (IMO more readable) alternative.
Yes I did play around with this originally but I need it to work in an expression 😦
Here’s a tailored snippet of what I would have liked to have wrote:
my ( $from, $to ) = do {
if ( $baz->$methods[0] ) { reverse @methods }
elsif ( $baz->$methods[1] ) { @methods }
else { die "Invalid option\n" }
};
Of course that wouldn’t work so this became:
my @methods = qw/x y/;
my ( $from, $to ) = do {
my ( $first, $second ) = @methods;
if ( $baz->$first ) { reverse @methods }
elsif ( $baz->$second ) { @methods }
else { die "Invalid option\n" }
};
/I3az/