Following this example,
I tried to override print
with my own:
BEGIN {*CORE::GLOBAL::print = sub {print 1};}
print 2;
But it turns out that it doesn't work,2
is still printed instead of 1
.
Why?
Because it has very special parsing rules that cannot be replicated by a normal function, the print
operator cannot be overridden.
print "foo\n";
print { *STDOUT } "foo\n";
You can find out which operators can be overriden using prototype
>perl -E"say qq{$_: }, defined(prototype(qq{CORE::$_})) ? 'yes' : 'no' for @ARGV" print map time chr
print: no
map: no
time: yes
chr: yes
PS — You'd have an infinite loop if your code had actually overridden print
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With