1#!./perl -w 2 3BEGIN { 4 chdir 't' if -d 't'; 5 @INC = '../lib'; 6} 7 8use strict; 9use File::Spec; 10use File::Path; 11 12my $dir; 13BEGIN 14{ 15 $dir = File::Spec->catdir( "auto-$$" ); 16 unshift @INC, $dir; 17} 18 19use Test::More tests => 17; 20 21# First we must set up some autoloader files 22my $fulldir = File::Spec->catdir( $dir, 'auto', 'Foo' ); 23mkpath( $fulldir ) or die "Can't mkdir '$fulldir': $!"; 24 25open(FOO, '>', File::Spec->catfile( $fulldir, 'foo.al' )) 26 or die "Can't open foo file: $!"; 27print FOO <<'EOT'; 28package Foo; 29sub foo { shift; shift || "foo" } 301; 31EOT 32close(FOO); 33 34open(BAR, '>', File::Spec->catfile( $fulldir, 'bar.al' )) 35 or die "Can't open bar file: $!"; 36print BAR <<'EOT'; 37package Foo; 38sub bar { shift; shift || "bar" } 391; 40EOT 41close(BAR); 42 43open(BAZ, '>', File::Spec->catfile( $fulldir, 'bazmarkhian.al' )) 44 or die "Can't open bazmarkhian file: $!"; 45print BAZ <<'EOT'; 46package Foo; 47sub bazmarkhianish { shift; shift || "baz" } 481; 49EOT 50close(BAZ); 51 52open(BLECH, '>', File::Spec->catfile( $fulldir, 'blechanawilla.al' )) 53 or die "Can't open blech file: $!"; 54print BLECH <<'EOT'; 55package Foo; 56sub blechanawilla { compilation error ( 57EOT 58close(BLECH); 59 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. 62open(BLECH, '>', File::Spec->catfile( $fulldir, 'blechanawil.al' )) 63 or die "Can't open blech file: $!"; 64print BLECH <<'EOT'; 65package Foo; 66sub blechanawilla { compilation error ( 67EOT 68close(BLECH); 69 70# Let's define the package 71package Foo; 72require AutoLoader; 73AutoLoader->import( 'AUTOLOAD' ); 74 75sub new { bless {}, shift }; 76sub foo; 77sub bar; 78sub bazmarkhianish; 79 80package main; 81 82my $foo = new Foo; 83 84my $result = $foo->can( 'foo' ); 85ok( $result, 'can() first time' ); 86is( $foo->foo, 'foo', 'autoloaded first time' ); 87is( $foo->foo, 'foo', 'regular call' ); 88is( $result, \&Foo::foo, 'can() returns ref to regular installed sub' ); 89 90eval { 91 $foo->will_fail; 92}; 93like( $@, qr/^Can't locate/, 'undefined method' ); 94 95$result = $foo->can( 'will_fail' ); 96ok( ! $result, 'can() should fail on undefined methods' ); 97 98# Used to be trouble with this 99eval { 100 my $foo = new Foo; 101 die "oops"; 102}; 103like( $@, qr/oops/, 'indirect method call' ); 104 105# Pass regular expression variable to autoloaded function. This used 106# to go wrong because AutoLoader used regular expressions to generate 107# autoloaded filename. 108'foo' =~ /(\w+)/; 109 110is( $foo->bar($1), 'foo', 'autoloaded method should not stomp match vars' ); 111is( $foo->bar($1), 'foo', '(again)' ); 112is( $foo->bazmarkhianish($1), 'foo', 'for any method call' ); 113is( $foo->bazmarkhianish($1), 'foo', '(again)' ); 114 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. 118eval { 119 $foo->blechanawilla; 120}; 121like( $@, qr/syntax error/, 'require error propagates' ); 122 123# test recursive autoloads 124open(F, '>', File::Spec->catfile( $fulldir, 'a.al')) 125 or die "Cannot make 'a' file: $!"; 126print F <<'EOT'; 127package Foo; 128BEGIN { b() } 129sub a { ::ok( 1, 'adding a new autoloaded method' ); } 1301; 131EOT 132close(F); 133 134open(F, '>', File::Spec->catfile( $fulldir, 'b.al')) 135 or die "Cannot make 'b' file: $!"; 136print F <<'EOT'; 137package Foo; 138sub b { ::ok( 1, 'adding a new autoloaded method' ) } 1391; 140EOT 141close(F); 142Foo::a(); 143 144package Bar; 145AutoLoader->import(); 146::ok( ! defined &AUTOLOAD, 'AutoLoader should not export AUTOLOAD by default' ); 147 148package Foo; 149AutoLoader->unimport(); 150eval { Foo->baz() }; 151::like( $@, qr/locate object method "baz"/, 152 'unimport() should remove imported AUTOLOAD()' ); 153 154package Baz; 155 156sub AUTOLOAD { 'i am here' } 157 158AutoLoader->import(); 159AutoLoader->unimport(); 160 161::is( Baz->AUTOLOAD(), 'i am here', '... but not non-imported AUTOLOAD()' ); 162 163package main; 164 165# cleanup 166END { 167 return unless $dir && -d $dir; 168 rmtree $dir; 169} 170