| 1 | #!./perl |
| 2 | |
| 3 | BEGIN { |
| 4 | chdir 't' if -d 't'; |
| 5 | if ($^O eq 'MacOS') { |
| 6 | $dir = ":auto-$$"; |
| 7 | $sep = ":"; |
| 8 | } else { |
| 9 | $dir = "auto-$$"; |
| 10 | $sep = "/"; |
| 11 | } |
| 12 | @INC = $dir; |
| 13 | push @INC, '../lib'; |
| 14 | } |
| 15 | |
| 16 | print "1..11\n"; |
| 17 | |
| 18 | # First we must set up some autoloader files |
| 19 | mkdir $dir, 0755 or die "Can't mkdir $dir: $!"; |
| 20 | mkdir "$dir${sep}auto", 0755 or die "Can't mkdir: $!"; |
| 21 | mkdir "$dir${sep}auto${sep}Foo", 0755 or die "Can't mkdir: $!"; |
| 22 | |
| 23 | open(FOO, ">$dir${sep}auto${sep}Foo${sep}foo.al") or die; |
| 24 | print FOO <<'EOT'; |
| 25 | package Foo; |
| 26 | sub foo { shift; shift || "foo" } |
| 27 | 1; |
| 28 | EOT |
| 29 | close(FOO); |
| 30 | |
| 31 | open(BAR, ">$dir${sep}auto${sep}Foo${sep}bar.al") or die; |
| 32 | print BAR <<'EOT'; |
| 33 | package Foo; |
| 34 | sub bar { shift; shift || "bar" } |
| 35 | 1; |
| 36 | EOT |
| 37 | close(BAR); |
| 38 | |
| 39 | open(BAZ, ">$dir${sep}auto${sep}Foo${sep}bazmarkhian.al") or die; |
| 40 | print BAZ <<'EOT'; |
| 41 | package Foo; |
| 42 | sub bazmarkhianish { shift; shift || "baz" } |
| 43 | 1; |
| 44 | EOT |
| 45 | close(BAZ); |
| 46 | |
| 47 | # Let's define the package |
| 48 | package Foo; |
| 49 | require AutoLoader; |
| 50 | @ISA=qw(AutoLoader); |
| 51 | |
| 52 | sub new { bless {}, shift }; |
| 53 | |
| 54 | package main; |
| 55 | |
| 56 | $foo = new Foo; |
| 57 | |
| 58 | print "not " unless $foo->foo eq 'foo'; # autoloaded first time |
| 59 | print "ok 1\n"; |
| 60 | |
| 61 | print "not " unless $foo->foo eq 'foo'; # regular call |
| 62 | print "ok 2\n"; |
| 63 | |
| 64 | # Try an undefined method |
| 65 | eval { |
| 66 | $foo->will_fail; |
| 67 | }; |
| 68 | print "not " unless $@ =~ /^Can't locate/; |
| 69 | print "ok 3\n"; |
| 70 | |
| 71 | # Used to be trouble with this |
| 72 | eval { |
| 73 | my $foo = new Foo; |
| 74 | die "oops"; |
| 75 | }; |
| 76 | print "not " unless $@ =~ /oops/; |
| 77 | print "ok 4\n"; |
| 78 | |
| 79 | # Pass regular expression variable to autoloaded function. This used |
| 80 | # to go wrong because AutoLoader used regular expressions to generate |
| 81 | # autoloaded filename. |
| 82 | "foo" =~ /(\w+)/; |
| 83 | print "not " unless $1 eq 'foo'; |
| 84 | print "ok 5\n"; |
| 85 | |
| 86 | print "not " unless $foo->bar($1) eq 'foo'; |
| 87 | print "ok 6\n"; |
| 88 | |
| 89 | print "not " unless $foo->bar($1) eq 'foo'; |
| 90 | print "ok 7\n"; |
| 91 | |
| 92 | print "not " unless $foo->bazmarkhianish($1) eq 'foo'; |
| 93 | print "ok 8\n"; |
| 94 | |
| 95 | print "not " unless $foo->bazmarkhianish($1) eq 'foo'; |
| 96 | print "ok 9\n"; |
| 97 | |
| 98 | # test recursive autoloads |
| 99 | open(F, ">$dir${sep}auto${sep}Foo${sep}a.al") or die; |
| 100 | print F <<'EOT'; |
| 101 | package Foo; |
| 102 | BEGIN { b() } |
| 103 | sub a { print "ok 11\n"; } |
| 104 | 1; |
| 105 | EOT |
| 106 | close(F); |
| 107 | |
| 108 | open(F, ">$dir${sep}auto${sep}Foo${sep}b.al") or die; |
| 109 | print F <<'EOT'; |
| 110 | package Foo; |
| 111 | sub b { print "ok 10\n"; } |
| 112 | 1; |
| 113 | EOT |
| 114 | close(F); |
| 115 | Foo::a(); |
| 116 | |
| 117 | # cleanup |
| 118 | END { |
| 119 | return unless $dir && -d $dir; |
| 120 | unlink "$dir${sep}auto${sep}Foo${sep}foo.al"; |
| 121 | unlink "$dir${sep}auto${sep}Foo${sep}bar.al"; |
| 122 | unlink "$dir${sep}auto${sep}Foo${sep}bazmarkhian.al"; |
| 123 | unlink "$dir${sep}auto${sep}Foo${sep}a.al"; |
| 124 | unlink "$dir${sep}auto${sep}Foo${sep}b.al"; |
| 125 | rmdir "$dir${sep}auto${sep}Foo"; |
| 126 | rmdir "$dir${sep}auto"; |
| 127 | rmdir "$dir"; |
| 128 | } |