http://www.perlmonks.org?node_id=296134


in reply to Source filters in eval/require

If you prefix eval strings with

'delete $INC{"Foo"}; use Foo;'
in your filter, then your filter will be re-loaded when the string is eval'd.

However, when I tried this -- using Filter::Simple as I thought that I could use it's separation of code and strings to my advantage -- it gets very confusing about what stage you are at. Everytime I thought I was close to having something work, another gotcha popped up.

So, I thought I pass the basic idea along and see if it give you better inspiration than I. :)

Update: dws pointed out that the above code wasn't responsible for my percieved partial success. The use Foo; will happen before the delete $INC{'Foo'}. The delete $INC{'Foo'} needs to delete $INC{'Foo.pm'} anyway.

The thing fooled me was a redundant piece of test code in the external file....D'oh.

Anyway, having re-examined it, I did get a little closer, but still way short of anything that could be called successful.

The filter

package Filter::Test; use Filter::Simple; FILTER_ONLY code => sub { # print'C: ', $_; s[eval\s*($Filter::Simple::placeholder)]['<<<<<1>>>>>' eval '< +<<<<2>>>>>' . $1 ]; # print'C: ', $_; $_; }, quotelike => sub { # print 'Q: ', $_; $_ = uc $_; s['<<<<<1>>>>>'][delete \$INC{'Filter/Test.pm'};]; s['<<<<<2>>>>>']["BEGIN{ require Filter::Test; Filter::Test::i +mport}"]; # print 'Q: ', $_; $_; }, ; print 'Filter::Test loaded';

The test program

#! perl -slw use strict; use Filter::Test; print 'Hello World from ', __PACKAGE__; eval "print 'Just another perl hacker'"; #require 'My/filtertest.pm';

The external program

package My::filtertest; print 'Hello World'; eval "print 'Just another perl hacker';"; 1;

The output

P:\test>296091 Filter::Test loaded HELLO WORLD FROM main P:\test>296091 Filter::Test loaded HELLO WORLD FROM main Filter::Test loaded Use of uninitialized value in concatenation (.) or string at d:/Perl/l +ib/Filter/Simple.pm line 156. Use of uninitialized value in concatenation (.) or string at d:/Perl/l +ib/Filter/Simple.pm line 156. String found where operator expected at (eval 5) line 1, near "PRINT ' +JUST ANOTHER PERL HACKER'" (Do you need to predeclare PRINT?) P:\test>

As you can see, the Filter is being loaded twice, and the text within the evald string has bee uppercased (too much of it, but that's a detail :).

I did have the require half working at one point, by using eval 'use Filter::Test;' do{ local *ARGV = 'file'; <> }; , but it starts getting very complicated when you start trying to filter an eval'd require file that contains evals that contains requires....

Recurse; See Recurse; 8^o.

I can't wrap my brain around it any longer, but maybe it will inspire you, or convince you to try a different route, which might be an equally valid assist :)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.