in reply to Overriding built-in function works only via import()?

Hello perlancar,

With warnings enabled, the second code gives me:

17:01 >perl -we "package Foo; sub readpipe { die }; package main; BEGI +N { my $rp = qq[main::readpipe]; *{ $rp } = \&Foo::readpipe; } print +readpipe('ls')" Ambiguous call resolved as CORE::readpipe(), qualify as such or use & +at -e line 1. 1801_SoPW.pl 1802_SoPW.pl 1803_CUfP ...

I don’t understand why the compiler sees the call as ambiguous; but following the advice in the warning and prepending an ampersand to the readpipe function does give the desired result:

17:01 >perl -we "package Foo; sub readpipe { die }; package main; BEGI +N { my $rp = qq[main::readpipe]; *{ $rp } = \&Foo::readpipe; } print +&readpipe('ls')" Died at -e line 1. 17:04 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Overriding built-in function works only via import()?
by perlancar (Hermit) on Feb 01, 2018 at 09:18 UTC
    Having to use ampersand in the "target code" removes the attraction to do this in the first place :)