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

Here's a snippet of my code.
#!/usr/bin/perl $counter = `ls /tmp/myfile.txt`;
The output is
ls: /tmp/myfile.txt: No such file or directory

Is there a way to suppress the output message of $counter, so that if it does not find any file, it will not print anything?

Replies are listed 'Best First'.
Re: Suppression of Output Message
by tilly (Archbishop) on Sep 09, 2008 at 18:45 UTC
    Is there any reason that you aren't using a built in file test?
    $counter = -e "/tmp/myfile.txt";
    That is less code, doesn't launch a new process, and you won't run into trouble if you're passed a filename with things like spaces in it.

    To learn more about the available filetests in Perl, check out http://perldoc.perl.org/functions/-X.html.

      Interesting! I've used file tests before but not in this manner. Thanks! I will try this out.

      I'd ++ but gots no moar votes left :(

      That being said, I second this idea.

      I'm so adjective, I verb nouns!

      chomp; # nom nom nom

Re: Suppression of Output Message
by dreadpiratepeter (Priest) on Sep 09, 2008 at 18:36 UTC
    try:
    $counter = `ls /tmp/myfile.txt 2>/dev/null`;


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."