in reply to Relative path in test file

$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': $!";

Replies are listed 'Best First'.
Re^2: Relative path in test file
by ikegami (Patriarch) on Jul 07, 2022 at 19:12 UTC

    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?

        Because $RealBin works in those circumstances, and $Bin doesn't. Worse, $Bin can cause the wrong code to be executed, and this could result if a privilege escalation.

        Those circumstances being the use of a link to the script.