in reply to An Approach to Function to Subroutine using Regular Expression
This regex approach is quite dangerous, since any x will be replaced, for instance exp(x) could become e$xp($x)
so at least try to replace at word boundaries \b with $str =~ s/\bx\b/\$x/g
demonstration:
DB<134> "ex p"=~s/\bx\b/\$x/r => "ex p" DB<135> "e x p"=~s/\bx\b/\$x/r => "e \$x p"
There are some ways to tweak Perl's parser though, but they are far too advanced for your level now.
i.e. using a function sub a {$a} to replace x with a (x is already an operator) and maybe catch and translate stuff like sin2 in AUTOLOAD.
|
|---|