K_TGTK
K_TGTK

Reputation: 815

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

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/

Upvotes: 2

Views: 304

Answers (2)

ikegami
ikegami

Reputation: 385917

my $dir_qfn = "$ENV{IMPORTANT_DIR}/../..";

Upvotes: 3

Håkon Hægland
Håkon Hægland

Reputation: 40758

Use Path::Tiny. For example:

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

Upvotes: 4

Related Questions