Skip to content

Calling methods with array variable not allowed!

August 13, 2009
tags: ,

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'


PS. Only thing I could find on this was this Perlmonks post:

About these ads
3 Comments leave one →
  1. August 13, 2009 4:18 pm

    BTW to understand whats going on in the line $baz->${ \$methods[1] }; have a look at mst wonderful post madness with methods


  2. mxf permalink
    August 14, 2009 7:44 am

    $baz->$_(23) for $methods[1];

    would be another (IMO more readable) alternative.

    • August 14, 2009 3:53 pm

      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" }


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: