borisz has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I like to overwrite system and chdir for debuging purpose. But I can't get it to work.
I tried so far:
*CORE::GLOBAL::system = sub { print "system\n" }; *CORE::GLOBAL::chdir = sub { print "chdir\n" }; *CORE::GLOBAL::glob = sub { print "glob\n" }; glob; chdir; system __OUTPUT__ glob
I wonder why this works for glob, but not for chdir, system, chr and ord. Whats wrong with my code?
Boris

Replies are listed 'Best First'.
Re: overwrite builtin functions ( system, chdir )
by ysth (Canon) on Apr 04, 2005 at 13:46 UTC
    Perl needs to know that you want to override a builtin at the time it compiles the code that calls the builtin. Put your *CORE::GLOBAL:: assignments in a BEGIN block.
      Thanks, that work fine!
      Boris
Re: overwrite builtin functions ( system, chdir )
by ktross (Deacon) on Apr 04, 2005 at 13:16 UTC
    Would something like this do?
    use subs 'system','chdir','glob'; glob; chdir; system; sub system {print "system\n"} sub chdir { print "chdir\n" } sub glob { print "glob\n" }

    __OUTPUT__

    glob
    chdir
    system
    
      Hi,
      No, that is not enough, I like to overwrite the functions global regardless of the namespace.
      So that this work:
      *CORE::GLOBAL::system = sub { print "system\n" }; *CORE::GLOBAL::chdir = sub { print "chdir\n" }; *CORE::GLOBAL::glob = sub { print "glob\n" }; package xyz; glob; chdir; system
      Boris