This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
checkAUTHORS.pl once more
[perl5.git] / t / op / caller.t
CommitLineData
07b8c804
RGS
1#!./perl
2# Tests for caller()
3
4BEGIN {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 require './test.pl';
d8c5b3c5 8 plan( tests => 78 );
07b8c804
RGS
9}
10
07b8c804
RGS
11my @c;
12
72699b0f
RGS
13print "# Tests with caller(0)\n";
14
07b8c804
RGS
15@c = caller(0);
16ok( (!@c), "caller(0) in main program" );
17
18eval { @c = caller(0) };
72699b0f
RGS
19is( $c[3], "(eval)", "subroutine name in an eval {}" );
20ok( !$c[4], "hasargs false in an eval {}" );
07b8c804
RGS
21
22eval q{ @c = (Caller(0))[3] };
72699b0f
RGS
23is( $c[3], "(eval)", "subroutine name in an eval ''" );
24ok( !$c[4], "hasargs false in an eval ''" );
07b8c804
RGS
25
26sub { @c = caller(0) } -> ();
72699b0f
RGS
27is( $c[3], "main::__ANON__", "anonymous subroutine name" );
28ok( $c[4], "hasargs true with anon sub" );
07b8c804
RGS
29
30# Bug 20020517.003, used to dump core
31sub foo { @c = caller(0) }
32my $fooref = delete $::{foo};
33$fooref -> ();
72699b0f
RGS
34is( $c[3], "(unknown)", "unknown subroutine name" );
35ok( $c[4], "hasargs true with unknown sub" );
36
37print "# Tests with caller(1)\n";
07b8c804
RGS
38
39sub f { @c = caller(1) }
40
72699b0f
RGS
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
07b8c804 48eval { f() };
72699b0f
RGS
49is( $c[3], "(eval)", "subroutine name in an eval {}" );
50ok( !$c[4], "hasargs false in an eval {}" );
07b8c804
RGS
51
52eval q{ f() };
72699b0f
RGS
53is( $c[3], "(eval)", "subroutine name in an eval ''" );
54ok( !$c[4], "hasargs false in an eval ''" );
07b8c804
RGS
55
56sub { f() } -> ();
72699b0f
RGS
57is( $c[3], "main::__ANON__", "anonymous subroutine name" );
58ok( $c[4], "hasargs true with anon sub" );
07b8c804
RGS
59
60sub foo2 { f() }
61my $fooref2 = delete $::{foo2};
62$fooref2 -> ();
72699b0f
RGS
63is( $c[3], "(unknown)", "unknown subroutine name" );
64ok( $c[4], "hasargs true with unknown sub" );
75b6c4ca
RGS
65
66# See if caller() returns the correct warning mask
67
68sub testwarn {
69 my $w = shift;
2db3864f 70 is( (caller(0))[9], $w, "warnings match caller");
75b6c4ca
RGS
71}
72
73# NB : extend the warning mask values below when new warnings are added
74{
75 no warnings;
2db3864f 76 BEGIN { is( ${^WARNING_BITS}, "\0" x 12, 'all bits off via "no warnings"' ) }
75b6c4ca 77 testwarn("\0" x 12);
2db3864f 78
75b6c4ca 79 use warnings;
ae18ef2b
JH
80 BEGIN { is( ${^WARNING_BITS}, "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\005", 'default bits on via "use warnings"' ); }
81 BEGIN { testwarn("\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\005", "#1"); }
75b6c4ca
RGS
82 # run-time :
83 # the warning mask has been extended by warnings::register
ae18ef2b 84 testwarn("\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x15");
2db3864f 85
75b6c4ca 86 use warnings::register;
ae18ef2b
JH
87 BEGIN { is( ${^WARNING_BITS}, "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x15", 'warning bits on via "use warnings::register"' ) }
88 testwarn("\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x15","#3");
75b6c4ca 89}
f2a7f298 90
91
92# The next two cases test for a bug where caller ignored evals if
93# the DB::sub glob existed but &DB::sub did not (for example, if
94# $^P had been set but no debugger has been loaded). The tests
95# thus assume that there is no &DB::sub: if there is one, they
96# should both pass no matter whether or not this bug has been
97# fixed.
98
99my $debugger_test = q<
100 my @stackinfo = caller(0);
101 return scalar @stackinfo;
102>;
103
104sub pb { return (caller(0))[3] }
105
106my $i = eval $debugger_test;
b3ca2e83 107is( $i, 11, "do not skip over eval (and caller returns 10 elements)" );
f2a7f298 108
109is( eval 'pb()', 'main::pb', "actually return the right function name" );
110
111my $saved_perldb = $^P;
112$^P = 16;
113$^P = $saved_perldb;
114
115$i = eval $debugger_test;
b3ca2e83 116is( $i, 11, 'do not skip over eval even if $^P had been on at some point' );
f2a7f298 117is( eval 'pb()', 'main::pb', 'actually return the right function name even if $^P had been on at some point' );
118
71860c90
NC
119print "# caller can now return the compile time state of %^H\n";
120
d8c5b3c5
NC
121sub hint_exists {
122 my $key = shift;
71860c90
NC
123 my $level = shift;
124 my @results = caller($level||0);
d8c5b3c5 125 exists $results[10]->{$key};
71860c90
NC
126}
127
d8c5b3c5
NC
128sub hint_fetch {
129 my $key = shift;
b3ca2e83
NC
130 my $level = shift;
131 my @results = caller($level||0);
d8c5b3c5 132 $results[10]->{$key};
b3ca2e83 133}
71860c90 134
d8c5b3c5 135$::testing_caller = 1;
a24d89c9 136
e81465be 137do './op/caller.pl' or die $@;