This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove workaround for distros needing dot in @INC
[perl5.git] / t / uni / caller.t
1 #!./perl
2 # Tests for caller()
3
4 BEGIN {
5     chdir 't' if -d 't';
6     require './test.pl';
7     set_up_inc('../lib');
8 }
9
10 use utf8;
11 use open qw( :utf8 :std );
12
13 plan( tests => 18 );
14
15 package main;
16
17 {
18     local $@;
19     eval 'ok(1);';
20     ::like $@, qr/Undefined subroutine &main::ok called at/u;
21 }
22 my @c;
23
24 sub { @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
29 sub 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
33 my $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
38 print "# Tests with caller(1)\n";
39
40 sub f { @c = caller(1) }
41
42 sub callf { f(); }
43 callf();
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
49 eval { f() };
50 ::is( $c[3], "(eval)", "subroutine name in an eval {}" );
51 ::ok( !$c[4], "hasargs false in an eval {}" );
52
53 eval q{ f() };
54 ::is( $c[3], "(eval)", "subroutine name in an eval ''" );
55 ::ok( !$c[4], "hasargs false in an eval ''" );
56
57 sub { f() } -> ();
58 ::is( $c[3], "main::__ANON__", "anonymous subroutine name" );
59 ::ok( $c[4], "hasargs true with anon sub" );
60
61 sub foo2 { f() }
62 () = *{"foo2"}; # see foo notes above
63 my $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
68 sub pb { return (caller(0))[3] }
69
70 ::is( eval 'pb()', 'main::pb', "actually return the right function name" );
71
72 my $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' );