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'

/I3az/

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

Advertisement
4 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

    /I3az/

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

      /I3az/

      • February 10, 2012 9:00 pm

        I aridme your mindset and your researching multiple methods. You bring up some great points.If I were you, though, I’d cut out the Wrecking Balm step. It’s expensive enough, and you’d be better served if you used that money toward a laser treatment. It’s rare to find someone that can ACTUALLY give good advice regarding Wrecking Balm, because as you said, many people quit before they get very far. So, you’re using it ASSUMING that it’ll work if used as directed for a long period of time. You don’t actually know if it will work. However, laser is GUARANTEED to work, so it would be a wiser investment, imho.I aridme that you’re taking the removal process slowly, too, but remember that you typically have to wait a few weeks/months in between laser treatments, anyway. Say the place you go recommends to wait 10 weeks in between laser treatments. I don’t know what your tattoo is of, but if it’s pretty intricate w/ various colors it may take multiple sessions to get it completely gone. So, if you do 6 sessions, for example, that means your tattoo will be gone in over a year!Good luck w/ whatever you decide to do.

Leave a Reply

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

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.