in reply to replacing text with a function
$text =~ s/(\W+)/$1->()/eg; # replace any nonwhitespace characters wit +h the result of a function call
The danger is that unless you trust the file being fed into the program, someone might feed you a word that called some bad subroutine in your program. Or it might just happen accidentally!
To make sure this doesn't happen, set up a hash of subroutine references(I think this could be called a "dispatch table"):
%subs = ( bar => \&bar foo => \&foo do_something => \&do_something ); $text =~ s/(\W+)/$subs{$1}->() if defined $subs{$1}/eg; # only call fr +om a list of specific subroutines
The $subs{$1}->() is calling the subroutine reference (coderef) defined in the hash, so for example "bar" would be replaced by the result of the "bar" subroutine.
dave hj~
|
|---|