develooper Front page | perl.perl5.porters | Postings from September 2001

RE: error dereferencing array in a tied hash

Thread Previous | Thread Next
Steve Sapovits
September 29, 2001 12:05
RE: error dereferencing array in a tied hash
Message ID:

> Given that this code works:
>     use Tie::Hash;
>     my $h = {};
>     tie %$h, "Tie::StdHash" or die;
>     $h->{KEY} = [];
>     push(@{$h->{KEY}}, (qw/foo bar/));
>     print "@{$h->{KEY}}\n";
> and not being able to see your code, I can only assume the problem is
> in your implementation.  Sorry. :(

I agree this is a lame post without code.  But I can't post the whole stack.
It wouldn't make sense without many files.

I resorted to the no-code post for 2 reasons:

1)  I got 2-3 responses from other lists from people saying they'd seen this
    with ties and never had it explained.

2)  The fact that right before return FETCH has the correct value, and the
    fact that this DOES work:  push(@{my $x = $h->{KEY}}, qw/foo bar/)

    Here are relevant snippets of code.  You can see that between the time
    the value is checked and the time it's dereferenced on the next line it
    becomes undefined.  Then it's defined on the next line.  Seems that this
    would have to be a Perl bug:

    The accessor code:

      $x->{ARRAY} = [];
      warn "About to call push, \$x->{ARRAY}=$x->{ARRAY}";
      eval {push(@{$x->{ARRAY}}, qw/foo bar fubar/)} or warn @_;
      warn "after push, \$x->{ARRAY}=$x->{ARRAY}";

    The tie code:

      sub FETCH
          my $self = shift;
          my $key  = shift;
          my $value = $self->{USER_OPT}->direct_get($key, $self->{ATTRS});

          warn "FETCH returning $value";
          return $value;

      sub STORE
          my $self  = shift;
          my $key   = shift;
          my $value = shift;
          my $name;

          warn "STORE putting $value in hash";
          ($name, $value) =
            $self->{USER_OPT}->direct_set($key, $value, $self->{ATTRS});

          return $value;

    The output.  Note that it's okay (line 32), but Perl fails on the next
line (33),
    and a check afterwards (line 34) shows it to be correct.  So it's the
    context that's making it appear to be undefined.

      STORE putting ARRAY(0x15f9d8) in hash at line 53.
      FETCH returning ARRAY(0x15f9d8) at line 42.
      About to call push, $x->{ARRAY}=ARRAY(0x15f9d8) at hashobj line 32.
      FETCH returning ARRAY(0x15f9d8) at line 42.
      Can't use an undefined value as an ARRAY reference at hashobj line 33.
      FETCH returning ARRAY(0x15f9d8) at line 42.
      after push, $x->{ARRAY}=ARRAY(0x15f9d8) at hashobj line 34.

Steve Sapovits

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About