in reply to Re: mocking or trapping system calls
in thread mocking or trapping system calls

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

Replies are listed 'Best First'.
Re^3: mocking or trapping system calls
by almut (Canon) on May 09, 2008 at 18:52 UTC

    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.