mirror of
https://github.com/Perl/perl5.git
synced 2026-01-26 08:38:23 +00:00
77 lines
2.1 KiB
Perl
77 lines
2.1 KiB
Perl
#!./perl
|
||
# Tests for caller()
|
||
|
||
BEGIN {
|
||
chdir 't' if -d 't';
|
||
require './test.pl';
|
||
set_up_inc('../lib');
|
||
}
|
||
|
||
use utf8;
|
||
use open qw( :utf8 :std );
|
||
|
||
plan( tests => 18 );
|
||
|
||
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 (#9367), used to dump core
|
||
sub foo { @c = caller(0) }
|
||
# The subroutine only gets anonymised if it is relying on a real GV
|
||
# for its name.
|
||
() = *{"foo"}; # with quotes so that the op tree doesn’t reference the GV
|
||
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() }
|
||
() = *{"foo2"}; # see foo notes above
|
||
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' );
|