in reply to Re: regex switch trouble
in thread regex switch trouble

so is it because its embedded in the regex ? why is it so hard to implement switch in a proper manner ?

Replies are listed 'Best First'.
Re^3: regex switch trouble
by graff (Chancellor) on Jun 14, 2007 at 02:07 UTC
    When you put executable perl code into the replacement (right-hand) portion of  s///e, I think you are by nature using "eval" to execute that code. Therefore, you run up against this little detail described in the man page for Switch (under the section heading "LIMITATIONS"):
    Due to the way source filters work in Perl, you can't use Switch inside a string "eval".
    The reference to "source filters" there has to do with how the Switch module is implemented. When you use it, the code that ends up being executed is actually different in significant ways from the code that you wrote. That's why many people (including the author of the module) advise against using Switch in "production code" (i.e. for anything that needs to be rock-solid and readily maintainable).

    As GrandFather pointed out, there are better ways to do what you need to do in this case.

      When you put executable perl code into the replacement (right-hand) portion of s///e , I think you are by nature using "eval" to execute that code.

      Nope, that's /ee, which in fact is discouraged and rarely used nowadays, that I can see. /e is not associated with the same issues. Thinking of all these matters, however, one can only appreciate even more how good they are doing with the new rules.

        Well, okay, thanks... I really don't enough about the perl internals involved with this operator to assert otherwise, and I'll admit I was "guessing" about this (hence the hedging phrase "...I think...")

        Still, perlop does describe the "e" modifier in s///e as: "Evaluate the right side as an expression", and I have to wonder whether this kind of evaluation (whatever kind it is) may have enough in common with "string eval" to have a similar effect on source filters and the Switch module. Based on evidence in the thread, it apparently does.

        Thanks for the pointer to the "Apocalypse" pages.

Re^3: regex switch trouble
by ikegami (Patriarch) on Jun 14, 2007 at 14:42 UTC
    Switch replaces the Perl parser with its own that understands the switch statement. However, Perl's parser is quite complex. Instead of re-implementing Perl, Switch takes some shortcuts which makes it malfunction in certain cases.