in reply to Re: Calling a subroutine from a scalar
in thread Calling a subroutine from a scalar

Well, I left you neutral, but I suspect it's because one can use eval, and that doing what the poster is asking needn't be "deep magic" or responded to with what was essentially "you don't want to do that" without even a brief explanation of why.

For the record, eval could be used thus:

my $f = 'subroutine'; eval "$f"; sub subroutine { print "In sub!\n"; }
You are right to say that there are many, many reasons why one doesn't want to use eval this way[0], and that there is probably a better solution. However, I would say that the OP simply didn't provide enough information about what (s)he is trying to accomplish for us to provide useful advice on what approach could prove safer and more efficient.

[0]: A few reasons, from the top of my head:

  1. Not strict-safe: the name of the func is a bareword string when eval'd
  2. Evaling a string without knowing what is in it is a security risk -- and if you know what's in it, a dispatch table or some similar structure is probably a better choice anyhow
  3. Evaling a string is slow (evaling a block isn't, compartively)

Yoda would agree with Perl design: there is no try{}