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

Hi Monks , can you please explain me why output redirection doesn't work for the next perl code ( actually it is not only perl's problem but also shell)

so , if I run in my bash or zsh
echo test | perl -pe 's/st/AB/'; >/tmp/xx
it doesn't get redirected to /tmp/xx I tried to redirect STDERR like
echo test | perl -pe 's/st/AB/'; >/tmp/xx 2>&1
but it also doesn't work . I think it comes from the way perl prints the result , but I can't understand it .

Replies are listed 'Best First'.
Re: output redirection when calling perl from shell - perl -pe "blabla"
by jethro (Monsignor) on Oct 15, 2010 at 11:28 UTC
    Remove ;. For the shell that character separates complete statements. So you basically redirect an empty statement

    If you wanted to have the ; as part of the perl script you should put it inside the ' '

      it is working now , thank you .
Re: output redirection when calling perl from shell - perl -pe "blabla"
by kcott (Archbishop) on Oct 16, 2010 at 04:24 UTC

    I suspect you made the (not unreasonable) assumption that the script was producing no output because the output file /tmp/xx was empty.

    Due to the way that output redirection is handled by the shell, the output file is created regardless of whether there is any actual output.

    You can use this behaviour to your advantage to quickly create an empty file. Here's an example:

    $ ls -l redirect_out ls: cannot access redirect_out: No such file or directory $ > redirect_out $ ls -l redirect_out -rw-r--r-- 1 ken None 0 Oct 16 14:50 redirect_out

    -- Ken