15 $dir = File::Spec->catdir( "auto-$$" );
19 use Test::More tests => 13;
21 # First we must set up some autoloader files
22 my $fulldir = File::Spec->catdir( $dir, 'auto', 'Foo' );
23 mkpath( $fulldir ) or die "Can't mkdir '$fulldir': $!";
25 open(FOO, '>', File::Spec->catfile( $fulldir, 'foo.al' ))
26 or die "Can't open foo file: $!";
29 sub foo { shift; shift || "foo" }
34 open(BAR, '>', File::Spec->catfile( $fulldir, 'bar.al' ))
35 or die "Can't open bar file: $!";
38 sub bar { shift; shift || "bar" }
43 open(BAZ, '>', File::Spec->catfile( $fulldir, 'bazmarkhian.al' ))
44 or die "Can't open bazmarkhian file: $!";
47 sub bazmarkhianish { shift; shift || "baz" }
52 # Let's define the package
55 AutoLoader->import( 'AUTOLOAD' );
57 sub new { bless {}, shift };
63 is( $foo->foo, 'foo', 'autoloaded first time' );
64 is( $foo->foo, 'foo', 'regular call' );
69 like( $@, qr/^Can't locate/, 'undefined method' );
71 # Used to be trouble with this
76 like( $@, qr/oops/, 'indirect method call' );
78 # Pass regular expression variable to autoloaded function. This used
79 # to go wrong because AutoLoader used regular expressions to generate
80 # autoloaded filename.
83 is( $foo->bar($1), 'foo', 'autoloaded method should not stomp match vars' );
84 is( $foo->bar($1), 'foo', '(again)' );
85 is( $foo->bazmarkhianish($1), 'foo', 'for any method call' );
86 is( $foo->bazmarkhianish($1), 'foo', '(again)' );
88 # test recursive autoloads
89 open(F, '>', File::Spec->catfile( $fulldir, 'a.al'))
90 or die "Cannot make 'a' file: $!";
94 sub a { ::ok( 1, 'adding a new autoloaded method' ); }
99 open(F, '>', File::Spec->catfile( $fulldir, 'b.al'))
100 or die "Cannot make 'b' file: $!";
103 sub b { ::ok( 1, 'adding a new autoloaded method' ) }
110 AutoLoader->import();
111 ::ok( ! defined &AUTOLOAD, 'AutoLoader should not export AUTOLOAD by default' );
114 AutoLoader->unimport();
116 ::like( $@, qr/locate object method "baz"/,
117 'unimport() should remove imported AUTOLOAD()' );
121 sub AUTOLOAD { 'i am here' }
123 AutoLoader->import();
124 AutoLoader->unimport();
126 ::is( Baz->AUTOLOAD(), 'i am here', '... but not non-imported AUTOLOAD()' );
132 return unless $dir && -d $dir;