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

In a tie constructor (TIESCALAR, TIEARRAY, TIEHASH, or TIEHANDLE) how can I access the value of the thing being tied? I really want to know how to do this for a TIEHANDLE, but I assume that the procedure would be about the same for any tie constructor. Similarly, can I set the tied variable during an untie?

Example code : I want to access the value of $t (or whatever the variable being tied is) in the TIESCALAR call. How can I get it? Or can I get it at all?

package TestTie; sub TIESCALAR{ my $pkg=shift; my $obj=[] ; bless $obj, $pkg; return $obj; } package main; my $t=9; tie $t , 'TestTie';
I've been through the perltie docs, the Tie chapter of Advanced Perl Programming and the source code for several of the Tie:: modules, but can't seem to find any pointers.

Replies are listed 'Best First'.
RE: Access to tied variable in the tie constructor???
by Anonymous Monk on May 27, 2000 at 14:50 UTC
    You can pass additional arguments to tie, like this:
    package TestTie;
    sub TIESCALAR{
        my ($pkg, $object) = @_;
        my $obj=[] ;
        bless $obj, $package;
        print "$$object\n";
        return $obj;
    }
    package main;
    my $t=9;
    tie $t, 'TestTie', \$t;
    
    
    hth, g
      I know that I can pass additional arguments to the tie, but I don't think I should have to. In my example $t is already an argument to the tie call and I don't want to have to pass it again. IMHO it looks redundant and un-clean to require the user of the tie to pass the tie variable twice and have the code look like:
      tie $t, 'TestTie', \$t;