use strict; use warnings; use Data::Dumper; sub hashify { my $path = shift; my $h = {}; my $current = $h; for (split '/', $path) { next unless length $_; $current->{$_} = {}; $current = $current->{$_}; } return $h; } print Dumper hashify('/foo/bar/baz/blubb');