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

if i call system(test.pl)>/dev/null from perl it redirects all execution of test.pl to null device.
is ther any way to redirect function call execuiton. what i mean is if i call
&func(), is it possible to redirect its execution to /dev/null. i dont want to ue system thats why i am in need of it .

Replies are listed 'Best First'.
Re: redirect function to /dev/null
by ikegami (Patriarch) on Mar 03, 2006 at 05:58 UTC
    { # Temporarily undefine STDOUT. local *STDOUT; func(); }
    as in
    sub func { print("in func\n"); } print("pre func\n"); { # Temporarily undefine STDOUT. local *STDOUT; func(); } print("post func\n");

    If func checks the return value of print in the above, it will notice that print failed. If you really wanted to print to /dev/nul (or to another file) and avoid that (rare) problem, you'd use

    { # Temporarily redirect STDOUT. open(local *STDOUT, '>', '/dev/nul'); func(); }

    You can do the same with STDERR. You might also want to look at $SIG{__WARN__}.