6

(EDIT: The pipe function below should return a blessed object for overloading to work correctly. See the accepted answer.)

I'm trying to use perl's overload capability to build up a simple parse tree. I don't need much - in fact, all I need is one operator that is left-associative. But there seems to an inconsistency in the way perl parses $x op $y versus a longer chain like $x op $y op $z op ....

Here's what I have:

package foo;

use overload '|' => \&pipe,
             "**" => \&pipe,
             ">>" => \&pipe;

sub pipe { [ $_[0], $_[1] ] }

package main;

my $x = bless ["x"], "foo";
my $y = bless ["y"], "foo";
my $z = bless ["z"], "foo";
my $w = bless ["w"], "foo";

                               # how perl parses it:
my $p2 = $x | $y;              # Cons x y
my $p3 = $x | $y | $z;         # Cons z (Cons x y)
my $p4 = $x | $y | $z | $w;    # Cons w (Cons z (Cons x y))
my $p5 = $z | ($x | $y);       # same as p3???

my $s2 = $x ** $y;             # Cons x y
my $s3 = $x ** $y ** $z;       # Cons x (Cons y z)
my $s4 = $x ** $y ** $z ** $w; # Cons x (Cons y (Cons z w))

sub d { Dumper(\@_) }

say "p2 = ".d($p2);
say "p3 = ".d($p3);
say "p4 = ".d($p4);
say "p5 = ".d($p5);

say "s2 = ".d($s2);
say "s3 = ".d($s3);
say "s4 = ".d($s4);

The output is something like:

p2 = [bless( ['x'], 'foo' ),bless( ['y'], 'foo' )]
p3 = [bless( ['z'], 'foo' ),[bless( ['x'], 'foo' ),bless( ['y'], 'foo' )]]
p4 = [bless( ['w'], 'foo' ),[bless( ['z'], 'foo' ),[bless( ['x'], 'foo' ),bless( ['y'], 'foo' )]]]
p5 = [bless( ['z'], 'foo' ),[bless( ['x'], 'foo' ),bless( ['y'], 'foo' )]]

s2 = [bless( ['x'], 'foo' ),bless( ['y'], 'foo' )]
s3 = [bless( ['x'], 'foo' ),[bless( ['y'], 'foo' ),bless( ['z'], 'foo' )]]
s4 = [bless( ['x'], 'foo' ),[bless( ['y'], 'foo' ),[bless( ['z'], 'foo' ),bless( ['w'], 'foo' )]]]

Shouldn't p2 have x and y reversed to be consistent with the other cases? Note that p3 and p5 produce the same output - so how I can tell them apart?

I don't see the same problem with the right-associative operator **.

Is there a work around for this?

3 Answers 3

3
use feature ":5.14";
use warnings FATAL => qw(all);
use strict;
use Data::Dump qw(dump pp);

sub foo() 
 {package foo;

  use overload '|' => \&p;

  sub p {bless [@{$_[0]},@{$_[1]}]}
 }

my $x = bless ["x"], "foo";
my $y = bless ["y"], "foo";
my $z = bless ["z"], "foo";

my $p = $x | $y | $z;

pp($p)

Produces:

bless(["x", "y", "z"], "foo")
1
  • Yes - I should have pipe return a blessed object.
    – ErikR
    Sep 6, 2012 at 16:08
2

Overloaded operator handlers sometimes receive operands in reversed order, but Perl will notify the handler when it does so by setting the swapped argument to true.

overload:

Three arguments are passed to all subroutines specified in the use overload directive (with one exception - see nomethod). [...] The third argument is set to TRUE if (and only if) the two operands have been swapped. Perl may do this to ensure that the first argument ($self ) is an object implementing the overloaded operation, in line with general object calling conventions. [...]

You disregarded the third argument passed to the handler. The underlying problem is that you forgot to return a foo object from pipe.

0
1

You should check Marpa, I think it is better in parsing things like this.

http://blogs.perl.org/users/jeffrey_kegler/2010/05/bnf-parsing-and-linear-time.html

https://metacpan.org/pod/Marpa::PP

I think it is a typo in your script:

my $p3 = $x || $y || $z;
1
  • It's not a typo - | is the "bit-wise or" operator, || is the "logical or" operator. Also, I'm looking to create a new operation on a new data type which is why I'm using overloading.
    – ErikR
    Sep 6, 2012 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.