Checking if a file exists within @INC

Discussion in 'Perl Misc' started by Paul Burton, Sep 25, 2003.

  1. Paul Burton

    Paul Burton Guest

    I'm using the "do" function to read in a system configuration file
    (which is just another perl script). "do" will search for the file using
    the @INC path list.

    The name of the file which is read is supplied by the user, so there is
    the potential that the specified file doesn't exist.

    Although I can put a check around the "do" statement to generate an
    error and crash out, I'd really like to do this check at the top of the
    program, where I'm checking all the user inputs are correct.

    However, at this point I don't want to execute the "do". What I want is
    a function which will search through the @INC directories and return an
    error if the specified file is not found there, or the name of the first
    directory matched otherwise.

    Of course I could write my own to do this. But, I figure that all the
    functions which use @INC (do, use, require etc.) must have to do
    something like this already, so I thought there would be something
    available to me to let me do this.

    But, I can't find this. Can someone point me to something suitable, or
    will I have to write my own?

    TIA

    Paul.
     
    Paul Burton, Sep 25, 2003
    #1
    1. Advertisements

  2. Paul Burton

    Sisyphus Guest

    Something like:

    use warnings;
    eval{ require 'some_file.ext' };
    if($@) {
    if($@ =~ /Can't locate/i) { print "File not found" }
    }

    If, for example, the file gets found, but fails to return a true value,
    then $@ gets set to a relevant error message - so you need to know that
    $@ is, in fact, reporting that the file aint there. Afaik, that regex is
    sufficient to establish that fact.

    Cheers,
    Rob
     
    Sisyphus, Sep 26, 2003
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.