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
|
#!./perl -w
# Tests for the coderef-in-@INC feature
BEGIN {
chdir 't' if -d 't';
@INC = qw(. ../lib);
}
use File::Spec;
require "test.pl";
plan(tests => 43);
my @tempfiles = ();
sub get_temp_fh {
my $f = "DummyModule0000";
1 while -e ++$f;
push @tempfiles, $f;
open my $fh, ">$f" or die "Can't create $f: $!";
print $fh "package ".substr($_[0],0,-3)."; 1;";
close $fh;
open $fh, $f or die "Can't open $f: $!";
return $fh;
}
END { 1 while unlink @tempfiles }
sub fooinc {
my ($self, $filename) = @_;
if (substr($filename,0,3) eq 'Foo') {
return get_temp_fh($filename);
}
else {
return undef;
}
}
push @INC, \&fooinc;
ok( !eval { require Bar; 1 }, 'Trying non-magic package' );
ok( eval { require Foo; 1 }, 'require() magic via code ref' );
ok( exists $INC{'Foo.pm'}, ' %INC sees it' );
is( ref $INC{'Foo.pm'}, 'CODE', ' key is a coderef in %INC' );
is( $INC{'Foo.pm'}, \&fooinc, ' key is correct in %INC' );
ok( eval "use Foo1; 1;", 'use()' );
ok( exists $INC{'Foo1.pm'}, ' %INC sees it' );
is( ref $INC{'Foo1.pm'}, 'CODE', ' key is a coderef in %INC' );
is( $INC{'Foo1.pm'}, \&fooinc, ' key is correct in %INC' );
ok( eval { do 'Foo2.pl'; 1 }, 'do()' );
ok( exists $INC{'Foo2.pl'}, ' %INC sees it' );
is( ref $INC{'Foo2.pl'}, 'CODE', ' key is a coderef in %INC' );
is( $INC{'Foo2.pl'}, \&fooinc, ' key 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;
ok( eval { require Foo; 1; }, 'Originally loaded packages preserved' );
ok( !eval { require Foo3; 1; }, 'Original magic INC purged' );
ok( eval { require Bar; 1 }, 'require() magic via array ref' );
ok( exists $INC{'Bar.pm'}, ' %INC sees it' );
is( ref $INC{'Bar.pm'}, 'ARRAY', ' key is an arrayref in %INC' );
is( $INC{'Bar.pm'}, $arrayref, ' key is correct in %INC' );
ok( eval "use Bar1; 1;", 'use()' );
ok( exists $INC{'Bar1.pm'}, ' %INC sees it' );
is( ref $INC{'Bar1.pm'}, 'ARRAY', ' key is an arrayref in %INC' );
is( $INC{'Bar1.pm'}, $arrayref, ' key is correct in %INC' );
ok( eval { do 'Bar2.pl'; 1 }, 'do()' );
ok( exists $INC{'Bar2.pl'}, ' %INC sees it' );
is( ref $INC{'Bar2.pl'}, 'ARRAY', ' key is an arrayref in %INC' );
is( $INC{'Bar2.pl'}, $arrayref, ' key 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;
ok( eval { require Quux; 1 }, 'require() magic via hash object' );
ok( exists $INC{'Quux.pm'}, ' %INC sees it' );
is( ref $INC{'Quux.pm'}, 'FooLoader',
' key is an object in %INC' );
is( $INC{'Quux.pm'}, $href, ' key is correct in %INC' );
pop @INC;
my $aref = bless( [], 'FooLoader' );
push @INC, $aref;
ok( eval { require Quux1; 1 }, 'require() magic via array object' );
ok( exists $INC{'Quux1.pm'}, ' %INC sees it' );
is( ref $INC{'Quux1.pm'}, 'FooLoader',
' key is an object in %INC' );
is( $INC{'Quux1.pm'}, $aref, ' key is correct in %INC' );
pop @INC;
my $sref = bless( \(my $x = 1), 'FooLoader' );
push @INC, $sref;
ok( eval { require Quux2; 1 }, 'require() magic via scalar object' );
ok( exists $INC{'Quux2.pm'}, ' %INC sees it' );
is( ref $INC{'Quux2.pm'}, 'FooLoader',
' key is an object in %INC' );
is( $INC{'Quux2.pm'}, $sref, ' key 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;
}
};
ok( eval { require Toto; 1 }, 'require() magic via anonymous code ref' );
ok( exists $INC{'Toto.pm'}, ' %INC sees it' );
ok( ! ref $INC{'Toto.pm'}, q/ key isn't a ref in %INC/ );
is( $INC{'Toto.pm'}, 'xyz', ' key is correct in %INC' );
pop @INC;
|