I am trying to learn source filter basics, after reading some documentation and trying out small examples. I wanted to work with some bigger ones. I find the given statement a lot more useful than the if-else structure. But the substitution isn't working inside the given structure.
The filter.
package filtertest; use Filter::Util::Call; use strict; use warnings; use feature "switch"; sub import { my $self = @_; my $ref = []; filter_add( bless $ref ); } sub filter { my $self = @_; my $status; $status = filter_read(); die "some thing messed up" if $status < 0 ; die "EOF reached" if $status == 0; given( $_ ){ when (/^say*/) { print "\n%%reaching here as $_ was matched%%"; s/say/print/; print "\n%%It was changed to $_%%"; } when ( /^use/ ) { print "\n&&reaching here as $_ was matched&&"; } default { print "\n**default condition**" } } $status; } 1;
The code I'm trying to run.
use strict; use warnings; use filtertest; say 'hello.world'; say 'hello';
The output is.
**default condition** **default condition** %%reaching here as say 'hello.world'; was matched%% %%It was changed to print 'hello.world'; String found where operator expected at filter_test.pl line 9, near "s +ay 'hello.world'" (Do you need to predeclare say?) %% **default condition** **default condition** %%reaching here as say 'hello'; was matched%% %%It was changed to print 'hello'; String found where operator expected at filter_test.pl line 12, near " +say 'hello'" (Do you need to predeclare say?) syntax error at filter_test.pl line 9, near "say 'hello.world'" EOF reached at filtertest.pm line 24. %%
What could be possibly going wrong?
In reply to Source Filter basics by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |