Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Bernhard (4508)

Bernhard
  Bernhard.Schmalhofer@gmx.de
https://www.xing ... hard_Schmalhofer

A freelance software engineer with experience in webapplications, databases and bioinformatics.

A contributor to Parrot and the person behind Pipp.

A physicist who worked with third sound in Helium-III and CCD X-ray detectors.

barney on irc.perl.org
github: http://github.com/bschmalhofer [github.com]
LinkedIn:http://www.linkedin.com/in/bernhardschmalhofer [linkedin.com]
XING: https://www.xing.com/profile/Bernhard_Schmalhofer [xing.com]

Journal of Bernhard (4508)

Thursday January 01, 2009
11:57 AM

Closures for Pipp

[ #38198 ]

In the last days I have been working on supporting closures for Pipp. Yes, you've read correctly. The next version of PHP will support real closures. See http://www.ibm.com/developerworks/opensource/library/os-php-5.3new2/index.html?c a=drs-tp5008 for a nice writeup.

Closures are connected with lexical variables, so I had I had to rethink the way I handle variables in Pipp. Initially I had the variables outside functions as package variables. Since this doesn't play well with closures, and as there are problems with included files, I changed those to lexical variables. This was the major part of the work. Thanks to Patrich Michaud for advising me where I should define the lexicals.

After switching to lexicals everywhere I followed my usual approach and tried to do the same as Rakudo does. This worked without hassle for a simple case.

<?php

  function gen_indentor ( ) {
      $indention = '+';
      $indentor = function ($line) use ($indention) {
          echo $indention . $line . "\n";
      };

      return $indentor;
  }

  $sub_1 = gen_indentor();

  $sub_1('one plus');

?>

correctly gives:

+one plus

But when I create a second closure I run into problems.

<?php

  function gen_indentor ($indention) {
      $indentor = function ($line) use ($indention) {
          echo $indention . $line . "\n";
      };

      return $indentor;
  }

  $sub_1 = gen_indentor('+');
  $sub_4 = gen_indentor('++++');

  $sub_1('one plus');
  $sub_2('four plusses');

?>

incorrectly gives:

++++one plus
++++four plusses

Looks like I need to fiddle some more.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.