in reply to Double quoted string as method call

Update As other people below be have mentioned, the code I posted here doesn't work at all. That's what happens when I post just before leaving work. My code does work with a hashref, but does nothing for a method call. D'oh.

I'm going to take a stab at this and say that you need braces around the call, like so:

$caller->{"database_${_}"}()

I haven't actually tested this, but it compiles cleanly. More knowledgeable monks will probably have a better solution and explanation.

Replies are listed 'Best First'.
Re: Re: Double quoted string as method call
by BUU (Prior) on May 04, 2004 at 22:05 UTC
    Unfortunately your idea doesn't work, at least on 5.8.4 on debian or 5.8.3 on win32. I think this is just an example of the constructs getting too complicated for the perl parser to handle, but I'm not sure exactly why. The work around is simple:
    my $base = 'foo_'; my $obj = new obj; for(qw/baz qux stuff/) { my $meth = "${base}_$_"; $obj->$meth; }
    Which works perfectly.

    I find it a little odd that the $obj->"meth" form doesn't work, since the reverse works perfectly, at least for packages: "package"->$meth
Re: Re: Double quoted string as method call
by ryantate (Friar) on May 04, 2004 at 22:02 UTC

    Pardon, but it appears you are selecting a value from a dereferenced hash rather than making a method call on an object or class. No?

    The latter is my goal, not the former. Perhaps I should have been clearer!

    Thanks anyway.