1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
#!./perl -w
# Tests for the coderef-in-@INC feature
use Config;
my $can_fork = 0;
my $minitest = $ENV{PERL_CORE_MINITEST};
my $has_perlio = $Config{useperlio};
BEGIN {
chdir 't' if -d 't';
@INC = qw(. ../lib);
}
if (!$minitest) {
if ($Config{d_fork} && eval 'require POSIX; 1') {
$can_fork = 1;
}
}
use strict;
use File::Spec;
require "test.pl";
plan(tests => 49 + !$minitest * (3 + 14 * $can_fork));
sub get_temp_fh {
my $f = tempfile();
open my $fh, ">$f" or die "Can't create $f: $!";
print $fh "package ".substr($_[0],0,-3).";\n1;\n";
print $fh $_[1] if @_ > 1;
close $fh or die "Couldn't close: $!";
open $fh, $f or die "Can't open $f: $!";
return $fh;
}
sub fooinc {
my ($self, $filename) = @_;
if (substr($filename,0,3) eq 'Foo') {
return get_temp_fh($filename);
}
else {
return undef;
}
}
push @INC, \&fooinc;
my $evalret = eval { require Bar; 1 };
ok( !$evalret, 'Trying non-magic package' );
$evalret = eval { require Foo; 1 };
die $@ if $@;
ok( $evalret, 'require Foo; magic via code ref' );
ok( exists $INC{'Foo.pm'}, ' %INC sees Foo.pm' );
is( ref $INC{'Foo.pm'}, 'CODE', ' val Foo.pm is a coderef in %INC' );
is( $INC{'Foo.pm'}, \&fooinc, ' val Foo.pm is correct in %INC' );
$evalret = eval "use Foo1; 1;";
die $@ if $@;
ok( $evalret, 'use Foo1' );
ok( exists $INC{'Foo1.pm'}, ' %INC sees Foo1.pm' );
is( ref $INC{'Foo1.pm'}, 'CODE', ' val Foo1.pm is a coderef in %INC' );
is( $INC{'Foo1.pm'}, \&fooinc, ' val Foo1.pm is correct in %INC' );
$evalret = eval { do 'Foo2.pl'; 1 };
die $@ if $@;
ok( $evalret, 'do "Foo2.pl"' );
ok( exists $INC{'Foo2.pl'}, ' %INC sees Foo2.pl' );
is( ref $INC{'Foo2.pl'}, 'CODE', ' val Foo2.pl is a coderef in %INC' );
is( $INC{'Foo2.pl'}, \&fooinc, ' val Foo2.pl is correct in %INC' );
pop @INC;
sub fooinc2 {
my ($self, $filename) = @_;
if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
return get_temp_fh($filename);
}
else {
return undef;
}
}
my $arrayref = [ \&fooinc2, 'Bar' ];
push @INC, $arrayref;
$evalret = eval { require Foo; 1; };
die $@ if $@;
ok( $evalret, 'Originally loaded packages preserved' );
$evalret = eval { require Foo3; 1; };
ok( !$evalret, 'Original magic INC purged' );
$evalret = eval { require Bar; 1 };
die $@ if $@;
ok( $evalret, 'require Bar; magic via array ref' );
ok( exists $INC{'Bar.pm'}, ' %INC sees Bar.pm' );
is( ref $INC{'Bar.pm'}, 'ARRAY', ' val Bar.pm is an arrayref in %INC' );
is( $INC{'Bar.pm'}, $arrayref, ' val Bar.pm is correct in %INC' );
ok( eval "use Bar1; 1;", 'use Bar1' );
ok( exists $INC{'Bar1.pm'}, ' %INC sees Bar1.pm' );
is( ref $INC{'Bar1.pm'}, 'ARRAY', ' val Bar1.pm is an arrayref in %INC' );
is( $INC{'Bar1.pm'}, $arrayref, ' val Bar1.pm is correct in %INC' );
ok( eval { do 'Bar2.pl'; 1 }, 'do "Bar2.pl"' );
ok( exists $INC{'Bar2.pl'}, ' %INC sees Bar2.pl' );
is( ref $INC{'Bar2.pl'}, 'ARRAY', ' val Bar2.pl is an arrayref in %INC' );
is( $INC{'Bar2.pl'}, $arrayref, ' val Bar2.pl is correct in %INC' );
pop @INC;
sub FooLoader::INC {
my ($self, $filename) = @_;
if (substr($filename,0,4) eq 'Quux') {
return get_temp_fh($filename);
}
else {
return undef;
}
}
my $href = bless( {}, 'FooLoader' );
push @INC, $href;
$evalret = eval { require Quux; 1 };
die $@ if $@;
ok( $evalret, 'require Quux; magic via hash object' );
ok( exists $INC{'Quux.pm'}, ' %INC sees Quux.pm' );
is( ref $INC{'Quux.pm'}, 'FooLoader',
' val Quux.pm is an object in %INC' );
is( $INC{'Quux.pm'}, $href, ' val Quux.pm is correct in %INC' );
pop @INC;
my $aref = bless( [], 'FooLoader' );
push @INC, $aref;
$evalret = eval { require Quux1; 1 };
die $@ if $@;
ok( $evalret, 'require Quux1; magic via array object' );
ok( exists $INC{'Quux1.pm'}, ' %INC sees Quux1.pm' );
is( ref $INC{'Quux1.pm'}, 'FooLoader',
' val Quux1.pm is an object in %INC' );
is( $INC{'Quux1.pm'}, $aref, ' val Quux1.pm is correct in %INC' );
pop @INC;
my $sref = bless( \(my $x = 1), 'FooLoader' );
push @INC, $sref;
$evalret = eval { require Quux2; 1 };
die $@ if $@;
ok( $evalret, 'require Quux2; magic via scalar object' );
ok( exists $INC{'Quux2.pm'}, ' %INC sees Quux2.pm' );
is( ref $INC{'Quux2.pm'}, 'FooLoader',
' val Quux2.pm is an object in %INC' );
is( $INC{'Quux2.pm'}, $sref, ' val Quux2.pm is correct in %INC' );
pop @INC;
push @INC, sub {
my ($self, $filename) = @_;
if (substr($filename,0,4) eq 'Toto') {
$INC{$filename} = 'xyz';
return get_temp_fh($filename);
}
else {
return undef;
}
};
$evalret = eval { require Toto; 1 };
die $@ if $@;
ok( $evalret, 'require Toto; magic via anonymous code ref' );
ok( exists $INC{'Toto.pm'}, ' %INC sees Toto.pm' );
ok( ! ref $INC{'Toto.pm'}, q/ val Toto.pm isn't a ref in %INC/ );
is( $INC{'Toto.pm'}, 'xyz', ' val Toto.pm is correct in %INC' );
pop @INC;
push @INC, sub {
my ($self, $filename) = @_;
if ($filename eq 'abc.pl') {
return get_temp_fh($filename, qq(return "abc";\n));
}
else {
return undef;
}
};
my $ret = "";
$ret ||= do 'abc.pl';
is( $ret, 'abc', 'do "abc.pl" sees return value' );
{
my $filename = './Foo.pm';
#local @INC; # local fails on tied @INC
my @old_INC = @INC; # because local doesn't work on tied arrays
@INC = sub { $filename = 'seen'; return undef; };
eval { require $filename; };
is( $filename, 'seen', 'the coderef sees fully-qualified pathnames' );
@INC = @old_INC;
}
# this will segfault if it fails
sub PVBM () { 'foo' }
{ my $dummy = index 'foo', PVBM }
# I don't know whether these requires should succeed or fail. 5.8 failed
# all of them; 5.10 with an ordinary constant in place of PVBM lets the
# latter two succeed. For now I don't care, as long as they don't
# segfault :).
unshift @INC, sub { PVBM };
eval 'require foo';
ok( 1, 'returning PVBM doesn\'t segfault require' );
eval 'use foo';
ok( 1, 'returning PVBM doesn\'t segfault use' );
shift @INC;
unshift @INC, sub { \PVBM };
eval 'require foo';
ok( 1, 'returning PVBM ref doesn\'t segfault require' );
eval 'use foo';
ok( 1, 'returning PVBM ref doesn\'t segfault use' );
shift @INC;
exit if $minitest;
SKIP: {
skip( "No PerlIO available", 3 ) unless $has_perlio;
pop @INC;
push @INC, sub {
my ($cr, $filename) = @_;
my $module = $filename; $module =~ s,/,::,g; $module =~ s/\.pm$//;
open my $fh, '<',
\"package $module; sub complain { warn q() }; \$::file = __FILE__;"
or die $!;
$INC{$filename} = "/custom/path/to/$filename";
return $fh;
};
require Publius::Vergilius::Maro;
is( $INC{'Publius/Vergilius/Maro.pm'},
'/custom/path/to/Publius/Vergilius/Maro.pm', '%INC set correctly');
is( our $file, '/custom/path/to/Publius/Vergilius/Maro.pm',
'__FILE__ set correctly' );
{
my $warning;
local $SIG{__WARN__} = sub { $warning = shift };
Publius::Vergilius::Maro::complain();
like( $warning, qr{something's wrong at /custom/path/to/Publius/Vergilius/Maro.pm}, 'warn() reports correct file source' );
}
}
pop @INC;
if ($can_fork) {
require PerlIO::scalar;
# This little bundle of joy generates n more recursive use statements,
# with each module chaining the next one down to 0. If it works, then we
# can safely nest subprocesses
my $use_filter_too;
push @INC, sub {
return unless $_[1] =~ /^BBBLPLAST(\d+)\.pm/;
my $pid = open my $fh, "-|";
if ($pid) {
# Parent
return $fh unless $use_filter_too;
# Try filters and state in addition.
return ($fh, sub {s/$_[1]/pass/; return}, "die")
}
die "Can't fork self: $!" unless defined $pid;
# Child
my $count = $1;
# Lets force some fun with odd sized reads.
$| = 1;
print 'push @main::bbblplast, ';
print "$count;\n";
if ($count--) {
print "use BBBLPLAST$count;\n";
}
if ($use_filter_too) {
print "die('In $_[1]');";
} else {
print "pass('In $_[1]');";
}
print '"Truth"';
POSIX::_exit(0);
die "Can't get here: $!";
};
@::bbblplast = ();
require BBBLPLAST5;
is ("@::bbblplast", "0 1 2 3 4 5", "All ran");
foreach (keys %INC) {
delete $INC{$_} if /^BBBLPLAST/;
}
@::bbblplast = ();
$use_filter_too = 1;
require BBBLPLAST5;
is ("@::bbblplast", "0 1 2 3 4 5", "All ran with a filter");
}
|