in reply to Overriding built-in function works only via import()?
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Overriding built-in function works only via import()?
by LanX (Saint) on Feb 02, 2018 at 09:02 UTC | |
by haukex (Archbishop) on Feb 02, 2018 at 10:02 UTC |