in reply to open file error

(edited to fix unclear statement)

Update 2: somehow, I never noticed splitdir

As jellisii2 said, File::Spec can be helpful.

In this case, File::Spec::splitpath would still have given "logfile.txt " because it can't know the extra space is not part of the file name.

Otherwise, File::Spec::splitpath is a good way to break down paths.

The potential negative to File::Spec::splitpath is that it only splits a path into volume, directory-path and filename. If you need to further split the directory path, you have to use a loop:

# this is NOT tested my ($vol, $dir, $file) = File::Spec::splitpath($path); my $temp_path = $path; my $temp_dir; my $temp_vol; my @dirs; while ($dir neq $temp_path) { $temp_path = $dir; ($temp_vol, $dir, $temp_dir) = File::Spec::splitpath($temp_path); unshift @dirs, $temp_dir; } unshift(@dirs, $dir) unless (@dirs > 0);

Replies are listed 'Best First'.
Re^2: open file error
by Anonymous Monk on Jul 19, 2016 at 22:16 UTC
    It's File::Spec->splitpath, and you're forgetting File::Spec->splitdir.