bgiii has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use Filter::Simple to filter some code, specifically in the __DATA__ section. I followed the doc in Filter::Simple, and was able to get it to modify stuff in the __DATA__ section. Problem is that in my original code with __DATA__ (which has been filtered), the DATA now seems to have been processed already, which is a big problem.
Here is my filter: package F; use strict; use warnings; use Filter::Simple; FILTER { s/FOO/bar/g; # print qq(>>>\n$_<<<\n); # uncomment to see changed src }; #""; 1; Here's my sample program, filter commented out: #use F; use strict; use warnings; print "FOO is good\n"; while (<DATA>) { print; } __DATA__ Here is my FOO data. No FOO here. Run it, and I see this output: % perl U.pl FOO is good Here is my FOO data. No FOO here. Now, uncomment the "use F;", and I see this: % perl U.pl bar is good Here is my FOO data. No FOO here. Note that no text was changed in the DATA part. Now, uncomment the "" +line in the filter, so my filter is now this: package F; use strict; use warnings; use Filter::Simple; FILTER { s/FOO/bar/g; # print qq(>>>\n$_<<<\n); # uncomment to see changed src } ""; 1; Rerun, and I get this: % perl U.pl bar is good
Now, in filter, if you uncomment the print line, you will see that indeed the text is being changed. But apparently, since the __DATA__ block has already been read once, my program no longer sees it.

This is not good. Does anyone have a suggestion? After the filter runs (which I WANT to modify DATA area), I need to still be able to read data. This is for an application in Inline::C, by the way.

Any help/suggestions is much appreciated.

Thanks!

Replies are listed 'Best First'.
Re: Filter::Simple and __DATA__
by johngg (Canon) on Mar 08, 2007 at 10:08 UTC
    You might be able to seek back to the start of the __DATA__ section. Something like (not tested)

    ... my $startPos = tell(DATA); ... # Filter processing here ... # And later seek(DATA, $startPos, 0); ...

    I hope this is of use.

    Cheers,

    JohnGG

Re: Filter::Simple and __DATA__
by Anno (Deacon) on Mar 08, 2007 at 12:09 UTC
    After the filter runs (which I WANT to modify DATA area), I need to still be able to read data. This is for an application in Inline::C, by the way.

    Modifying the __DATA__ section is not a typical job for a source filter. There are usually more direct ways to process __DATA__. With Inline::C, the FILTERS option can be used to pre-process the C code without employing a full source filter.

    Anno