Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Source filters in eval/require

by BrowserUk (Pope)
on Oct 03, 2003 at 01:03 UTC ( #296134=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Source filters in eval/require
Select or Download Code
Replies are listed 'Best First'.
Re: Re: Source filters in eval/require
by liz (Monsignor) on Oct 03, 2003 at 07:54 UTC
    ...I can't wrap my brain around it any longer...

    That describes my feelings quite a bit before posting ;-)

    ...maybe it will inspire you, or convince you to try a different route, which might be an equally valid assist :)

    I'm considering stealing Filter::Util::Call::filter_add and Filter::Util::Call::filter_del. Problem only is that I would need to steal these before they're getting loaded. ;-( Does anyone have any idea about that?

    Liz

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://296134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (18)
As of 2015-07-30 18:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls