17 $dir = File::Spec->catdir( "auto-$$" );
21 use Test::More tests => 22;
23 # First we must set up some autoloader files
24 my $fulldir = File::Spec->catdir( $dir, 'auto', 'Foo' );
25 mkpath( $fulldir ) or die "Can't mkdir '$fulldir': $!";
27 open(FOO, '>', File::Spec->catfile( $fulldir, 'foo.al' ))
28 or die "Can't open foo file: $!";
31 sub foo { shift; shift || "foo" }
36 open(BAR, '>', File::Spec->catfile( $fulldir, 'bar.al' ))
37 or die "Can't open bar file: $!";
40 sub bar { shift; shift || "bar" }
45 open(BAZ, '>', File::Spec->catfile( $fulldir, 'bazmarkhian.al' ))
46 or die "Can't open bazmarkhian file: $!";
49 sub bazmarkhianish { shift; shift || "baz" }
54 open(BLECH, '>', File::Spec->catfile( $fulldir, 'blechanawilla.al' ))
55 or die "Can't open blech file: $!";
58 sub blechanawilla { compilation error (
62 # This is just to keep the old SVR3 systems happy; they may fail
63 # to find the above file so we duplicate it where they should find it.
64 open(BLECH, '>', File::Spec->catfile( $fulldir, 'blechanawil.al' ))
65 or die "Can't open blech file: $!";
68 sub blechanawilla { compilation error (
72 # Let's define the package
75 AutoLoader->import( 'AUTOLOAD' );
77 sub new { bless {}, shift };
85 my $result = $foo->can( 'foo' );
86 ok( $result, 'can() first time' );
87 is( $foo->foo, 'foo', 'autoloaded first time' );
88 is( $foo->foo, 'foo', 'regular call' );
89 is( $result, \&Foo::foo, 'can() returns ref to regular installed sub' );
90 $result = $foo->can( 'bar' );
91 ok( $result, 'can() should work when importing AUTOLOAD too' );
92 is( $foo->bar, 'bar', 'regular call' );
93 is( $result, \&Foo::bar, '... returning ref to regular installed sub' );
98 like( $@, qr/^Can't locate/, 'undefined method' );
100 $result = $foo->can( 'will_fail' );
101 ok( ! $result, 'can() should fail on undefined methods' );
103 # Used to be trouble with this
105 my $foo = Foo->new();
108 like( $@, qr/oops/, 'indirect method call' );
110 # Pass regular expression variable to autoloaded function. This used
111 # to go wrong because AutoLoader used regular expressions to generate
112 # autoloaded filename.
115 is( $foo->bar($1), 'foo', 'autoloaded method should not stomp match vars' );
116 is( $foo->bar($1), 'foo', '(again)' );
117 is( $foo->bazmarkhianish($1), 'foo', 'for any method call' );
118 is( $foo->bazmarkhianish($1), 'foo', '(again)' );
120 # Used to retry long subnames with shorter filenames on any old
121 # exception, including compilation error. Now AutoLoader only
122 # tries shorter filenames if it can't find the long one.
126 like( $@, qr/syntax error/i, 'require error propagates' );
128 # test recursive autoloads
129 open(F, '>', File::Spec->catfile( $fulldir, 'a.al'))
130 or die "Cannot make 'a' file: $!";
134 sub a { ::ok( 1, 'adding a new autoloaded method' ); }
139 open(F, '>', File::Spec->catfile( $fulldir, 'b.al'))
140 or die "Cannot make 'b' file: $!";
143 sub b { ::ok( 1, 'adding a new autoloaded method' ) }
150 AutoLoader->import();
151 ::ok( ! defined &AUTOLOAD, 'AutoLoader should not export AUTOLOAD by default' );
152 ::ok( ! defined &can, '... nor can()' );
155 AutoLoader->unimport();
157 ::like( $@, qr/locate object method "baz"/,
158 'unimport() should remove imported AUTOLOAD()' );
162 sub AUTOLOAD { 'i am here' }
164 AutoLoader->import();
165 AutoLoader->unimport();
167 ::is( Baz->AUTOLOAD(), 'i am here', '... but not non-imported AUTOLOAD()' );
171 use AutoLoader 'AUTOLOAD';
178 $INC{"SomeClass.pm"} = $0; # Prepare possible recursion
180 my $p = SomeClass->new();
181 } # <-- deep recursion in AUTOLOAD looking for SomeClass::DESTROY?
182 ::ok(1, "AutoLoader shouldn't loop forever if \%INC is modified");
186 return unless $dir && -d $dir;