This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp_ctl.c: pp_caller UTF8 cleanup.
authorBrian Fraser <fraserbn@gmail.com>
Mon, 26 Sep 2011 22:32:45 +0000 (15:32 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Thu, 6 Oct 2011 20:01:09 +0000 (13:01 -0700)
MANIFEST
pp_ctl.c
t/uni/caller.t [new file with mode: 0644]

index 81cfeee..b7258eb 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -5234,6 +5234,7 @@ t/test.pl                 Simple testing library
 t/thread_it.pl                 Run regression tests in a new thread
 t/uni/bless.t                  See if Unicode bless works
 t/uni/cache.t                  See if Unicode swash caching works
+t/uni/caller.t                 See if Unicode doesn't get mangled in caller()
 t/uni/case.pl                  See if Unicode casing works
 t/uni/chomp.t                  See if Unicode chomp works
 t/uni/chr.t                    See if Unicode chr works
index 0364723..2d8c4f2 100644 (file)
--- a/pp_ctl.c
+++ b/pp_ctl.c
@@ -1869,7 +1869,7 @@ PP(pp_caller)
     register const PERL_CONTEXT *cx;
     const PERL_CONTEXT *dbcx;
     I32 gimme;
-    const char *stashname;
+    const HEK *stash_hek;
     I32 count = 0;
     bool has_arg = MAXARG && TOPs;
 
@@ -1888,14 +1888,14 @@ PP(pp_caller)
        RETURN;
     }
 
-    stashname = CopSTASHPV(cx->blk_oldcop);
+    stash_hek = HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop));
     if (GIMME != G_ARRAY) {
         EXTEND(SP, 1);
-       if (!stashname)
+       if (!stash_hek)
            PUSHs(&PL_sv_undef);
        else {
            dTARGET;
-           sv_setpv(TARG, stashname);
+           sv_sethek(TARG, stash_hek);
            PUSHs(TARG);
        }
        RETURN;
@@ -1903,10 +1903,13 @@ PP(pp_caller)
 
     EXTEND(SP, 11);
 
-    if (!stashname)
+    if (!stash_hek)
        PUSHs(&PL_sv_undef);
-    else
-       mPUSHs(newSVpv(stashname, 0));
+    else {
+       dTARGET;
+       sv_sethek(TARG, stash_hek);
+       PUSHTARG;
+    }
     mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
     mPUSHi((I32)CopLINE(cx->blk_oldcop));
     if (!has_arg)
diff --git a/t/uni/caller.t b/t/uni/caller.t
new file mode 100644 (file)
index 0000000..b98ae07
--- /dev/null
@@ -0,0 +1,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' );