15 $dir = File::Spec->catdir( "auto-$$" );
19 use Test::More tests => 14;
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 open(BLECH, '>', File::Spec->catfile( $fulldir, 'blechanawilla.al' ))
53 or die "Can't open blech file: $!";
56 sub blechanawilla { compilation error (
60 # This is just to keep the old SVR3 systems happy; they may fail
61 # to find the above file so we duplicate it where they should find it.
62 open(BLECH, '>', File::Spec->catfile( $fulldir, 'blechanawil.al' ))
63 or die "Can't open blech file: $!";
66 sub blechanawilla { compilation error (
70 # Let's define the package
73 AutoLoader->import( 'AUTOLOAD' );
75 sub new { bless {}, shift };
81 is( $foo->foo, 'foo', 'autoloaded first time' );
82 is( $foo->foo, 'foo', 'regular call' );
87 like( $@, qr/^Can't locate/, 'undefined method' );
89 # Used to be trouble with this
94 like( $@, qr/oops/, 'indirect method call' );
96 # Pass regular expression variable to autoloaded function. This used
97 # to go wrong because AutoLoader used regular expressions to generate
98 # autoloaded filename.
101 is( $foo->bar($1), 'foo', 'autoloaded method should not stomp match vars' );
102 is( $foo->bar($1), 'foo', '(again)' );
103 is( $foo->bazmarkhianish($1), 'foo', 'for any method call' );
104 is( $foo->bazmarkhianish($1), 'foo', '(again)' );
106 # Used to retry long subnames with shorter filenames on any old
107 # exception, including compilation error. Now AutoLoader only
108 # tries shorter filenames if it can't find the long one.
112 like( $@, qr/syntax error/, 'require error propagates' );
114 # test recursive autoloads
115 open(F, '>', File::Spec->catfile( $fulldir, 'a.al'))
116 or die "Cannot make 'a' file: $!";
120 sub a { ::ok( 1, 'adding a new autoloaded method' ); }
125 open(F, '>', File::Spec->catfile( $fulldir, 'b.al'))
126 or die "Cannot make 'b' file: $!";
129 sub b { ::ok( 1, 'adding a new autoloaded method' ) }
136 AutoLoader->import();
137 ::ok( ! defined &AUTOLOAD, 'AutoLoader should not export AUTOLOAD by default' );
140 AutoLoader->unimport();
142 ::like( $@, qr/locate object method "baz"/,
143 'unimport() should remove imported AUTOLOAD()' );
147 sub AUTOLOAD { 'i am here' }
149 AutoLoader->import();
150 AutoLoader->unimport();
152 ::is( Baz->AUTOLOAD(), 'i am here', '... but not non-imported AUTOLOAD()' );
158 return unless $dir && -d $dir;