I have a hash of arrays that I'd like to use as stacks. Initially, the arrays are empty. Later in my code, I'm using one of these stacks at a time for some processing.

What I'd like to do is set a temporary reference equal to the result of the hash lookup and perform operations on that since this will eliminate unnecessary lookups and make my code a little less disgusting.

The problem I am having is that when I assign a reference to an undefined list, the modifications I make are not reflected. As soon as I add an element to the original list, everything works as expected. This would make sense if perl's hard references don't do anything special for undefined references.

Currently, I could do something really cheezy, like pushing and then popping once from each list at the start (which would create empty but "defined" arrays), but this doesn't seem particularly robust since this might change in the next perl revision.

So, is there a good way to create an array of size 0 that is also defined? Would using soft references fix this problem?

Please enlighten this humble novice, oh great perl monks.


In reply to creating defined references to arrays of size zero by spieper

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.