in reply to Reference to functions in CORE:: pseudo package?

There is a highly magical way to override some core keywords. Here, I'll override glob to never find anything,

$ ls AGM AGM.pm foo $ perl -e'print prototype("CORE::glob"), $/' $ perl -e'{ local *CORE::GLOBAL::glob = sub {()}; while (<*>) {print " +Oh, No!\n"} } while (<*>){ print "Ha!\n"}' Ha! Ha! Ha! $
I tried printing the prototype of the builtin function to see if a prototype was needed to override.

Unfortunately, print cannot be overridden. I told that tale in !Overriding Builtin print.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Reference to functions in CORE:: pseudo package?
by ysth (Canon) on Feb 02, 2004 at 02:41 UTC
    AFAIK, it only works to override keywords at compile time (i.e. in BEGIN or with use). That kinda ruins it for scoped override like you have. (Though you may be able to use a named sub and temporarily replace the sub with local).

    Update: Zaxo's code does work, though I don't understand why. Perhaps something special about glob? Trying to override a different function works the way I recall:

    $ # override in BEGIN block works $ perl -e'BEGIN { *CORE::GLOBAL::sin = sub { "a" } } print sin(0)' a $ # override at runtime doesn't have any effect $ perl -e'*CORE::GLOBAL::sin = sub { "a" }; print sin(0)' 0 $ # using local, still no effect $ perl -e'{ local *CORE::GLOBAL::sin = sub { "a" }; print sin(0) } pri +nt sin(0) ' 00