in reply to Proper way of passing around tie'd variable
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.
|
|---|