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

Hello fellow monks.

The code below uses a source code filter, which replaces all "word"s with "worlds". However, it only prints "Hello word" instead of "Hello world".

# test code my $a = 'use filtertest; BEGIN { print "eval test\n"; } sub { print "Hello word\n"; }->(); END { print "eval over\n" }'; eval $a;
# module: filtertest package filtertest; use Filter::Util::Call; sub import { my($type, @arguments) = @_; filter_add( sub { my($status); $status = filter_read(); s/word/world/g if ($status > 0); $status } ) } 1;

The problem is that I use eval to evaluate the code string. Actually, perl -e 'use filtertest; print "Hello word\n"' gives the same, i.e. wrong, result. But

use filtertest; print "Hello word\n";

works properly.

It seems the BEGIN magic of the filter is not executed when being 'eval'ed. Does this mean that Filter::Util::Call confilcts with the builtin eval function?

Thanks in advance. Feel free to correct my mistakes.

Replies are listed 'Best First'.
Re: Filter::Util::Call does not work in eval?
by Anonymous Monk on Sep 25, 2014 at 07:45 UTC

      Thanks a lot! I'll go through those threads :)