Ryan Gies wrote:

With the intention of optimization, I am looking for a way around using *eval* in the below snippet at line 19:

    my $value = eval $key;

The objective is to get from $key to $value, knowing that $key is a literal string.

Thank you for an insights!


#!/usr/bin/perl -w
use strict;
use Data::Dumper qw(Dumper);

my $hash = {
    'food' => {
        'fruit' => [
            {
                'name'  => 'apple',
                'color' => 'red',
            },

        ],
    },
};

my $key = '$$hash{\'food\'}{\'fruit\'}[0]';

my $value = eval $key;

print  Dumper( $value );

Oi Mr Rube Goldberg, just use the key :)


my $fruit = $hash->{'food'}{'fruit'}[0];
print "Name: $fruit->{'name'}\n", "Color: $fruit->{'color'}\n";

Or just use it in one super long deref...

HTH :)

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to