This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don’t let ?: folding affect truncate
authorFather Chrysostomos <sprout@cpan.org>
Thu, 26 Jul 2012 05:07:12 +0000 (22:07 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Thu, 26 Jul 2012 05:20:15 +0000 (22:20 -0700)
truncate(${\1} ? foo : bar, 0) and truncate(1 ? foo : bar, 0) should
behave the same way, but were treated differently, due to the way ?:
is folded in the latter case.  Now that foldedness is recorded in the
op tree (cc2ebcd7902), we can use the OPpCONST_FOLDED flag to distin-
guish truncate(1 ? foo : bar, 0) from truncate(foo, 0).

op.c
t/comp/fold.t

diff --git a/op.c b/op.c
index 52e600f..b433227 100644 (file)
--- a/op.c
+++ b/op.c
@@ -10031,7 +10031,8 @@ Perl_ck_trunc(pTHX_ OP *o)
        if (kid->op_type == OP_NULL)
            kid = (SVOP*)kid->op_sibling;
        if (kid && kid->op_type == OP_CONST &&
-           (kid->op_private & OPpCONST_BARE))
+           (kid->op_private & (OPpCONST_BARE|OPpCONST_FOLDED))
+                            == OPpCONST_BARE)
        {
            o->op_flags |= OPf_SPECIAL;
            kid->op_private &= ~OPpCONST_STRICT;
index c600067..5d6d9bf 100644 (file)
@@ -4,7 +4,7 @@
 # we've not yet verified that use works.
 # use strict;
 
-print "1..25\n";
+print "1..26\n";
 my $test = 0;
 
 # Historically constant folding was performed by evaluating the ops, and if
@@ -139,3 +139,13 @@ print "ok ", ++$test, " - stat(const ? word : ....)\n";
 # in case we are in t/
 print "not " unless stat(1 ? TEST : 0) eq stat("TEST");
 print "ok ", ++$test, " - stat(const ? word : ....)\n";
+
+# or truncate
+my $n = "for_fold_dot_t$$";
+open F, ">$n" or die "open: $!";
+print F "bralh blah blah \n";
+close F or die "close $!";
+eval "truncate 1 ? $n : 0, 0;";
+print "not " unless -z $n;
+print "ok ", ++$test, " - truncate(const ? word : ...)\n";
+unlink $n;