3 # Tests for the source filters in coderef-in-@INC
9 skip_all_if_miniperl('no dynamic loading on miniperl, no Filter::Util::Call');
10 skip_all_without_perlio();
14 use Filter::Util::Call;
19 no warnings 'uninitialized';
20 ref $_[1] eq 'ARRAY' ? @{$_[1]} : $_[1];
25 open $fh, "<", \'pass("Can return file handles from \@INC");';
28 my @origlines = ("# This is a blank line\n",
29 "pass('Can return generators from \@INC');\n",
30 "pass('Which return multiple lines');\n",
33 my @lines = @origlines;
36 # Return of 0 marks EOF
37 return defined $_ ? 1 : 0;
40 do \&generator or die;
43 # Check that the array dereferencing works ready for the more complex tests:
44 do [\&generator] or die;
46 sub generator_with_state {
48 is (ref $param, 'ARRAY', "Got our parameter");
50 return defined $_ ? 1 : 0;
53 do [\&generator_with_state,
54 ["pass('Can return generators which take state');\n",
55 "pass('And return multiple lines');\n",
59 open $fh, "<", \'fail("File handles and filters work from \@INC");';
61 do [$fh, sub {s/fail/pass/; return;}] or die;
63 open $fh, "<", \'fail("File handles and filters with state work from \@INC");';
65 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
67 print "# 2 tests with pipes from subprocesses.\n";
69 my ($echo_command, $pass_arg, $fail_arg);
72 $echo_command = 'write sys$output';
77 $echo_command = 'echo';
82 open $fh, "$echo_command $pass_arg|" or die $!;
86 open $fh, "$echo_command $fail_arg|" or die $!;
88 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
92 my $status = filter_read();
93 tr/A-Za-z/N-ZA-Mn-za-m/;
98 open $fh, "<", \<<'EOC';
100 cnff("This will rot13'ed prepend");
105 open $fh, "<", \<<'EOC';
106 ORTVA {ebg13_svygre};
107 pass("This will rot13'ed twice");
110 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
113 sub prepend_rot13_filter {
116 # Filters should append to any existing data in $_
117 # But (logically) shouldn't filter it twice.
120 my $status = filter_read();
121 my $got = substr $_, 0, length $test, '';
122 is $got, $test, "Upstream didn't alter existing data";
123 tr/A-Za-z/N-ZA-Mn-za-m/;
125 die "Looping infinitely" unless $count--;
130 open $fh, "<", \<<'EOC';
131 ORTVA {cercraq_ebg13_svygre};
132 pass("This will rot13'ed twice");
135 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
137 # This generates a heck of a lot of oks, but I think it's necessary.
139 sub prepend_block_counting_filter {
145 my $status = filter_read($amount);
146 cmp_ok (length $_, '<=', $amount, "block mode works?");
148 if ($status <= 0 or /\n/s) {
153 die "Looping infinitely";
158 open $fh, "<", \<<'EOC';
159 BEGIN {prepend_block_counting_filter};
164 do [$fh, sub {return;}] or die;
166 open $fh, "<", \<<'EOC';
167 BEGIN {prepend_block_counting_filter};
168 pas("SSS make s fast SSS");
172 todo_skip "disabled under -Dmad", 50 if $Config{mad};
173 do [$fh, sub {s/s/ss/gs; s/([\nS])/$1$1$1/gs; return;}] or die;
176 sub prepend_line_counting_filter {
180 my $status = filter_read();
181 my $newlines = tr/\n//;
182 cmp_ok ($newlines, '<=', 1, "1 line at most?");
183 $_ = $output . $_ if defined $output;
188 open $fh, "<", \<<'EOC';
189 BEGIN {prepend_line_counting_filter};
190 pass("You should see this line thrice");
193 do [$fh, sub {$_ .= $_ . $_; return;}] or die;
195 do \"pass\n(\n'Scalar references are treated as initial file contents'\n)\n"
198 open $fh, "<", \"ss('The file is concatenated');";
200 do [\'pa', $fh] or die;
202 open $fh, "<", \"ff('Gur svygre vf bayl eha ba gur svyr');";
204 do [\'pa', $fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
206 open $fh, "<", \"SS('State also works');";
208 do [\'pa', $fh, sub {s/($_[1])/lc $1/ge; return;}, "S"] or die;
210 @lines = ('ss', '(', "'you can use a generator'", ')');
212 do [\'pa', \&generator] or die;
214 do [\'pa', \&generator_with_state,
215 ["ss('And generators which take state');\n",
216 "pass('And return multiple lines');\n",
219 @origlines = keys %{{ "1\n+\n2\n" => 1 }};
221 do \&generator or die;
222 is $origlines[0], "1\n+\n2\n", 'ink filters do not mangle cow buffers';
224 # d8723a6a74b2c12e wasn't perfect, as the char * returned by SvPV*() can be
225 # a temporary, freed at the next FREETMPS. And there is a FREETMPS in
229 # Need both alternatives on the regexp, because currently the logic in
230 # pp_require for what is written to %INC is somewhat confused
232 \'like(__FILE__, qr/(?:GLOB|CODE)\(0x[0-9a-f]+\)/, "__FILE__ is valid");';
236 # [perl #91880] $_ having the wrong refcount inside a
238 local @INC; local $|;
239 unshift @INC, sub { sub { undef *_; --$| }};
241 pass '$_ has the right refcount inside a filter sub';