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

Lets say I have a tied variable, like this:

use Tie::HashTree qw(hash_to_tree); my $tree = tie my %x, 'Tie::HashTree';

and the constructor looks like this:

sub TIEHASH { my ($class, $self) = @_; $self->{level} = ""; $self->{name} = "top"; $self->{top} = {}; bless ($self, $class); }

Instead of "top", I'd like the name to be the name of the tied variable. Is there any way to do this without forcing the user to input another argument?

Thanks, jryan

Replies are listed 'Best First'.
Re: Is there any way to find the name of a tied variable?
by blakem (Monsignor) on Sep 11, 2001 at 04:40 UTC
    My guess is that it isn't possible (or a really good idea) for the simple reason that Values can be called by more than one Name. There is *not* a 1 to 1 relationship between them. At least in the untied world, it would be quite cumbersome to make sure Values always new exactly what Names were refering to them. Consider the following code:
    #!/usr/bin/perl -wT use strict; my $x = {name => 'x', value => 123}; my $y = $x; $y->{name} = 'y'; $y->{value} = 321; print "X: $x->{name} => $x->{value}\n"; print "Y: $y->{name} => $y->{value}\n"; =output X: y => 321 Y: y => 321
    Here we have the *names* 'x' and 'y' pointing to the same value.. It doesn't make sense in this instance for the value to know what its name is, because it has more than one (could have used an anon hash, in which case it would have no Name at all).

    In general I think its a bad idea to attempt to replicate the name of the variable in the contents of the variable. However, I haven't done much Tie:: development (used them a bunch, but haven't spent much time under the hood) so take my advice in the more general sense.

    -Blake

Re: Is there any way to find the name of a tied variable?
by suaveant (Parson) on Sep 11, 2001 at 05:41 UTC
    One problem is you aren't even tying $tree to begin with, you are tying %x, and then assigning the object ref to $tree. But I do not think there is any way to do this... nor should you really want to... root is very dscriptive or the top node of a tree (or bottom node bdepending on how you look at it)

    You could probably do something along the lines of the followi ng...

    use Tie::HashTree qw(hash_to_tree); #my $tree = tie my %x, 'Tie::HashTree'; Tie::HashTree->new('tree'); sub new { my $pkg = shift; my($ppackage) = (caller)[0]; ${$ppackage.'::'.$_[0]} = tie %{$ppackage.'::'.$_[0]}, $pkg; } sub TIEHASH { my ($class, $self) = @_; $self->{level} = ""; $self->{name} = "top"; $self->{top} = {}; bless ($self, $class); }
    Now, you will probably have to turn off strict and warnings inside the new()... but it will work (I sort of tested it) but it is probably a bad idea, and you should probably just either keep the same name for all, or have them pass it in. However, don't ever say you weren't enlightened to the dark corners of perl ;-) TIMTOWTDI

    Update BTW - the new func ties and saves to $tree and %tree in the package it was called from

                    - Ant
                    - Some of my best work - Fish Dinner