I have a hash that is passed by reference to a function.

In that function, the hash gets tied and modified. After the function returns, the original hash now has new values. It goes a little something like this:

my %hash; my $blah = function(\%hash); warn Dumper \%hash; # prints a bunch of values
This is what the function looks like... (note that I simplified the function and everything else for demo purposes)
sub function { my ($hash) = @_; tie %$hash, 'Apache::Session::File', undef, { Directory => $r->dir_config('blah'), LockDirectory => $r->dir_config('blah'), }; $hash->{auth}->{user} = "jacques" }
Okay, everything is working as it is suppose to be. But when I put that tie() into a separate module that returns the tied hash, then the original hash is not modified. This is a problem. Here's what the function looks like when using that module.
sub function { my ($hash) = @_; $hash = MyModule::tie_session(); $hash->{auth}->{user} = "jacques"; }
When putting the tie() in a module, the %hash will have no values when the function returns. It never gets modified outside of the function. How can I use the modular approach and have the original hash modified?

Here's what the module looks like:

Package MyModule; use Apache::Session::File; sub tie_session { my $hash = {}; eval { tie %$hash, 'Apache::Session::File',undef, {Directory => $r->dir_config('Blah'), LockDirectory => $r->dir_config('Blah'), }; }; return $@ if ($@); return $hash; } 1;

In reply to References, hashes, and tie() -- oh my by jacques

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.