Now there is nothing inherently bad with this code. However, it hard-codes both the path separator and the .. parent directory alias. I therefore whipped out File::Spec , and come up with this monstrosity.use Cwd qw(abs_path); use File::Basename qw(dirname); my $dir_parent = abs_path(dirname(__FILE__) . '/../');
Now this code is nice in a way as it only relies on File::Spec. But, god! It's soo long!use File::Spec; my $dir_parent = do { my ($vol, $dir, $file) = File::Spec->splitpath( File::Spec->rel2abs(__FILE__) ); # Go up 1 directory my @dirs = File::Spec->splitdir($dir); pop @dirs while (@dirs > 1 && $dirs[-1] eq ''); # Clean up pop @dirs if @dirs > 1; # Go up 1 real directory. $dir = File::Spec->catdir(@dirs); File::Spec->catpath($vol, $dir); };
Now, I've since adapted my code to no longer need this information. However, the above two questions still remain. How would y'all determine the parent directory of a script or module in a platform independent way?use File::Basename qw(dirname); use File::Spec; my $dir_parent = File::Spec->rel2abs( File::Spec->catdir( dirname(__FILE__), '..' ) );
In reply to Platform Independent Directory Traversal by wind
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |