develooper Front page | perl.perl5.porters | Postings from July 2001

turning EXPR into sub { EXPR }

Thread Next
From:
Jeff 'japhy/Marillion' Pinyan
Date:
July 31, 2001 09:37
Subject:
turning EXPR into sub { EXPR }
Message ID:
Pine.GSO.4.21.0107311220460.28213-100000@crusoe.crusoe.net
Could someone lend me a hand in turning an expression (or specifically,
the OP representing it) into an anonymous subroutine?

Here's what I've got so far, which lends itself to weird bugs:

  /* op.c:Perl_ck_subr */
  /* around line 6550 */

  OP* kid = o2;
  OP* sib = kid->op_sibling;
  kid->op_sibling = 0;

  /* japhy */
  o2 = newANONSUB(
    start_subparse(FALSE, CVf_ANON),
    Nullop,
    block_end(block_start(TRUE), kid)
  );

  o2->op_sibling = sib;
  prev->op_sibling = o2;

This is in an attempt to let

  sub foo (&@);

parse

  foo /xyz/, @list;

as it would parse

  foo { /xyz/ } @list;

The thing I want to do is construct a OP_REFGEN op, which I see I can do
using newANONSUB.  The problem is that there seem to be some
subroutine-scope issues around this new code-ref.  For example:

  sub MY_MAP (&@) {
    my $cref = shift;
    for (@_) { print $cref->($_), "\n" }
  }

  MY_MAP /i/, qw( this is bad );

prints

  this1
  is1
  bad

For some reason, the argument itself is being returned in addition to the
return call.  I put in that block_end(block_start ...) stuff in an attempt
to alleviate the condition, but nope.

Any suggestions?

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


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