Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2021-10-17 13:35 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (71 votes). Check out past polls.