Jerry Preston ha scritto:
I have this array and I am trying to figure out how to read $id  in it.  Any
ideas?

@history = (
  {
    program => 'racer',
    version => '0.45',
    input => {
      '/home/' => undef,
    },
    input_contents => '
      $name    = \'Jerry\';
      $id      = \'035\';
      1;
    ',
    perl => {
      location => '/home/',
      version => 5.00502
    }
  }
);

Thanks,

Jerry


Hi, the value associated with key "input_contents" seems to be perl code. Thus you can "eval()" it:


<code language="perl">
use strict;
use warnings;
use diagnostics;

# the data:
my @history = (
    {
        program => 'racer',
        version => '0.45',
        input => { '/home/' => undef, },
        input_contents => '$name    = \'Jerry\';$id      = \'035\';1;',
        perl => {
            location => '/home/',
            version => 5.00502
        }
    }
);

# the problem: read $id "value"

my $input_contents = $history[0]->{input_contents};

# predeclare variables in eval()ed code to satisfy use strict pragma
my $id;
my $name;

# see perldoc -f eval (eval EXPR)
eval $input_contents;

print "Code to be eval()ed:\n";
print "$dummy1\n\n";
print "After eval():\n";
print "\$name=$name\n";
print "\$id=$id\n";
</code>

--
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