Why does the first code work, while the second doesn't?
The Camel (🐪, Chapter 11) to the rescue:
Overriding Built-in Functions ... it's the assignment of a code reference to a typeglob that triggers the override, as in *open = \&myopen. Furthermore, the assignment must occur in some other package; this makes accidental overriding through typeglob aliasing intentionally difficult.
And looking at git history, the corresponding language was in perlsub as early as perl-5.000. So the behavior appears to be quite intentional.
And BTW, here's another short way to do what you want that doesn't use subs:
BEGIN { package Foo; *main::readpipe = sub {"Foo"} } print `xyz`; __END__ Foo
In reply to Re: Overriding built-in function works only via import()?
by haukex
in thread Overriding built-in function works only via import()?
by perlancar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |