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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.