I have a hash array that I want to use to hold useful information for debugging purposes for a large Perl program.

The problem is that while I can add an entry to a hash in a subroutine, when the program flow returns to main(), the entry in the hash is no longer there, due to problems with the scope.

The code is as this:
our %debug = (); $debug{'level'} = 1; $debug{'stack'}[0] = "main()"; $rv = MyModule::testStack(\%debug); my @t = $debug{'stack'}; my $CountMain = scalar (@t); print "$CountMain \n"; # This prints 1 #Now there is another subroutine defined in another module: sub testStack { my $ptr = $_[0]; my %h = %$ptr; my @a = $h{'stack'}; push (@a, "testStack()"); my $count = scalar (@a); print "$count \n"; # This prints 2 return 1; }


I am passing the hash array "debug" to testStack() by reference. Yet, when I want to add an element to the array debug{'stack'} in testStack(), it is not added to the debug of main()'s scope.

How do I get around this problem? I would like to refer to $debug{'stack'} as an array so I can use push/pop functions.

In reply to How do I add entries to a hash array in a different scope? by SQADude

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.