A pipe operator exists on perl v5.42?

You know how many languages have a "pipe" operator, either ready or in the making? Like PHP, here, for example: https://laravel-news.com/the-pipe-operator-is-coming-to-php-85

Well, Perl v5.42 (almost) has that too! Check these examples:


$ perl -E 'say "Alexander"->&CORE::substr(1, 3);'
lex

$ perl -E 'say ","->&CORE::join(qw/ 10 20 30 /);'
10,20,30

I believe this would work with any user defined or imported subroutine too, instead of the core functions (there you get to omit the "CORE::").

3 Comments

It is not a 5.42 thing:


$ perl -E 'print "lbarfThis is perl versionblarfAlexander"->CORE::substr(5, 20);print " ",$^V'
This is perl version v5.16.3

This is just normal Perl 5 behavior, although your need v5.16 or later for CORE.

Here's a demonstration that we build up in [Intermediate Perl](https://www.intermediateperl.com/) (and as a gist where I might expand this). The problem is that the arrow operator is under documented in perlop.

#!/usr/bin/perl

{
package Parent;
sub bar { print "Parent: $_[0]" . "\n" }
}

{
package Foo;
use parent qw(Parent);
sub bar { print $_[0] . "\n" }
}


print "# the invocant\n";
my $string = 'Foo';
$string->bar;

my $blessed = bless {}, $string;
$blessed->bar;

print "\n# the method\n";
my $method = 'bar';
$string->$method;
$blessed->$method;

# in SUPER, the virtual namespace that looks in @ISA
# this looks in the current package, not the package of
# the object. For this to work in main::, main needs a
# parent class
print "\n# main super class\n";
our @ISA = qw(MainSuper);
{
package MainSuper;
sub bar { print "MainSuper: $_[0]" . "\n" }
}
$string->SUPER::bar();
$blessed->SUPER::bar();

# Now start in Foo instead, and get what you probably wanted.
# Check out the SUPER module on CPAN that changes this oddity.
print "\n# Foo super class\n";
{
package Foo;
$string->SUPER::bar();
$blessed->SUPER::bar();
}

# Now, specify any sub you like:
print "\n# arbitrary sub\n";
sub quux { print "quux: @_" . "\n" }
$string->main::quux( 3, 7, 1 ); # really quux( $string, 3, 7, 1 )
$blessed->main::quux( 7, 3, 1 ); # really quux( $blessed, 7, 3, 1 )

# compare this with can, which returns a code ref is it can resolve
# that method, but where you have to supply the first argument yourself
print "\n# can\n";
my $coderef = $blessed->can('bar');
$coderef->($blessed);
$coderef->($string);

# but CORE is another package, and its does the same thing you just
# saw in main. This is a v5.16 thing, but it's using the
{
print "\n# CORE::substr\n";
if( $] print $string->CORE::substr( 0, 2 ), "\n"; # really substr( $string, 0, 2 )
print $blessed->CORE::substr( 3, 4 ), "\n"; # really substr( $blessed, 3, 4 )
}

You can run this under v5.8.9 and it works fine (skipping the CORE bits). It probably runs fine on earlier Perls too but I don't have those lying around.

$ perl5.8.9 test.pl
# the invocant
Foo
Foo=HASH(0x143008b70)

# the method
Foo
Foo=HASH(0x143008b70)

# main super class
MainSuper: Foo
MainSuper: Foo=HASH(0x143008b70)

# Foo super class
Parent: Foo
Parent: Foo=HASH(0x143008b70)

# arbitrary sub
quux: Foo 3 7 1
quux: Foo=HASH(0x143008b70) 7 3 1

# can
Foo=HASH(0x143008b70)
Foo

# CORE::substr
v5.16 needed for core, this is 5.008009

As for the pipe operator part of this post, the arrow is not going to do that. A proper pipe operator would feed all of the values from the left hand side to the right hand argument list. The arrow isn't going to do that.

Leave a comment

About karjala

user-pic I'm a Perl developer.