Hi

I always thought that codefilters are executed before any parsing, but it seems to me that the code already has to be valid before parsing or validation.

Example: The following code tries to use the character X as a new kind of sigil (for motivation please see Re^2: Two more Features Perl 5 Maybe Needs)

#!/usr/bin/perl no warnings; use Filter::Simple; my $transform = sub { s{ X(\w+)\[ } {\$$1->\[}xg; s{ X(\w+) } {\$$1}xg; }; FILTER_ONLY "executable_no_comments" => $transform; while ($line=<DATA>) {$_.=$line} if ($_) { print "\nbefore:\n",$_; &$transform; print "\nafter:\n", $_; print "\neval:\n"; eval $_; } __DATA__ Xarr=[1..3]; print "@Xarr";
this code works as intended:
before: Xarr=[1..3]; print "@Xarr"; after: $arr=[1..3]; print "@$arr"; eval: 1 2 3
but if I comment out the lines starting with while and __DATA__ to filter the code directly I get:
Can't modify constant item in scalar assignment at /home/lanx/perl/sig +il.pl line 29, near "];" Execution of /home/lanx/perl/sigil.pl aborted due to compilation error +s.
line 29 is Xarr=[1..3]; which is of course not valid before transformation.

So is it only possible to use codefilters on syntactically valid code, or did I miss a possibility to filter non-valid code before processing???

Cheers Rolf


In reply to Are codefilters restricted to valid code? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.