15 $dir = File::Spec->catdir( "auto-$$" );
19 use Test::More tests => 17;
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 };
84 my $result = $foo->can( 'foo' );
85 ok( $result, 'can() first time' );
86 is( $foo->foo, 'foo', 'autoloaded first time' );
87 is( $foo->foo, 'foo', 'regular call' );
88 is( $result, \&Foo::foo, 'can() returns ref to regular installed sub' );
93 like( $@, qr/^Can't locate/, 'undefined method' );
95 $result = $foo->can( 'will_fail' );
96 ok( ! $result, 'can() should fail on undefined methods' );
98 # Used to be trouble with this
103 like( $@, qr/oops/, 'indirect method call' );
105 # Pass regular expression variable to autoloaded function. This used
106 # to go wrong because AutoLoader used regular expressions to generate
107 # autoloaded filename.
110 is( $foo->bar($1), 'foo', 'autoloaded method should not stomp match vars' );
111 is( $foo->bar($1), 'foo', '(again)' );
112 is( $foo->bazmarkhianish($1), 'foo', 'for any method call' );
113 is( $foo->bazmarkhianish($1), 'foo', '(again)' );
115 # Used to retry long subnames with shorter filenames on any old
116 # exception, including compilation error. Now AutoLoader only
117 # tries shorter filenames if it can't find the long one.
121 like( $@, qr/syntax error/, 'require error propagates' );
123 # test recursive autoloads
124 open(F, '>', File::Spec->catfile( $fulldir, 'a.al'))
125 or die "Cannot make 'a' file: $!";
129 sub a { ::ok( 1, 'adding a new autoloaded method' ); }
134 open(F, '>', File::Spec->catfile( $fulldir, 'b.al'))
135 or die "Cannot make 'b' file: $!";
138 sub b { ::ok( 1, 'adding a new autoloaded method' ) }
145 AutoLoader->import();
146 ::ok( ! defined &AUTOLOAD, 'AutoLoader should not export AUTOLOAD by default' );
149 AutoLoader->unimport();
151 ::like( $@, qr/locate object method "baz"/,
152 'unimport() should remove imported AUTOLOAD()' );
156 sub AUTOLOAD { 'i am here' }
158 AutoLoader->import();
159 AutoLoader->unimport();
161 ::is( Baz->AUTOLOAD(), 'i am here', '... but not non-imported AUTOLOAD()' );
167 return unless $dir && -d $dir;