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)

Wednesday June 24, 2009
08:35 AM

Basic OO-features in Pipp

When starting on the ReflectionExtension class for Pipp, I got reminded that some very basic OO-features were not working yet. The good think is that I can all that stuff from Rakudo. So simple inheritance and reading member of class instances are working now.

I also simplified my Test.php. The current test number is now tracked in a global variable. Before that change, the test number had to be passed in from the test script.

Sunday June 07, 2009
06:45 AM

My first PHP extension

PHP Extensions are usually written C and they usually export at least one constant, function, class, resource type or stream to the PHP userspace. So they are pretty much the same as XS-Modules in Perl 5. Extensive information about PHP extension can be found in Sara Golemons book 'Extending and Embedding PHP'.

The several hundred standard functions of PHP are implemented in terms of extensions. Pipp, http://pipp.org/, either needs to reimplement this massive count of functions, or support extensions natively. Being lazy, native support for extensions is the way to go. So let's see how to create a dummy PHP extension. The following is for Linux and is based on the tutorial http://devzone.zend.com/article/1021-Extension-Writing-Part-I-Introduction-to-PH P-and-Zend.

The first step is to build PHP 5.3 with development support. The source can be checked out from a CVS repository. See http://www.php.net/anoncvs.php for details and the password for anonymous CVS access.

  mkdir ~/first_php_extension
  cd ~/first_php_extension
  cvs -d :pserver:cvsread@cvs.php.net:/repository login
  cvs -d :pserver:cvsread@cvs.php.net:/repository checkout -r PHP_5_3 php5

Buildconf performs some checks and creates a configure script for the GNU autotools. 're2c' is the parser generator used by PHP 5.3. It is not build dependency, as the generated C-files are in the repository. There is also a message about 'autotools 2.13' being recommended, but I simply ignore that.

  cd ~/first_php_extension/php5
  ./buildconf

For building extensions we need an installed PHP and it's associated helper scripts. Let's install our new PHP next to the source, so that the system PHP is left undisturbed.

  mkdir ~/first_php_extension/installed
  ./configure --prefix=/home/bernhard/first_php_extension/installed --enable-debug --enable-maintainer-zts --enable-embed
  make test
  make install
  export PATH=~/first_php_extension/installed/bin:$PATH

This should leave us with PHP 5.3 with debug support.

  bernhard@heist:~/first_php_extension/php5$ php --version
  PHP 5.3.0RC3-dev (cli) (built: Jun  7 2009 12:36:41) (DEBUG)
  Copyright (c) 1997-2009 The PHP Group
  Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

The next step is to create a dummy extension without specific functionality. The helper script 'ext_skel' creates a stub in the directory 'pipp_sample'.

  cd ~/first_php_extension/php5/ext
  ./ext_skel --extname=pipp_sample

For now I leave pipp_sample.c alone, but config.m4 needs to be edited. Instructions can be found in config.m4 itself, I ended up with uncommenting the lines 16, 18, 60.

  cd ~/first_php_extension/php5/ext/pipp_sample
  vi config.m4

The helper phpize takes info from the PHP installation and creates more files, including a configure script.

  phpize

Now the dummy extension can be configured and compiled.

  ./configure --enable-pipp_sample
  make test

A sanity test can be done with:

  php pipp_sample.php

Now let's copy the generated shared lib for use by the installed PHP.

  make install

The shared library could also be load by Pipp. Let's take a look at the function.

  bernhard@heist:~$ nm /home/bernhard/first_php_extension/installed/lib/php/extensions/debug-zts-200901 15/pipp_sample.so | grep ' U '
  U php_info_print_table_end
  U php_info_print_table_header
  U php_info_print_table_start
  U spprintf
  U zend_parse_parameters

The undefined functions are provided by the PHP extension API. These are also the functions that need to be implemented for Pipp.

Thursday March 05, 2009
03:27 PM

Declaring lexicals within $sth-bind_columns()

Lately I have taken on the maintainance of a legacy Perl 5 script. The script heavily uses the DBI function bind_columns() for retrieving data from a relational database. After some refaktoring I ended up with something like this:

my $sth = $dbh->prepare(<<'END_SQL');
SELECT color, food, num_legs
  FROM pet
END_SQL
my ( $food, $something_else, $color, $num_legs);
$sth->bind_columns( \$color, \$food, $num_legs );

