8 plan( tests => 97 ); # some tests are run in a BEGIN block
13 BEGIN { print "# Tests with caller(0)\n"; }
16 ok( (!@c), "caller(0) in main program" );
18 eval { @c = caller(0) };
19 is( $c[3], "(eval)", "subroutine name in an eval {}" );
20 ok( !$c[4], "hasargs false in an eval {}" );
22 eval q{ @c = caller(0) };
23 is( $c[3], "(eval)", "subroutine name in an eval ''" );
24 ok( !$c[4], "hasargs false in an eval ''" );
26 sub { @c = caller(0) } -> ();
27 is( $c[3], "main::__ANON__", "anonymous subroutine name" );
28 ok( $c[4], "hasargs true with anon sub" );
30 # Bug 20020517.003 (#9367), used to dump core
31 sub foo { @c = caller(0) }
32 my $fooref = delete $::{foo};
34 is( $c[3], "main::foo", "deleted subroutine name" );
35 ok( $c[4], "hasargs true with deleted sub" );
39 is +(caller 0)[1], __FILE__,
40 "[perl #68712] filenames after require in a BEGIN block"
43 print "# Tests with caller(1)\n";
45 sub f { @c = caller(1) }
49 is( $c[3], "main::callf", "subroutine name" );
50 ok( $c[4], "hasargs true with callf()" );
52 ok( !$c[4], "hasargs false with &callf" );
55 is( $c[3], "(eval)", "subroutine name in an eval {}" );
56 ok( !$c[4], "hasargs false in an eval {}" );
59 is( $c[3], "(eval)", "subroutine name in an eval ''" );
60 ok( !$c[4], "hasargs false in an eval ''" );
63 is( $c[3], "main::__ANON__", "anonymous subroutine name" );
64 ok( $c[4], "hasargs true with anon sub" );
67 my $fooref2 = delete $::{foo2};
69 is( $c[3], "main::foo2", "deleted subroutine name" );
70 ok( $c[4], "hasargs true with deleted sub" );
72 # See if caller() returns the correct warning mask
78 foreach (unpack('W*', $in)) {
79 $out .= sprintf('\x%02x', $_);
86 local $Level = $Level + 2;
87 my ($got, $exp, $desc) = @_;
88 if (! ok($got eq $exp, $desc)) {
89 diag(' got: ' . show_bits($got));
90 diag('expected: ' . show_bits($exp));
97 check_bits( (caller(0))[9], $w, "warnings match caller ($id)");
102 BEGIN { check_bits( ${^WARNING_BITS}, "\0" x $warnings::BYTES, 'all bits off via "no warnings"' ) }
103 testwarn("\0" x $warnings::BYTES, 'no bits');
106 BEGIN { check_bits( ${^WARNING_BITS}, "\x55" x $warnings::BYTES,
107 'default bits on via "use warnings"' ); }
108 testwarn("\x55" x $warnings::BYTES, 'all');
112 # The next two cases test for a bug where caller ignored evals if
113 # the DB::sub glob existed but &DB::sub did not (for example, if
114 # $^P had been set but no debugger has been loaded). The tests
115 # thus assume that there is no &DB::sub: if there is one, they
116 # should both pass no matter whether or not this bug has been
119 my $debugger_test = q<
120 my @stackinfo = caller(0);
121 return scalar @stackinfo;
124 sub pb { return (caller(0))[3] }
126 my $i = eval $debugger_test;
127 is( $i, 11, "do not skip over eval (and caller returns 10 elements)" );
129 is( eval 'pb()', 'main::pb', "actually return the right function name" );
131 my $saved_perldb = $^P;
135 $i = eval $debugger_test;
136 is( $i, 11, 'do not skip over eval even if $^P had been on at some point' );
137 is( eval 'pb()', 'main::pb', 'actually return the right function name even if $^P had been on at some point' );
139 print "# caller can now return the compile time state of %^H\n";
144 my @results = caller($level||0);
145 exists $results[10]->{$key};
151 my @results = caller($level||0);
152 $results[10]->{$key};
156 my $tmpfile = tempfile();
158 open my $fh, '>', $tmpfile or die "open $tmpfile: $!";
171 @DB::args = bless [], 'KAZASH';
174 print scalar @DB::args;
180 @DB::args = (); # At this point, the object should be freed.
183 print scalar @DB::args;
190 my $got = runperl(progfile => $tmpfile, args => [$_]);
192 like($got, qr/\s*0 1 DESTROY 0 0\s*/,
193 "\@DB::args doesn't leak with \$^P = $_");
197 # This also used to leak [perl #97010]:
200 sub fwib::DESTROY { ++$gone }
202 sub { () = caller(0) }->(); # initialise PL_dbargs
203 @args = bless[],'fwib';
204 sub { () = caller(0) }->(); # clobber @args without initialisation
205 ::is $gone, 1, 'caller does not leak @DB::args elems when AvREAL';
208 # And this crashed [perl #93320]:
215 pass 'No crash when @DB::args is freed between caller calls';
219 sub TIEARRAY { bless [] }
222 sub FETCH { $_[0][$_[1]] }
223 sub STORE { $_[0][$_[1]] = $_[2] }
226 eval { sub { () = caller 0; } ->(1..3) };
227 ::like $@, qr "^Cannot set tied \@DB::args at ",
228 'caller dies with tie @DB::args';
229 ::ok tied @args, '@DB::args is still tied';
234 fresh_perl_is <<'END', "ok\n", {},
235 { package foo; sub bar { main::bar() } }
239 my $y = \($1+2); # this is the one that reuses the mem addr, but
240 my $z = \($1+2); # try the others just in case
241 s/2// for $$x, $$y, $$z; # now SvOOK
247 "No crash when freed stash is reused for PV with offset hack";
249 is eval "(caller 0)[6]", "(caller 0)[6]",
250 'eval text returned by caller does not include \n;';
253 is (sub { (caller)[2] }->(), __LINE__,
254 '[perl #115768] caller gets line numbers from nulled cops');
256 # Test it at the end of the program, too.
257 fresh_perl_is(<<'115768', 2, {},
261 sub foo { print +(caller)[2] }
263 '[perl #115768] caller gets line numbers from nulled cops (2)');
265 # PL_linestr should not be modifiable
266 eval '"${;BEGIN{ ${\(caller 2)[6]} = *foo }}"';
267 pass "no assertion failure after modifying eval text via caller";
269 is eval "<<END;\nfoo\nEND\n(caller 0)[6]",
270 "<<END;\nfoo\nEND\n(caller 0)[6]",
271 'here-docs do not gut eval text';
272 is eval "s//<<END/e;\nfoo\nEND\n(caller 0)[6]",
273 "s//<<END/e;\nfoo\nEND\n(caller 0)[6]",
274 'here-docs in quote-like ops do not gut eval text';
276 # The bitmask should be assignable to ${^WARNING_BITS} without resulting in
277 # different warnings settings.
279 my $ bits = sub { (caller 0)[9] }->();
281 local $SIG{__WARN__} = sub { $w++ };
284 BEGIN { ${^WARNING_BITS} = $bits }
290 is $w, 1, 'value from (caller 0)[9] (bitmask) works in ${^WARNING_BITS}';
294 sub getlineno { (caller)[2] }
295 my $line = eval "\n#line 3000000000\ngetlineno();";
296 is $line, "3000000000", "check large line numbers are preserved";
298 # This was fixed with commit d4d03940c58a0177, which fixed bug #78742
299 fresh_perl_is <<'END', "__ANON__::doof\n", {},
302 sub doof { caller(0) }
305 "caller should not SEGV when the current package is undefined";
307 # caller should not SEGV when the eval entry has been cleared #120998
308 fresh_perl_is <<'END', 'main', {},
309 $SIG{__DIE__} = \&dbdie;
316 "caller should not SEGV for eval '' stack frames";
319 local $::TODO = 'RT #7165: line number should be consistent for multiline subroutine calls';
320 fresh_perl_is(<<'EOP', "6\n9\n", {}, 'RT #7165: line number should be consistent for multiline subroutine calls');
322 my ($package, $file, $line) = caller;
334 $::testing_caller = 1;
336 do './op/caller.pl' or die $@;
341 my ($pkg, $file, $line) = caller;
342 ::is $file, 'virtually/op/caller.t', "BEGIN block sees correct caller filename";
343 ::is $line, 12345, "BEGIN block sees correct caller line";
345 local $::TODO = "BEGIN blocks have wrong caller package [perl #129239]";
346 ::is $pkg, 'RT129239', "BEGIN block sees correct caller package";
348 #line 12345 "virtually/op/caller.t"