xref: /openbsd-src/gnu/usr.bin/perl/t/uni/caller.t (revision f3efcd0145415b7d44d9da97e0ad5c21b186ac61)
1#!./perl
2# Tests for caller()
3
4BEGIN {
5    chdir 't' if -d 't';
6    require './test.pl';
7    set_up_inc('../lib');
8}
9
10use utf8;
11use open qw( :utf8 :std );
12
13plan( tests => 18 );
14
15package main;
16
17{
18    local $@;
19    eval 'ok(1);';
20    ::like $@, qr/Undefined subroutine &main::ok called at/u;
21}
22my @c;
23
24sub { @c = caller(0) } -> ();
25::is( $c[3], "main::__ANON__", "anonymous subroutine name" );
26::ok( $c[4], "hasargs true with anon sub" );
27
28# Bug 20020517.003 (#9367), used to dump core
29sub foo { @c = caller(0) }
30# The subroutine only gets anonymised if it is relying on a real GV
31# for its name.
32() = *{"foo"}; # with quotes so that the op tree doesn’t reference the GV
33my $fooref = delete $main::{foo};
34$fooref -> ();
35::is( $c[3], "main::__ANON__", "deleted subroutine name" );
36::ok( $c[4], "hasargs true with deleted sub" );
37
38print "# Tests with caller(1)\n";
39
40sub f { @c = caller(1) }
41
42sub callf { f(); }
43callf();
44::is( $c[3], "main::callf", "subroutine name" );
45::ok( $c[4], "hasargs true with callf()" );
46&callf;
47::ok( !$c[4], "hasargs false with &callf" );
48
49eval { f() };
50::is( $c[3], "(eval)", "subroutine name in an eval {}" );
51::ok( !$c[4], "hasargs false in an eval {}" );
52
53eval q{ f() };
54::is( $c[3], "(eval)", "subroutine name in an eval ''" );
55::ok( !$c[4], "hasargs false in an eval ''" );
56
57sub { f() } -> ();
58::is( $c[3], "main::__ANON__", "anonymous subroutine name" );
59::ok( $c[4], "hasargs true with anon sub" );
60
61sub foo2 { f() }
62() = *{"foo2"}; # see foo notes above
63my $fooref2 = delete $main::{foo2};
64$fooref2 -> ();
65::is( $c[3], "main::__ANON__", "deleted subroutine name" );
66::ok( $c[4], "hasargs true with deleted sub" );
67
68sub pb { return (caller(0))[3] }
69
70::is( eval 'pb()', 'main::pb', "actually return the right function name" );
71
72my $saved_perldb = $^P;
73$^P = 16;
74$^P = $saved_perldb;
75
76::is( eval 'pb()', 'main::pb', 'actually return the right function name even if $^P had been on at some point' );
77