Not being entirely happy with I went one step further and got:

my $sth = $dbh->prepare(<<'END_SQL');
SELECT color, food, num_legs
  FROM pet
END_SQL
$sth->bind_columns( \( my ( $color, $food, $num_legs) ) ):
my ( $something_else );

This style enforces some structure in the declaration. Is there a way to make this really nice? Installing a module is no option in this context.

Sunday February 15, 2009
08:18 AM

Pipp has left the nest and moved to github

Pipp, that is PHP on Parrot, has moved to github, http://github.com/bschmalhofer/pipp/.

I have not yet tested with an installed Parrot yet. So for now please check out Pipp in a Parrot source dir. See http://wiki.github.com/bschmalhofer/pipp for details.

If you want to play with a cooler language than PHP, then take a look at https://trac.parrot.org/parrot/wiki/Languages.

Sunday January 25, 2009
03:15 PM

Moved Unlambda and Lazy K to github

Unlambda, http://www.madore.org/~david/programs/unlambda/, and Lazy K, http://esolangs.org/wiki/Lazy_K, are two pure functional esoteric programming languages. Leo Tötsch implemented them for Parrot. As language implementation are encouraged to leave the nest, I moved them over to Github, http://github.com/bschmalhofer.

See https://trac.parrot.org/parrot/wiki/Languages for the current list of languages for Parrot.

Sunday January 18, 2009
07:38 AM

Parrot m4 has left the nest

Parrot m4 is an incomplete implementation of GNU m4 on top of parrot. As language implementations are encouraged to leave Parrot's svn, I have moved the code and the history over to http://github.com/bschmalhofer/m4/. The source can easily be fetched by typing 'make co-m4' in the 'languages' directory of a Parrot working copy.

I am no longer working on m4, so anybody is welcome to clone it. However I think that porting GNU m4 to Perl 6 would be the more interesting task these days.

The mostly up-to-date list of Parrot HLLs is at https://trac.parrot.org/parrot/wiki/Languages.

Monday January 12, 2009
04:58 PM

HQ9+ left the nest

HQ9+, http://www.esolangs.org/wiki/HQ9_Plus, is a little language the makes a few things easy, and almost all other things impossible.

There is an implementation of HQ9+ for Parrot that is based on the Parrot Compiler Toolkit. For getting my feet wet with git, it moved the source code from Parrot's svn repository to http://github.com/bschmalhofer/hq9plus/tree/master. The migration went very smoothly and git made a good initial impression on me. The instructions on github.com were very helpful. Details on the migration are on https://trac.parrot.org/parrot/wiki/LeaveTheNest.

In this migration I simply discarded the svn commit history. For moving other languages to git, it would be handy to have a complete and current git mirror of Parrot's svn repository.

Thursday January 01, 2009
11:57 AM

Closures for Pipp

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.

Sunday December 21, 2008
07:04 AM

Pipp is now more boring

Comparing different parsing and tree transformation techniques was for me a major reason for starting work on Pipp. Therefore Pipp had until now support for different frontends:

Antlr 3
Parsing and tree transformation with Java-based Antlr 3
PHC
Take XML-output from phc and transform it with XSLT
PCT
Parrot Compiler Toolkit

Lately I found that I only worked on the PCT variant. So in order to keep it simple, especially for new contributors, I removed support for Antlr3 and PHC.

So, Pipp is now more boring, but other exciting things are coming.

Thursday November 27, 2008
03:36 PM

Rewriting Pipp tests in PHP

Pipp is Parrot's PHP and it has small test suite that lives alongside the code in the Parrot repository. Most of the test scripts are written in Perl 5 using the Parrot::Test modules. So usually I tell Pipp to run some PHP code and check whether the expected result is printed. This works fine but is not very exiting.

More exciting, for some definition of exciting, is to run PHP scripts and emit TAP with a testing lib implemented in PHP. So Pipp this needs

  1. user defined functions with param passing
  2. global for keeping track of the running test count
  3. module loading

Stealing from Rakudo I now got most of that. global is still missing, so currently I pass in the current test number and increment it outside the testing functions.

Over the next days I plan to port a selection of the easier scripts to PHP. Things like TODO, SKIP and regex matching have to wait for later. Takers welcome!