my $str = "/One/Two/Three"; my @paths = ($str); while ($paths[-1] =~ tr#/#/# > 1) { push @paths, substr($paths[-1], 0, rindex($paths[-1], '/')); } print "$_\n" for @paths;