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

Hi,
I'm modifying a test tool that uses a config file. At the moment the path to the file is hard coded and I want to be able to store it a avariable in a settings.pm file that contains evenvironmental settings.
The problem that this settings file may not be there, in which case I'd like to set the variable locally.
Is there a way to check if a file exists on disk and to put a use statement in conditional statement,so something like:
if (<check that the file exists>) { use settings; } else { $::CONFIGPATH = "C:\\configdir\\config.pm"; }
Is this complete gibberish or can I write someeing similar? Thanks
Edited by Chady -- added code tags and some line breaks.

Replies are listed 'Best First'.
Re: use statement in a conditional?
by imp (Priest) on Aug 18, 2006 at 13:48 UTC
    You could use require instead, as it happens at runtime instead of compile time. The downside is that you need to know where the file is of course.
    if (<check that the file exists>) { require 'settings.pm'; settings->import(); } else { $::CONFIGPATH = "C:\\configdir\\config.pm"; }
    Or you could use it in an eval, and run the else if it fails.
    eval "use settings"; if ($@) { $::CONFIGPATH = "C:\\configdir\\config.pm"; }
Re: use statement in a conditional?
by davidrw (Prior) on Aug 18, 2006 at 13:49 UTC
    See require for more info, but basically:
    if( $someTest ){ my $class = 'Class::DBI'; eval "require $class"; die "Could not load '$class': $@" if $@; }
Re: use statement in a conditional?
by ikegami (Patriarch) on Aug 18, 2006 at 15:49 UTC
    Check out if (the module, not the flow control statement).
      It worked! Thanks for your help ^_^
        Uh just another quick question (in relation to the above) Is there a command to get the letter of the current drive? I've done it using getcwd and then matching the result against /.:/, but it seems like something for which there'd be a standard command. Thanks.
Re: use statement in a conditional?
by davorg (Chancellor) on Aug 18, 2006 at 13:49 UTC

    Wrap the "use" statement in a call to "eval" and then examine $@ for errors and take appropriate action.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg