Search code examples
perlhttp-redirectstderrsystem-calls

How can I get rid of the STDERR in Perl


I'm using some system commands in Perl.

In the below case I was getting output as follows:

ls: import-log.*: No such file or directory

ls: error-log.*: No such file or directory

No specified files found for deletion

My code:

sub monthoryear() 
{

  @importlog = `ls -al import-log.*`;

  @errorlog = `ls -al error-log.*`;

}

I don't want to see the following in the output even if there are no files.

ls: import-log.*: No such file or directory &

ls: error-log.*: No such file or directory

Solution

  • You can add stderr redirection in your subshell commands:

    @importlog = `ls -al import-log.* 2>/dev/null`;
    @errorlog = `ls -al error-log.* 2>/dev/null`;