The statements
    my %index2;
    eval{ %index2 = ( $index ) };
are equivalent to the statement
    my %index2 = ( $index );
with the only difference being the time at which they are evaluated: the eval statement is evaluated at run time; the other statement is evaluated at compile time | the eval statement is executed such that run-time errors are captured; both statements must be syntactically correct at compile time. (Thanks to Anonymonk for pointing out below the original confusion of this statement. As always, see The Docs for full details!)

But consider
    my %index2 = ( $index );
This statement attempts to initialize a hash with the single string  $index used as a key (and with no associated value) and not with a key/value pair. That's not going to work. Instead, consider

c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my $index = q{1 => 'foo', 2 => 'bar', 3 => 'baz'}; my %hash = ( $index => 'foobar' ); print Dumper \%hash; " $VAR1 = { '1 => \'foo\', 2 => \'bar\', 3 => \'baz\'' => 'foobar' };
Here, it's possible to see that the entire string of  $index becomes the key for the value 'foobar'.

Doing a full string eval would have given you the result you expected:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my $index = q{1 => 'foo', 2 => 'bar', 3 => 'baz'}; my %hash; eval qq{ %hash = ($index); }; print Dumper \%hash; " $VAR1 = { '1' => 'foo', '3' => 'baz', '2' => 'bar' };
or maybe just
c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my $index = q{1 => 'foo', 2 => 'bar', 3 => 'baz'}; my %hash = eval qq{ ($index) }; print Dumper \%hash; " $VAR1 = { '1' => 'foo', '3' => 'baz', '2' => 'bar' };
Update: Or indeed just  my %hash = eval $index; as LanX points out below!

See eval for a discussion of the differences between the
    eval { statement; ...; };
and
    eval "statement; ...;";
forms (referred to respectively as  eval BLOCK and  eval EXPR in the doc).


Give a man a fish:  <%-(-(-(-<


In reply to Re: Using eval to create a hash by AnomalousMonk
in thread Using eval to create a hash by davidfilmer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.