develooper Front page | perl.perl5.porters | Postings from December 2000

[PATCH blead] Fix problem with `&' prototype

Thread Next
From:
Daniel Chetlin
Date:
December 27, 2000 15:55
Subject:
[PATCH blead] Fix problem with `&' prototype
Message ID:
20001227155532.D9573@darkstar
I noticed this while casting about for a clean solution to the "no
short-circuiting grep" problem.

One can say:

  grep { /foo/ } qw/bar foo bazfoo/;

or

  grep { /foo/ } ('bar', 'foo', 'bazfoo');

And one can say (subroutine cribbed directly from perlsub):

  sub mygrep (&@) {
      my $code = shift;
      my @result;
      foreach $_ (@_) {
          push(@result, $_) if &$code;
      }
      @result;
  }
  mygrep { /foo/ } 'bar', 'foo', 'bazfoo';

But one cannot say:

  mygrep { /foo/ } qw/bar foo bazfoo/;

nor

  mygrep { /foo/ } ('bar', 'foo', 'bazfoo');

Discovering this started me on a journey of several hours and much
frustration, and it resulted in this patch. The patch doesn't break any
current tests for me, and it fixes this problem. However, since before
today I'd never taken a serious look at op.c (let alone toke.c and
perly.y), I don't pretend to be aware of the full ramifications.

Do note that this bug has been around at least since 5.004_05.

Also, thanks are due to Casey and Simon for their encouragement.

-dlc

--- op.c	2000/12/27 23:13:59	1.1
+++ op.c	2000/12/27 23:26:52
@@ -2421,6 +2421,8 @@
 	if (type == OP_LIST) {	/* already a PUSHMARK there */
 	    first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
 	    ((LISTOP*)last)->op_first->op_sibling = first;
+            if (!(first->op_flags & OPf_PARENS))
+                last->op_flags &= ~OPf_PARENS;
 	}
 	else {
 	    if (!(last->op_flags & OPf_KIDS)) {
--- comp/proto.t	2000/12/27 23:35:38	1.1
+++ comp/proto.t	2000/12/27 23:36:18
@@ -19,7 +19,7 @@
 
 use strict;
 
-print "1..123\n";
+print "1..124\n";
 
 my $i = 1;
 
@@ -343,6 +343,7 @@
 
 @array = (qw(O K)," ", $i++);
 sub_array { lc shift } @array;
+sub_array { lc shift } ('O', 'K', ' ', $i++);
 print "\n";
 
 ##

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About