http://www.perlmonks.org?node_id=889655


in reply to Alternative for a directory builder

If the order doesn't matter, you could sucessively chop off stuff from the end.  This prints the same as your code, but reversed:

my $_=$a; say while s|[^/]+/*$||; __END__ /aaaa/bbbb/cccc/dddd/ /aaaa/bbbb/cccc/ /aaaa/bbbb/ /aaaa/ /

But if order does matter (e.g. for mkdir purposes), you can of course also say

my @dirs; my $_=$a; push @dirs, $_ while s|[^/]+/*$||; say for reverse @dirs; __END__ / /aaaa/ /aaaa/bbbb/ /aaaa/bbbb/cccc/ /aaaa/bbbb/cccc/dddd/