sub string_to_struct { my $string = shift; my @refs = split /__/, $string; $string = "\$$refs[0]"; for (1 .. $#refs) { $string .= "-> {'$refs[$_]'}" }; return eval "\\$string"; # a ref, for reasons to do with not failing if eval "\$string" would be an array, hash, hashref etc. }