Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Code comes first:
use Data::Dumper; use strict; no warnings; no autovivification; my $return_from_sub = '{X}{Y}' ; # a result return from some sub my $to_process = '$x->' . $return_from_sub; # my local def my $x = { Y => { Z => 10, } }; # after some regex work on the $to_process my $lastPart = 'Y' ; # the last part from $to_process my $base = '$x->{X}' ; # before the $lastPart my $lastPartIs = "HASH" ; my $newData = [qw/a b c/]; my $ref = eval $base || eval "$base = {}"; # whatever it is, just make + it referencable if not defined. if ( $lastPartIs eq "HASH" ) { $ref->{"$lastPart"} = $newData; # Remark 1 } elsif ( $lastPartIs eq "ARRAY" ) { $ref->["$lastPart"] = $newData; # Remark 1 } print Dumper $x;

Above code demonstrated some sort what I want (but not entirely correct), however I have to make it one level up from the last "part".

I know that I cannot directly get that reference via eval. like :

$ref = eval '$base$return_from_sub'; $ref = [/whatever/];

This just make $ref pointed to something else so this won't work. But I wonder is there any trick can do something like :

$ref = eval '\$refString'; deference( $ref ) = $whatever ;# [] {} ""

Provided that, I don't know what will be returned from the sub, and I don't know what's gonna be stored. both (return_from_sub and to write) can be array ref, hash ref, code ref, whatever ref or simple scalar.


In reply to How to get a reference from an eval() string? by exilepanda

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2020-03-30 01:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      To "Disagree to disagree" means to:









      Results (172 votes). Check out past polls.

      Notices?