No such thing as a small change | |
PerlMonks |
Readonly references, replicating data structuresby nikmit (Sexton) |
on Jan 22, 2017 at 11:37 UTC ( [id://1180111]=perlquestion: print w/replies, xml ) | Need Help?? |
nikmit has asked for the wisdom of the Perl Monks concerning the following question: When I initially hit this it seemed incomprehensible... then I figured out why it's happening but not yet how to fix it - so here I am humbly asking for your wisdom. I expected the below code to create a fresh copy of @arr every time gimme() is executed. Instead it creates a new reference for @arr but reuses all of the nested references. The result is that gimme() returns the last box with its content, rather than a fresh empty box which is what I want... I considered using Readonly or a configuration file on disk to ensure I get an empty box every time, but both seem clunky and slow. The actual data structure is nested to 5-6 levels and while it is not huge, it will be executed often. I suspect turning the data structure into an object may be the right path, but I'm yet to pick that side of perl up... What would be your advice?
Result of that code is:
Back to
Seekers of Perl Wisdom
|
|