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

hey I'm writing a perl script which writes it's own database, however, the script needs to be able to operate correctly even with the database missing. I dont want to deal with error messages when an open command fails, So my goal is to check for the database before I open it. The obvious choice is to use grep

From what I know of using system commands, I have to have it set up a such:

$grep=`grep file`;

I need to get a return value from grep, however. I'm gonna assume that the above used $grep scalar will store some type of return value from grep signifying whether or not the file was there, Could someone back this up and maybe tell me what return values i'll get for which options?

thanx

-Me

20041202 Edit by ysth; change title from: grep

Replies are listed 'Best First'.
Re: Check whether a file exists
by NetWallah (Canon) on Dec 02, 2004 at 22:53 UTC
    *Shudder*

    Please RTFM on Unix grep.

    grep pattern file

    This searches the file for a pattern.

    To check for the presence of a file, use perl's "-e" function. Read "perldoc perlfunc".

        ...each is assigned his own private delusion but he cannot see the baggage on his own back.

Re: Check whether a file exists
by ercparker (Hermit) on Dec 03, 2004 at 06:24 UTC
    I would suggest what NetWallah suggested previously
    if (-e 'database.db') { #database exists }
Re: Check whether a file exists
by Happy-the-monk (Canon) on Dec 02, 2004 at 22:49 UTC

    You already defined your test cases in prose.

    All you need to do now is to create the situations on the target machine in a safe environment and see what your code does.

    print ">$grep<\n"; # see what $grep contains

    Cheers, Sören