Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8487809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:21:22+00:00 2026-06-10T21:21:22+00:00

(EDIT: The pipe function below should return a blessed object for overloading to work

  • 0

(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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:21:23+00:00Added an answer on June 10, 2026 at 9:21 pm
    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")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: See my answer below--> I am wanting to have a view that when
I'm trying to pipe the output of the parts-of-speech function into the index-words function
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
Edit : Note that, as Daniel and latkin noted in an answer and a
I am using VS2005 c#. I have this function for converting pipe delimited text
I am trying to save a pipe delimited text file into an excel worksheet
I am trying to pipe the output of a tail command into another bash
Edit: My comment below regarding sed 's@^@ @' <(f1) is incorrect While $BASH_SUBSHELL indicates
I want to pipe [edit: real-time text] the output of several subprocesses (sometimes chained,
Using Vim, I'm trying to pipe text selected in visual mode to a UNIX

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.