in reply to mocking or trapping system calls

I know I can replace system via *CORE::GLOBAL::system

Actually, you can't. system isn't overridable

>perl -le"print defined(prototype('CORE::system'))?1:0" 0

If you could override readpipe, then you could override backticks, since they're the same thing, but you can't.

(Perl 5.8.8)

Replies are listed 'Best First'.
Re^2: mocking or trapping system calls
by wu-lee (Beadle) on May 09, 2008 at 18:16 UTC
    Hmm, if that's true why does this work for me on v5.8.8?

    $ perl -e 'BEGIN { *CORE::GLOBAL::system = sub { print "hello $_[0]\n" + } } system "foo"' hello foo

      I think overriding with *CORE::GLOBAL::system does actually work in most cases... The main reason that system doesn't have a prototype (which could be specified with Perl's prototype system), and thus is formally not overridable, is its "indirect object" syntax (without a comma after the first argument — see exec), which you can't (syntactically) handle with a Perl routine... As long as you don't need it, you should be fine. (But there would still be the problem with overriding backticks, as you're saying.)

      Update: Here's a sample command using this indirect object syntax

      system {'bash'} 'zsh', '-c', 'echo I think I am a $0'; # outputs "I think I am a zsh" (although it's a bash)

      which works fine as long as system isn't overridden. If you try to override it, you'd just get a compile-time error

      syntax error at ./685741.pl line 7, near "} 'zsh'" Execution of ./685741.pl aborted due to compilation errors.