xref: /openbsd/gnu/usr.bin/perl/t/op/caller.t (revision 91f110e0)
1#!./perl
2# Tests for caller()
3
4BEGIN {
5    chdir 't' if -d 't';
6    @INC = '../lib';
7    require './test.pl';
8    plan( tests => 91 );
9}
10
11my @c;
12
13BEGIN { print "# Tests with caller(0)\n"; }
14
15@c = caller(0);
16ok( (!@c), "caller(0) in main program" );
17
18eval { @c = caller(0) };
19is( $c[3], "(eval)", "subroutine name in an eval {}" );
20ok( !$c[4], "hasargs false in an eval {}" );
21
22eval q{ @c = caller(0) };
23is( $c[3], "(eval)", "subroutine name in an eval ''" );
24ok( !$c[4], "hasargs false in an eval ''" );
25
26sub { @c = caller(0) } -> ();
27is( $c[3], "main::__ANON__", "anonymous subroutine name" );
28ok( $c[4], "hasargs true with anon sub" );
29
30# Bug 20020517.003, used to dump core
31sub foo { @c = caller(0) }
32my $fooref = delete $::{foo};
33$fooref -> ();
34is( $c[3], "main::__ANON__", "deleted subroutine name" );
35ok( $c[4], "hasargs true with deleted sub" );
36
37BEGIN {
38 require strict;
39 is +(caller 0)[1], __FILE__,
40  "[perl #68712] filenames after require in a BEGIN block"
41}
42
43print "# Tests with caller(1)\n";
44
45sub f { @c = caller(1) }
46
47sub callf { f(); }
48callf();
49is( $c[3], "main::callf", "subroutine name" );
50ok( $c[4], "hasargs true with callf()" );
51&callf;
52ok( !$c[4], "hasargs false with &callf" );
53
54eval { f() };
55is( $c[3], "(eval)", "subroutine name in an eval {}" );
56ok( !$c[4], "hasargs false in an eval {}" );
57
58eval q{ f() };
59is( $c[3], "(eval)", "subroutine name in an eval ''" );
60ok( !$c[4], "hasargs false in an eval ''" );
61
62sub { f() } -> ();
63is( $c[3], "main::__ANON__", "anonymous subroutine name" );
64ok( $c[4], "hasargs true with anon sub" );
65
66sub foo2 { f() }
67my $fooref2 = delete $::{foo2};
68$fooref2 -> ();
69is( $c[3], "main::__ANON__", "deleted subroutine name" );
70ok( $c[4], "hasargs true with deleted sub" );
71
72# See if caller() returns the correct warning mask
73
74sub show_bits
75{
76    my $in = shift;
77    my $out = '';
78    foreach (unpack('W*', $in)) {
79        $out .= sprintf('\x%02x', $_);
80    }
81    return $out;
82}
83
84sub check_bits
85{
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));
91    }
92}
93
94sub testwarn {
95    my $w = shift;
96    my $id = shift;
97    check_bits( (caller(0))[9], $w, "warnings match caller ($id)");
98}
99
100{
101    no warnings;
102    # Build the warnings mask dynamically
103    my ($default, $registered);
104    BEGIN {
105	for my $i (0..$warnings::LAST_BIT/2 - 1) {
106	    vec($default, $i, 2) = 1;
107	}
108	$registered = $default;
109	vec($registered, $warnings::LAST_BIT/2, 2) = 1;
110    }
111
112    # The repetition number must be set to the value of $BYTES in
113    # lib/warnings.pm
114    BEGIN { check_bits( ${^WARNING_BITS}, "\0" x 14, 'all bits off via "no warnings"' ) }
115    testwarn("\0" x 14, 'no bits');
116
117    use warnings;
118    BEGIN { check_bits( ${^WARNING_BITS}, $default,
119			'default bits on via "use warnings"' ); }
120    BEGIN { testwarn($default, 'all'); }
121    # run-time :
122    # the warning mask has been extended by warnings::register
123    testwarn($registered, 'ahead of w::r');
124
125    use warnings::register;
126    BEGIN { check_bits( ${^WARNING_BITS}, $registered,
127			'warning bits on via "use warnings::register"' ) }
128    testwarn($registered, 'following w::r');
129}
130
131
132# The next two cases test for a bug where caller ignored evals if
133# the DB::sub glob existed but &DB::sub did not (for example, if
134# $^P had been set but no debugger has been loaded).  The tests
135# thus assume that there is no &DB::sub: if there is one, they
136# should both pass  no matter whether or not this bug has been
137# fixed.
138
139my $debugger_test =  q<
140    my @stackinfo = caller(0);
141    return scalar @stackinfo;
142>;
143
144sub pb { return (caller(0))[3] }
145
146my $i = eval $debugger_test;
147is( $i, 11, "do not skip over eval (and caller returns 10 elements)" );
148
149is( eval 'pb()', 'main::pb', "actually return the right function name" );
150
151my $saved_perldb = $^P;
152$^P = 16;
153$^P = $saved_perldb;
154
155$i = eval $debugger_test;
156is( $i, 11, 'do not skip over eval even if $^P had been on at some point' );
157is( eval 'pb()', 'main::pb', 'actually return the right function name even if $^P had been on at some point' );
158
159print "# caller can now return the compile time state of %^H\n";
160
161sub hint_exists {
162    my $key = shift;
163    my $level = shift;
164    my @results = caller($level||0);
165    exists $results[10]->{$key};
166}
167
168sub hint_fetch {
169    my $key = shift;
170    my $level = shift;
171    my @results = caller($level||0);
172    $results[10]->{$key};
173}
174
175{
176    my $tmpfile = tempfile();
177
178    open my $fh, '>', $tmpfile or die "open $tmpfile: $!";
179    print $fh <<'EOP';
180#!perl -wl
181use strict;
182
183{
184    package KAZASH ;
185
186    sub DESTROY {
187	print "DESTROY";
188    }
189}
190
191@DB::args = bless [], 'KAZASH';
192
193print $^P;
194print scalar @DB::args;
195
196{
197    local $^P = shift;
198}
199
200@DB::args = (); # At this point, the object should be freed.
201
202print $^P;
203print scalar @DB::args;
204
205# It shouldn't leak.
206EOP
207    close $fh;
208
209    foreach (0, 1) {
210        my $got = runperl(progfile => $tmpfile, args => [$_]);
211        $got =~ s/\s+/ /gs;
212        like($got, qr/\s*0 1 DESTROY 0 0\s*/,
213             "\@DB::args doesn't leak with \$^P = $_");
214    }
215}
216
217# This also used to leak [perl #97010]:
218{
219    my $gone;
220    sub fwib::DESTROY { ++$gone }
221    package DB;
222    sub { () = caller(0) }->(); # initialise PL_dbargs
223    @args = bless[],'fwib';
224    sub { () = caller(0) }->(); # clobber @args without initialisation
225    ::is $gone, 1, 'caller does not leak @DB::args elems when AvREAL';
226}
227
228# And this crashed [perl #93320]:
229sub {
230  package DB;
231  ()=caller(0);
232  undef *DB::args;
233  ()=caller(0);
234}->();
235pass 'No crash when @DB::args is freed between caller calls';
236
237# This also crashed:
238package glelp;
239sub TIEARRAY { bless [] }
240sub EXTEND   {         }
241sub CLEAR    {        }
242sub FETCH    { $_[0][$_[1]] }
243sub STORE    { $_[0][$_[1]] = $_[2] }
244package DB;
245tie @args, 'glelp';
246eval { sub { () = caller 0; } ->(1..3) };
247::like $@, qr "^Cannot set tied \@DB::args at ",
248              'caller dies with tie @DB::args';
249::ok tied @args, '@DB::args is still tied';
250untie @args;
251package main;
252
253# [perl #113486]
254fresh_perl_is <<'END', "ok\n", {},
255  { package foo; sub bar { main::bar() } }
256  sub bar {
257    delete $::{"foo::"};
258    my $x = \($1+2);
259    my $y = \($1+2); # this is the one that reuses the mem addr, but
260    my $z = \($1+2);  # try the others just in case
261    s/2// for $$x, $$y, $$z; # now SvOOK
262    $x = caller;
263    print "ok\n";
264};
265foo::bar
266END
267    "No crash when freed stash is reused for PV with offset hack";
268
269is eval "(caller 0)[6]", "(caller 0)[6]",
270  'eval text returned by caller does not include \n;';
271
272# PL_linestr should not be modifiable
273eval '"${;BEGIN{  ${\(caller 2)[6]} = *foo  }}"';
274pass "no assertion failure after modifying eval text via caller";
275
276is eval "<<END;\nfoo\nEND\n(caller 0)[6]",
277        "<<END;\nfoo\nEND\n(caller 0)[6]",
278        'here-docs do not gut eval text';
279is eval "s//<<END/e;\nfoo\nEND\n(caller 0)[6]",
280        "s//<<END/e;\nfoo\nEND\n(caller 0)[6]",
281        'here-docs in quote-like ops do not gut eval text';
282
283# The bitmask should be assignable to ${^WARNING_BITS} without resulting in
284# different warnings settings.
285{
286 my $ bits = sub { (caller 0)[9] }->();
287 my $w;
288 local $SIG{__WARN__} = sub { $w++ };
289 eval '
290   use warnings;
291   BEGIN { ${^WARNING_BITS} = $bits }
292   local $^W = 1;
293   () = 1 + undef;
294   $^W = 0;
295   () = 1 + undef;
296 ';
297 is $w, 1, 'value from (caller 0)[9] (bitmask) works in ${^WARNING_BITS}';
298}
299
300$::testing_caller = 1;
301
302do './op/caller.pl' or die $@;
303