Serialise changes to %^H onto the current COP. Return the compile time
[p5sagit/p5-mst-13.2.git] / t / op / caller.t
1 #!./perl
2 # Tests for caller()
3
4 BEGIN {
5     chdir 't' if -d 't';
6     @INC = '../lib';
7     require './test.pl';
8     plan( tests => 48 );
9 }
10
11 my @c;
12
13 print "# Tests with caller(0)\n";
14
15 @c = caller(0);
16 ok( (!@c), "caller(0) in main program" );
17
18 eval { @c = caller(0) };
19 is( $c[3], "(eval)", "subroutine name in an eval {}" );
20 ok( !$c[4], "hasargs false in an eval {}" );
21
22 eval q{ @c = (Caller(0))[3] };
23 is( $c[3], "(eval)", "subroutine name in an eval ''" );
24 ok( !$c[4], "hasargs false in an eval ''" );
25
26 sub { @c = caller(0) } -> ();
27 is( $c[3], "main::__ANON__", "anonymous subroutine name" );
28 ok( $c[4], "hasargs true with anon sub" );
29
30 # Bug 20020517.003, used to dump core
31 sub foo { @c = caller(0) }
32 my $fooref = delete $::{foo};
33 $fooref -> ();
34 is( $c[3], "(unknown)", "unknown subroutine name" );
35 ok( $c[4], "hasargs true with unknown sub" );
36
37 print "# Tests with caller(1)\n";
38
39 sub f { @c = caller(1) }
40
41 sub callf { f(); }
42 callf();
43 is( $c[3], "main::callf", "subroutine name" );
44 ok( $c[4], "hasargs true with callf()" );
45 &callf;
46 ok( !$c[4], "hasargs false with &callf" );
47
48 eval { f() };
49 is( $c[3], "(eval)", "subroutine name in an eval {}" );
50 ok( !$c[4], "hasargs false in an eval {}" );
51
52 eval q{ f() };
53 is( $c[3], "(eval)", "subroutine name in an eval ''" );
54 ok( !$c[4], "hasargs false in an eval ''" );
55
56 sub { f() } -> ();
57 is( $c[3], "main::__ANON__", "anonymous subroutine name" );
58 ok( $c[4], "hasargs true with anon sub" );
59
60 sub foo2 { f() }
61 my $fooref2 = delete $::{foo2};
62 $fooref2 -> ();
63 is( $c[3], "(unknown)", "unknown subroutine name" );
64 ok( $c[4], "hasargs true with unknown sub" );
65
66 # See if caller() returns the correct warning mask
67
68 sub testwarn {
69     my $w = shift;
70     is( (caller(0))[9], $w, "warnings match caller");
71 }
72
73 # NB : extend the warning mask values below when new warnings are added
74 {
75     no warnings;
76     BEGIN { is( ${^WARNING_BITS}, "\0" x 12, 'all bits off via "no warnings"' ) }
77     testwarn("\0" x 12);
78
79     use warnings;
80     BEGIN { is( ${^WARNING_BITS}, "UUUUUUUUUUU\025", 'default bits on via "use warnings"' ); }
81     BEGIN { testwarn("UUUUUUUUUUU\025", "#1"); }
82     # run-time :
83     # the warning mask has been extended by warnings::register
84     testwarn("UUUUUUUUUUUU");
85
86     use warnings::register;
87     BEGIN { is( ${^WARNING_BITS}, "UUUUUUUUUUUU", 'warning bits on via "use warnings::register"' ) }
88     testwarn("UUUUUUUUUUUU","#3");
89 }
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
99 my $debugger_test =  q<
100     my @stackinfo = caller(0);
101     return scalar @stackinfo;
102 >;
103
104 sub pb { return (caller(0))[3] }
105
106 my $i = eval $debugger_test;
107 is( $i, 11, "do not skip over eval (and caller returns 10 elements)" );
108
109 is( eval 'pb()', 'main::pb', "actually return the right function name" );
110
111 my $saved_perldb = $^P;
112 $^P = 16;
113 $^P = $saved_perldb;
114
115 $i = eval $debugger_test;
116 is( $i, 11, 'do not skip over eval even if $^P had been on at some point' );
117 is( eval 'pb()', 'main::pb', 'actually return the right function name even if $^P had been on at some point' );
118
119 # caller can now return the compile time state of %^H
120 sub get_dooot {
121     my $level = shift;
122     my @results = caller($level||0);
123     $results[10]->{dooot};
124 }
125 sub get_hash {
126     my $level = shift;
127     my @results = caller($level||0);
128     $results[10];
129 }
130 sub dooot {
131     is(get_dooot(), undef);
132     my $hash = get_hash();
133     ok(!exists $hash->{dooot});
134     is(get_dooot(1), 54);
135     BEGIN {
136         $^H{dooot} = 42;
137     }
138     is(get_dooot(), 6 * 7);
139     is(get_dooot(1), 54);
140
141     BEGIN {
142         $^H{dooot} = undef;
143     }
144     is(get_dooot(), undef);
145     $hash = get_hash();
146     ok(exists $hash->{dooot});
147
148     BEGIN {
149         delete $^H{dooot};
150     }
151     is(get_dooot(), undef);
152     $hash = get_hash();
153     ok(!exists $hash->{dooot});
154     is(get_dooot(1), 54);
155 }
156 {
157     is(get_dooot(), undef);
158     BEGIN {
159         $^H{dooot} = 1;
160     }
161         is(get_dooot(), 1);
162
163     BEGIN {
164         $^H{dooot} = 42;
165     }
166     {
167         {
168             BEGIN {
169                 $^H{dooot} = 6 * 9;
170             }
171             is(get_dooot(), 54);
172             {
173                 BEGIN {
174                     delete $^H{dooot};
175                 }
176                 is(get_dooot(), undef);
177                 my $hash = get_hash();
178                 ok(!exists $hash->{dooot});
179             }
180             dooot();
181         }
182         is(get_dooot(), 6 * 7);
183     }
184     is(get_dooot(), 6 * 7);
185 }