in reply to Creating data tree from input without eval
What makes you think that your code, written in Perl, is faster than the Perl code for eval, written in C?
I would really recommend you move to a saner way of storing your configuration information, like a slash-delimited path to the target or a dot delimited path, like this:
blah.hat=splat cat=doh,ray
Then, assuming your data wouldn't contain commas, you could conveniently create your configuration information by using one of the Config modules. Or you could write your own parser for that (untested, ad-hoc code, see Data::Dumper for a better diving implementation):
... my $Config = { some => 'default values', }; while (<CONFIG>) { chomp; my ($key, $value) = split /=/,2; my @path = split /\./, $key; my $loc = $Config; my $last = pop @path; for (@path) { $loc->{$_} ||= {}; $loc = $loc->{$_}; }; if ($value =~ /,/) { # Store an array $loc->{$last} = [ split /,/, $value ]; } else { $loc->{$last} = $value; }; };
Also see Data::Diver for far more convenient diving into data structures.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Creating data tree from input without eval
by merlyn (Sage) on Apr 05, 2008 at 16:54 UTC | |
by cosmicperl (Chaplain) on Apr 05, 2008 at 18:54 UTC | |
|
Re^2: Creating data tree from input without eval
by cosmicperl (Chaplain) on Apr 05, 2008 at 18:49 UTC | |
by ikegami (Patriarch) on Apr 06, 2008 at 06:36 UTC | |
by planetscape (Chancellor) on Apr 06, 2008 at 07:13 UTC | |
by ikegami (Patriarch) on Apr 06, 2008 at 09:20 UTC |