I used the same idea, but File::Spec to parse the filename/directory portions when I was testing the following, which may help if you need portability later:
use strict;
use warnings;
use File::Spec;
my $p = q{/var/www/vhosts/testing.com/httpdocs/test1.html};
my ( $v, $d, $f ) = File::Spec->splitpath( $p );
my @dp = File::Spec->splitdir( $d );
foreach my $i ( 0 .. $#dp ) {
next unless ( $i and length $dp[$i] );
print File::Spec->catdir( @dp[0 .. $i ] ), qq{\n};
}
For my sample data, the code gave me the following output:
Hope that helps.
Update 2011-02-22
I was able to come up with a map solution that seemed to work, although I believe others may have better solutions:
|