So you want something like this to work:
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 %mapping = ( ARRAY => '@', HASH => '%', SCALAR => '$', GLOB => '*', REF => '$', CODE => '&' ); return eval $mapping{$type}.'{$_[0]}';
My code above expands out to:
but a more correct version of this useless code would be: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";
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")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");
In reply to (tye)Re2: Eval and $@% ...
by tye
in thread Eval and $@% ...
by dragonchild
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |