So you want something like this to work:

my %mapping = ( ARRAY => '@', HASH => '%', SCALAR => '$', GLOB => '*', REF => '$', CODE => '&' ); return eval $mapping{$type}.'{$_[0]}';
Note that your old code used eval "$mapping{$type}{$_[0]}" which ends up trying to run code like "$SCALAR(0xbadf00)", which doesn't do much good.

My code above expands out to:

return @{$_[0]} if $type eq "ARRAY"; return %{$_[0]} if $type eq "HASH"; return ${$_[0]} if $type eq "SCALAR"; return *{$_[0]} if $type eq "GLOB"; return ${$_[0]} if $type eq "REF"; return &{$_[0]} if $type eq "CODE";
but a more correct version of this useless code would be:
my( $ref )= @_; $ref= $$ref while UNIVERSAL::isa($ref,"REF"); return @$ref if UNIVERSAL::isa($ref,"ARRAY"); return %$ref if UNIVERSAL::isa($ref,"HASH"); return $$ref if UNIVERSAL::isa($ref,"SCALAR"); return &$ref() if UNIVERSAL::isa($ref,"CODE"); return $$ref,@$ref,%$ref if UNIVERSAL::isa($ref,"GLOB");
Now if you really want to try to use stringy eval for this, feel free. It seems like a waste to me. And, no, block eval would do you no good in this, unless you use it to make some fatal errors non-fatal (which is what block eval is for), such as using @$ref when $ref is a reference to something other than array.

        - tye (but my friends call me "Tye")

In reply to (tye)Re2: Eval and $@% ... by tye
in thread Eval and $@% ... by dragonchild

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.