xref: /openbsd/gnu/usr.bin/perl/t/op/caller.t (revision 891d7ab6)
1#!./perl
2# Tests for caller()
3
4BEGIN {
5    chdir 't' if -d 't';
6    @INC = '../lib';
7    require './test.pl';
8    plan( tests => 80 );
9}
10
11my @c;
12
13print "# 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))[3] };
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], "(unknown)", "unknown subroutine name" );
35ok( $c[4], "hasargs true with unknown sub" );
36
37print "# Tests with caller(1)\n";
38
39sub f { @c = caller(1) }
40
41sub callf { f(); }
42callf();
43is( $c[3], "main::callf", "subroutine name" );
44ok( $c[4], "hasargs true with callf()" );
45&callf;
46ok( !$c[4], "hasargs false with &callf" );
47
48eval { f() };
49is( $c[3], "(eval)", "subroutine name in an eval {}" );
50ok( !$c[4], "hasargs false in an eval {}" );
51
52eval q{ f() };
53is( $c[3], "(eval)", "subroutine name in an eval ''" );
54ok( !$c[4], "hasargs false in an eval ''" );
55
56sub { f() } -> ();
57is( $c[3], "main::__ANON__", "anonymous subroutine name" );
58ok( $c[4], "hasargs true with anon sub" );
59
60sub foo2 { f() }
61my $fooref2 = delete $::{foo2};
62$fooref2 -> ();
63is( $c[3], "(unknown)", "unknown subroutine name" );
64ok( $c[4], "hasargs true with unknown sub" );
65
66# See if caller() returns the correct warning mask
67
68sub show_bits
69{
70    my $in = shift;
71    my $out = '';
72    foreach (unpack('W*', $in)) {
73        $out .= sprintf('\x%02x', $_);
74    }
75    return $out;
76}
77
78sub check_bits
79{
80    local $Level = $Level + 2;
81    my ($got, $exp, $desc) = @_;
82    if (! ok($got eq $exp, $desc)) {
83        diag('     got: ' . show_bits($got));
84        diag('expected: ' . show_bits($exp));
85    }
86}
87
88sub testwarn {
89    my $w = shift;
90    my $id = shift;
91    check_bits( (caller(0))[9], $w, "warnings match caller ($id)");
92}
93
94{
95    no warnings;
96    # Build the warnings mask dynamically
97    my ($default, $registered);
98    BEGIN {
99	for my $i (0..$warnings::LAST_BIT/2 - 1) {
100	    vec($default, $i, 2) = 1;
101	}
102	$registered = $default;
103	vec($registered, $warnings::LAST_BIT/2, 2) = 1;
104    }
105    BEGIN { check_bits( ${^WARNING_BITS}, "\0" x 12, 'all bits off via "no warnings"' ) }
106    testwarn("\0" x 12, 'no bits');
107
108    use warnings;
109    BEGIN { check_bits( ${^WARNING_BITS}, $default,
110			'default bits on via "use warnings"' ); }
111    BEGIN { testwarn($default, 'all'); }
112    # run-time :
113    # the warning mask has been extended by warnings::register
114    testwarn($registered, 'ahead of w::r');
115
116    use warnings::register;
117    BEGIN { check_bits( ${^WARNING_BITS}, $registered,
118			'warning bits on via "use warnings::register"' ) }
119    testwarn($registered, 'following w::r');
120}
121
122
123# The next two cases test for a bug where caller ignored evals if
124# the DB::sub glob existed but &DB::sub did not (for example, if
125# $^P had been set but no debugger has been loaded).  The tests
126# thus assume that there is no &DB::sub: if there is one, they
127# should both pass  no matter whether or not this bug has been
128# fixed.
129
130my $debugger_test =  q<
131    my @stackinfo = caller(0);
132    return scalar @stackinfo;
133>;
134
135sub pb { return (caller(0))[3] }
136
137my $i = eval $debugger_test;
138is( $i, 11, "do not skip over eval (and caller returns 10 elements)" );
139
140is( eval 'pb()', 'main::pb', "actually return the right function name" );
141
142my $saved_perldb = $^P;
143$^P = 16;
144$^P = $saved_perldb;
145
146$i = eval $debugger_test;
147is( $i, 11, 'do not skip over eval even if $^P had been on at some point' );
148is( eval 'pb()', 'main::pb', 'actually return the right function name even if $^P had been on at some point' );
149
150print "# caller can now return the compile time state of %^H\n";
151
152sub hint_exists {
153    my $key = shift;
154    my $level = shift;
155    my @results = caller($level||0);
156    exists $results[10]->{$key};
157}
158
159sub hint_fetch {
160    my $key = shift;
161    my $level = shift;
162    my @results = caller($level||0);
163    $results[10]->{$key};
164}
165
166{
167    my $tmpfile = tempfile();
168
169    open my $fh, '>', $tmpfile or die "open $tmpfile: $!";
170    print $fh <<'EOP';
171#!perl -wl
172use strict;
173
174{
175    package KAZASH ;
176
177    sub DESTROY {
178	print "DESTROY";
179    }
180}
181
182@DB::args = bless [], 'KAZASH';
183
184print $^P;
185print scalar @DB::args;
186
187{
188    local $^P = shift;
189}
190
191@DB::args = (); # At this point, the object should be freed.
192
193print $^P;
194print scalar @DB::args;
195
196# It shouldn't leak.
197EOP
198    close $fh;
199
200    foreach (0, 1) {
201        my $got = runperl(progfile => $tmpfile, args => [$_]);
202        $got =~ s/\s+/ /gs;
203        like($got, qr/\s*0 1 DESTROY 0 0\s*/,
204             "\@DB::args doesn't leak with \$^P = $_");
205    }
206}
207
208$::testing_caller = 1;
209
210do './op/caller.pl' or die $@;
211