This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix spelling: precede
[perl5.git] / t / uni / caller.t
CommitLineData
d527ce7c
BF
1#!./perl
2# Tests for caller()
3
4BEGIN {
5 chdir 't' if -d 't';
d527ce7c 6 require './test.pl';
624c42e2 7 set_up_inc('../lib');
d527ce7c
BF
8}
9
10use utf8;
11use open qw( :utf8 :std );
12
c5583fc7
N
13plan( tests => 18 );
14
d527ce7c
BF
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
ee95e30c 28# Bug 20020517.003 (#9367), used to dump core
d527ce7c 29sub foo { @c = caller(0) }
6881372e
FC
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
d527ce7c
BF
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() }
6881372e 62() = *{"foo2"}; # see foo notes above
d527ce7c
BF
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' );