use strict; use warnings; use Data::Dumper (); my $S = {}; sub splitTo { my ($pH, $pA, $leaf) = @_; $pA = [ grep $_, split /\//, $pA ] unless ref $pA; return $leaf unless @$pA; my $nxt = shift @$pA; my $pHN = ref $pH->{$nxt} ? $pH->{$nxt} : {}; $pH->{$nxt} = splitTo($pHN, $pA, $leaf); $pH; } splitTo($S, 'foo/bar', 'oops'); splitTo($S, 'foo/bar/baz', '\\o/'); splitTo($S, 'foo/gnu', 'gnat'); print Data::Dumper->Dump([$S], ['S']);