Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Relative path in test file

by Dirk80 (Pilgrim)
on Jul 07, 2022 at 14:15 UTC ( [id://11145332]=perlquestion: print w/replies, xml ) Need Help??

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

I have my test files in a directory "t", e.g. the file ..../t/mytest.t. In mytest.t I want to open a file by using a relative path. If I want that make test is successful, I have to use a relative path like this ./t/data/test.txt. But if would call e.g. perl mytest.t it would fail, because the relative path should be ./data/test.txt.

Is there an elegant way to open a file with a relative path independent if I call it from the "t" directory or its parent directory?

Thank you!

Replies are listed 'Best First'.
Re: Relative path in test file
by Corion (Patriarch) on Jul 07, 2022 at 14:18 UTC

    $0 contains the name of the program as it was invoked. In your test cases, $0 will be:

    perl t/mytest.t # t/mytest.t
    perl mytest.t # mytest.t

    So, if you want to open the file relative to your main program, use a path relative to $0. The FindBin module does that already, so you can just use that:

    use FindBin; my $datafile = "$FindBin::Bin/data/test.txt"; open my $fh, $datafile or die "Couldn't open '$datafile': $!";

      Whever it makes a difference, $FindBin::RealBin is better than $FindBin::Bin. So might as well always use it.

        Whever it makes a difference, $FindBin::RealBin is better than $FindBin::Bin.

        why?

Re: Relative path in test file
by haukex (Archbishop) on Jul 07, 2022 at 20:34 UTC

    From your problem description it's a bit unclear to me whether you mean that you simply want to locate a file relative to the location of your test script, or you mean that the filename string you work with in your script should be expressed as a relative path (e.g. absolute "/path/to/data/test.txt" vs. relative "../data/test.txt"). The former is more common and what Corion's reply is doing - though I would personally recommend using catfile from File::Spec instead of manually concatenating the paths for maximum portability. If you need the latter, which is sometimes useful e.g. when you have to test an API and make sure it handles relative filenames correctly, then you can take the previously mentioned approach and additionally apply abs2rel from File::Spec on the filename.

      Thank you all so much. This community here is so great.

      Corion's solution already worked great. But I extended it now by using $FindBin::RealBin and doing the concatenation by using File::Spec->catfile(...).

Re: Relative path in test file
by JayBee (Scribe) on Jul 09, 2022 at 05:35 UTC
    Consider these from CGI::Simple
    $q = CGI::Simple->new; $full_url = $q->url(); $full_url = $q->url(-full=>1); $relative_url = $q->url(-relative=>1); $absolute_url = $q->url(-absolute=>1); $url_with_path = $q->url(-path_info=>1); $url_with_path_and_query = $q->url(-path_info=>1,-query=>1); $netloc = $q->url(-base => 1);

      Sorry, but the question has nothing to do with CGI, and URL paths are not the same as file system paths.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11145332]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-20 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found