13 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))[3] };
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, used to dump core
31 sub foo { @c = caller(0) }
32 my $fooref = delete $::{foo};
34 is( $c[3], "(unknown)", "unknown subroutine name" );
35 ok( $c[4], "hasargs true with unknown sub" );
37 print "# Tests with caller(1)\n";
39 sub f { @c = caller(1) }
43 is( $c[3], "main::callf", "subroutine name" );
44 ok( $c[4], "hasargs true with callf()" );
46 ok( !$c[4], "hasargs false with &callf" );
49 is( $c[3], "(eval)", "subroutine name in an eval {}" );
50 ok( !$c[4], "hasargs false in an eval {}" );
53 is( $c[3], "(eval)", "subroutine name in an eval ''" );
54 ok( !$c[4], "hasargs false in an eval ''" );
57 is( $c[3], "main::__ANON__", "anonymous subroutine name" );
58 ok( $c[4], "hasargs true with anon sub" );
61 my $fooref2 = delete $::{foo2};
63 is( $c[3], "(unknown)", "unknown subroutine name" );
64 ok( $c[4], "hasargs true with unknown sub" );
66 # See if caller() returns the correct warning mask
70 is( (caller(0))[9], $w, "warnings match caller");
73 # NB : extend the warning mask values below when new warnings are added
76 BEGIN { is( ${^WARNING_BITS}, "\0" x 12, 'all bits off via "no warnings"' ) }
80 BEGIN { is( ${^WARNING_BITS}, "UUUUUUUUUUU\025", 'default bits on via "use warnings"' ); }
81 BEGIN { testwarn("UUUUUUUUUUU\025", "#1"); }
83 # the warning mask has been extended by warnings::register
84 testwarn("UUUUUUUUUUUU");
86 use warnings::register;
87 BEGIN { is( ${^WARNING_BITS}, "UUUUUUUUUUUU", 'warning bits on via "use warnings::register"' ) }
88 testwarn("UUUUUUUUUUUU","#3");
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
99 my $debugger_test = q<
100 my @stackinfo = caller(0);
101 return scalar @stackinfo;
104 sub pb { return (caller(0))[3] }
106 my $i = eval $debugger_test;
107 is( $i, 11, "do not skip over eval (and caller returns 10 elements)" );
109 is( eval 'pb()', 'main::pb', "actually return the right function name" );
111 my $saved_perldb = $^P;
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' );
119 print "# caller can now return the compile time state of %^H\n";
123 my @results = caller($level||0);
129 my @results = caller($level||0);
130 $results[10]->{dooot};
135 my @results = caller($level||0);
136 $results[10]->{thikoosh};
140 is(get_dooot(), undef);
141 is(get_thikoosh(), undef);
142 my $hash = get_hash();
143 ok(!exists $hash->{dooot});
144 ok(!exists $hash->{thikoosh});
145 is(get_dooot(1), 54);
149 is(get_dooot(), 6 * 7);
150 is(get_dooot(1), 54);
155 is(get_dooot(), undef);
157 ok(exists $hash->{dooot});
162 is(get_dooot(), undef);
164 ok(!exists $hash->{dooot});
165 is(get_dooot(1), 54);
168 is(get_dooot(), undef);
169 is(get_thikoosh(), undef);
172 $^H{thikoosh} = "SKREECH";
175 is(get_thikoosh(), "SKREECH");
186 is(get_thikoosh(), "SKREECH");
191 is(get_dooot(), undef);
192 my $hash = get_hash();
193 ok(!exists $hash->{dooot});
194 is(get_thikoosh(), "SKREECH");
198 is(get_dooot(), 6 * 7);
199 is(get_thikoosh(), "SKREECH");
201 is(get_dooot(), 6 * 7);
202 is(get_thikoosh(), "SKREECH");
205 print "# which now works inside evals\n";
211 is(get_dooot(), 6 * 7);
213 eval "is(get_dooot(), 6 * 7); 1" or die $@;
215 eval <<'EOE' or die $@;
216 is(get_dooot(), 6 * 7);
217 eval "is(get_dooot(), 6 * 7); 1" or die $@;
222 eval "is(get_dooot(), 54); 1" or die $@;
223 eval 'BEGIN { $^H{dooot} = -1; }; 1' or die $@;
225 eval "is(get_dooot(), 54); 1" or die $@;
231 $^H{dooot} = "FIP\0FOP\0FIDDIT\0FAP";
233 is(get_dooot(), "FIP\0FOP\0FIDDIT\0FAP", "Can do embedded 0 bytes");
236 $^H{dooot} = chr 256;
238 is(get_dooot(), chr 256, "Can do Unicode");
243 is(get_dooot(), -42, "Can do IVs");
248 cmp_ok(get_dooot(), '>', 42, "Can do UVs");
266 is(get_hash()->{$k1}, 2, "UTF-8 or not, it's the same");
267 is(get_hash()->{$k2}, 2, "UTF-8 or not, it's the same");
268 is(get_hash()->{$k3}, 3, "Octect sequences and UTF-8 are distinct");