In this respect tying is no different from passing any other hash around. If you want a subroutine to modify the original you have to pass a reference. As it stands now you're just copying the contents of the hash around.

my %hash = qw(foo 1 bar 2); foo(%hash); bar(\%hash);

The foo() subroutine gets a list, ('foo', 1, 'bar', 2). It can copy this list into a hash, but changes to that hash have no effect on %hash. bar() gets a reference to the original hash; changes to the reference affect %hash in the outer code.

Similarly, if you want to maintain the tie when you pass your hash out of hash_init(), you need to return a reference. Otherwise you're just copying the contents of your hash into another, non-tied, hash. You could also pass a hash reference for it to tie, i.e. hash_init(\%hash)

See perldoc perlreftut, perldoc perlref, and Beginning Perl's Chapter on References.


In reply to Re: Proper way of passing around tie'd variable by Somni
in thread Proper way of passing around tie'd variable by graffitici

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.