Why are Perl source filters bad and when is it OK to use them?

后端 未结 7 1522
Happy的楠姐
Happy的楠姐 2020-11-30 07:55

It is \"common knowledge\" that source filters are bad and should not be used in production code.

When answering a a similar, but more specific question I couldn\'t

7条回答
  •  星月不相逢
    2020-11-30 08:24

    It's worth mentioning that Devel::Declare keywords (and starting with Perl 5.11.2, pluggable keywords) aren't source filters, and don't run afoul of the "only perl can parse Perl" problem. This is because they're run by the perl parser itself, they take what they need from the input, and then they return control to the very same parser.

    For example, when you declare a method in MooseX::Declare like this:

    method frob ($bubble, $bobble does coerce) {
      ... # complicated code
    }
    

    The word "method" invokes the method keyword parser, which uses its own grammar to get the method name and parse the method signature (which isn't Perl, but it doesn't need to be -- it just needs to be well-defined). Then it leaves perl to parse the method body as the body of a sub. Anything anywhere in your code that isn't between the word "method" and the end of a method signature doesn't get seen by the method parser at all, so it can't break your code, no matter how tricky you get.

提交回复
热议问题