G'day nikmit,

I probably would have written that more like this:

#!/usr/bin/env perl -l use strict; use warnings; use Data::Dump; for (1..3) { my $ref1 = gimme(); $ref1->[0]{box}{y}{z} = $_; $ref1->[0]{box}{w} = 'x' if $_ == 2; $ref1->[0]{box}{a}{d} = 4 if $_ == 3; trace($ref1); my $ref2 = gimme(); trace($ref2); } sub gimme { [ { box => { a => { b => 2, c => 3 }, y => {} } } ] } sub trace { my ($ref) = @_; print 'Tracing: '; print join ' -> ', $ref, $ref->[0], $ref->[0]{box}, $ref->[0]{box}{y}; dd $ref; return; }

Notes:

Here's the output from a sample run:

Tracing: ARRAY(0x7fdc5307b7c8) -> HASH(0x7fdc5282e280) -> HASH(0x7fdc5282de48) +-> HASH(0x7fdc5282dd88) [{ box => { a => { b => 2, c => 3 }, y => { z => 1 } } }] Tracing: ARRAY(0x7fdc530b1f18) -> HASH(0x7fdc530b4bc0) -> HASH(0x7fdc530251a0) +-> HASH(0x7fdc53025170) [{ box => { a => { b => 2, c => 3 }, y => {} } }] Tracing: ARRAY(0x7fdc530b4bc0) -> HASH(0x7fdc52846220) -> HASH(0x7fdc5282def0) +-> HASH(0x7fdc5282dd70) [ { box => { a => { b => 2, c => 3 }, w => "x", y => { z => 2 } } }, ] Tracing: ARRAY(0x7fdc530b8a48) -> HASH(0x7fdc53025188) -> HASH(0x7fdc530b1ee8) +-> HASH(0x7fdc530b7968) [{ box => { a => { b => 2, c => 3 }, y => {} } }] Tracing: ARRAY(0x7fdc530b8a48) -> HASH(0x7fdc5282dda0) -> HASH(0x7fdc53094b50) +-> HASH(0x7fdc5282de48) [ { box => { a => { b => 2, c => 3, d => 4 }, y => { z => 3 } } }, ] Tracing: ARRAY(0x7fdc530b1f48) -> HASH(0x7fdc530b7a88) -> HASH(0x7fdc530aabb0) +-> HASH(0x7fdc530b1e58) [{ box => { a => { b => 2, c => 3 }, y => {} } }]

— Ken


In reply to Re: Readonly references, replicating data structures by kcott
in thread Readonly references, replicating data structures by nikmit

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.