File: caller.t

package info (click to toggle)
perl 5.20.2-3%2Bdeb8u11
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 102,964 kB
  • sloc: perl: 555,553; ansic: 214,041; sh: 38,121; pascal: 8,783; cpp: 3,895; makefile: 2,393; xml: 2,325; yacc: 1,741
file content (71 lines) | stat: -rw-r--r-- 1,926 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!./perl
# Tests for caller()

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    require './test.pl';
    plan( tests => 18 );
}

use utf8;
use open qw( :utf8 :std );

package main;

{
    local $@;
    eval 'ok(1);';
    ::like $@, qr/Undefined subroutine &main::ok called at/u;
}
my @c;

sub { @c = caller(0) } -> ();
::is( $c[3], "main::__ANON__", "anonymous subroutine name" );
::ok( $c[4], "hasargs true with anon sub" );

# Bug 20020517.003, used to dump core
sub foo { @c = caller(0) }
my $fooref = delete $main::{foo};
$fooref -> ();
::is( $c[3], "main::__ANON__", "deleted subroutine name" );
::ok( $c[4], "hasargs true with deleted sub" );

print "# Tests with caller(1)\n";

sub f { @c = caller(1) }

sub callf { f(); }
callf();
::is( $c[3], "main::callf", "subroutine name" );
::ok( $c[4], "hasargs true with callf()" );
&callf;
::ok( !$c[4], "hasargs false with &callf" );

eval { f() };
::is( $c[3], "(eval)", "subroutine name in an eval {}" );
::ok( !$c[4], "hasargs false in an eval {}" );

eval q{ f() };
::is( $c[3], "(eval)", "subroutine name in an eval ''" );
::ok( !$c[4], "hasargs false in an eval ''" );

sub { f() } -> ();
::is( $c[3], "main::__ANON__", "anonymous subroutine name" );
::ok( $c[4], "hasargs true with anon sub" );

sub foo2 { f() }
my $fooref2 = delete $main::{foo2};
$fooref2 -> ();
::is( $c[3], "main::__ANON__", "deleted subroutine name" );
::ok( $c[4], "hasargs true with deleted sub" );

sub pb { return (caller(0))[3] }

::is( eval 'pb()', 'main::pb', "actually return the right function name" );

my $saved_perldb = $^P;
$^P = 16;
$^P = $saved_perldb;

::is( eval 'pb()', 'main::pb', 'actually return the right function name even if $^P had been on at some point' );