LanX has asked for the wisdom of the Perl Monks concerning the following question:
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)
this code works as intended:#!/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";
but if I comment out the lines starting with while and __DATA__ to filter the code directly I get:before: Xarr=[1..3]; print "@Xarr"; after: $arr=[1..3]; print "@$arr"; eval: 1 2 3
line 29 is Xarr=[1..3]; which is of course not valid before transformation.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.
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Are codefilters restricted to valid code?
by Corion (Patriarch) on Dec 22, 2008 at 14:31 UTC | |
by LanX (Saint) on Dec 22, 2008 at 15:05 UTC | |
|
Re: Are codefilters always restricted to valid code?
by almut (Canon) on Dec 22, 2008 at 14:33 UTC |