perlancar has asked for the wisdom of the Perl Monks concerning the following question:
Compare this two piece of one-liner code that try to achieve the same thing, i.e. overriding backtick a.k.a. readpipe():
% perl -e'package Foo; sub readpipe { die }; sub import { my $caller = caller(); *{"$caller\::readpipe"} = \&readpipe } package main; BEGIN { Foo->import }; print readpipe("ls")'
Died at -e line 1.
% perl -e'package Foo; sub readpipe { die }; package main; BEGIN { *{"main::readpipe"} = \&Foo::readpipe } print readpipe("ls")'
file1
...
Why does the first code work, while the second doesn't? In the second code I've also tried putting the whole Foo code inside BEGIN block, without avail.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Overriding built-in function works only via import()?
by haukex (Archbishop) on Feb 01, 2018 at 09:47 UTC | |
by perlancar (Hermit) on Feb 01, 2018 at 10:07 UTC | |
|
Re: Overriding built-in function works only via import()?
by Athanasius (Archbishop) on Feb 01, 2018 at 07:07 UTC | |
by perlancar (Hermit) on Feb 01, 2018 at 09:18 UTC | |
|
Re: Overriding built-in function works only via import()?
by haukex (Archbishop) on Feb 01, 2018 at 19:11 UTC | |
by LanX (Saint) on Feb 02, 2018 at 09:02 UTC | |
by haukex (Archbishop) on Feb 02, 2018 at 10:02 UTC | |
|
Re: Overriding built-in function works only via import()?
by ikegami (Patriarch) on Feb 01, 2018 at 17:37 UTC |