#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Benchmark 'timethese'; timethese( 100000, { 'manual' => sub { my @arr = qw(aa bb cc dd ee ff); my %hash; my $ref = \%hash; while (defined (my $element = shift @arr)) { $ref = ($ref->{$element} = @arr ? {} : 0); } \%hash; }, 'eval_string' => sub { my $hoh; my @array = qw(aa bb cc dd ee ff); my %hash; my $eval_string = '$hoh->{' . (join '}{', @array) . '} = 0'; eval "$eval_string"; $hoh }, } );