How can I get the directory two levels up in a path in Perl?

Viewed 148

How to go two directories up from an environment variable in perl?

sub do_file_tracing {
  my var = "$ENV{IMPORTANT_DIR}";

}

I want to create a file at long/path/to/the/important_dir long/path/to/the/important_dir/../../ , that is at long/path/to/

2 Answers

Use Path::Tiny. For example:

use Path::Tiny;
my $dir = path($ENV{IMPORTANT_DIR})->parent(2);
my $dir_qfn = "$ENV{IMPORTANT_DIR}/../..";
Related