djcp3 has asked for the wisdom of the Perl Monks concerning the following question:

My first question is this, is there a way to get a reference to the tied variable inside a TIEHASH call or other TIE* call. For instance, if I did tie @array, $class; is there a way inside TIEHASH to get a reference to @array?
  • Comment on Access reference of a tied hash within TIEHASH

Replies are listed 'Best First'.
Re: Access reference of a tied hash within TIEHASH
by BrowserUk (Patriarch) on Mar 18, 2013 at 19:02 UTC

    Look at the documentation for tie and note the return value from that call.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Access reference of a tied hash within TIEHASH
by tobyink (Canon) on Mar 18, 2013 at 22:13 UTC

    No, not unless you pass it in manually.

    use v5.14; package MyHash { use Data::Dumper; use Tie::Hash (); use base "Tie::StdHash"; sub TIEHASH { my $class = shift; print Dumper @_; $class->SUPER::TIEHASH(@_); } } my %hash; tie %hash, MyHash => \%hash;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Access reference of a tied hash within TIEHASH
by McA (Priest) on Mar 18, 2013 at 19:00 UTC

    Hi,

    I don't really understand your question.

    With tie you make the tied object behave like a perl variable of choice. That means you are forced to implement the behaviour you want to achieve.

    Can you explain what you want to do?

    McA

      I need to access a reference to the hash being tied inside the TIEHASH function. For instance: tie %hash, $class; sub TIEHASH { my ($title)=@_; # need to access a ref %hash in some way from right here. }