in reply to Re: Re: $var as accessor name?
in thread $var as accessor name?

If the method name is a variable, it does have to be a simple scalar (e.g. $obj->$hash{method_foo} won't work).

Replies are listed 'Best First'.
Re: Re: Re: Re: $var as accessor name?
by BrowserUk (Patriarch) on Mar 12, 2004 at 05:32 UTC

    That's not strictly true, though it is messy.

    { package test; sub new{ return bless [], $_[0]; } sub meth1{ print 'meth1'; } }; $t=test->new; $t->meth1; meth1 $h{x}='meth1'; $t->${ \$h{x} }; meth1

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      Shhh...the newbies are listening.

      More seriously, I very occasionally do that to interpolate an expression in a qq string where that makes the code clearer, but even if I did a lot more OO than I do, I don't think I'd ever do it for method names. It's so much clearer to use a temporary scalar. But I guess it depends on what you are used to seeing.