(EDIT: the pipe function below should return a blessed object for the overload to work properly). See accepted answer.)
I am trying to use perl overload to create a simple parsing tree. I donβt need much - in fact I need only one operator, which is left-associative. But there seems to be an inconsistency in how perl parses $x op $y is compared to a longer one like $x op $y op $z op ...
Here is 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";
The result looks something like this:
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' )]]]
Should p2 change x and y to suit other cases? Note that p3 and p5 produce the same result - so how can I tell them separately?
I do not see the same problem with the right-associative operator ** .
Is there any work for this?