Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^11: Getting for() to accept a tied array in one statement [TANGENT: untie attempted while # references still exist]

by pryrt (Abbot)
on Apr 18, 2019 at 17:09 UTC ( [id://1232768]=note: print w/replies, xml ) Need Help??


in reply to Re^10: Getting for() to accept a tied array in one statement
in thread Getting for() to accept a tied array in one statement

I'm not good with tie, but thought I'd take this as a learning exercise. I was surprised by using our @data instead of a fresh anonymous aref for each instance, so I tied @y to the same class to confirm it (confirmed). I wanted to untie those variables and re-use them to tie to a second class that used separate data structures in TIEARRAY (in spoiler; the mods worked as I expected, with separate data structures for @x and @y)... but was surprised at the "untie attempted while 2 inner references still exist" warning when I did the untie. I thought the $x reference was probably the culprit, so undefined that.... but there was still one remaining inner reference. What is the second inner reference, and where did it come from?

use strict; use warnings; package MyClass; { use Tie::Array; our @ISA = ('Tie::Array'); our @data; #mandatory methods sub TIEARRAY { my $class = shift; bless \@data, $class; @data = @_ +; return \@data } sub FETCH { my ($self, $index ) = @_; print "FETCH($index)\n"; ret +urn $data[$index] } sub STORE { my ($self, $index, $value) = @_; print "STORE($index)\ +n"; $data[$index] = $value } sub FETCHSIZE { print "<FETCHSIZE> "; return scalar @data } }; package main; $|++; local $" = ", "; my @x; tie @x, "MyClass", 0, 0, 0; my $x = \$x[2]; $$x++; print "x = (@x)\n"; =begin comment When I first saw the above, it looked like no matter how many items we +re tied, they would all refer to the same @MyClass::data internal arr +ay. The next few lines showed that's true: when I tied @y to the same clas +s, @x lost its data; and when an element of @y was changed, the same +happened to @x. =cut my @y; tie @y, "MyClass", 0, 0, 0; print "y = (@y)\n"; print "x = (@x)\n"; $y[1] = 3.14; print "y = (@y)\n"; print "x = (@x)\n"; untie @y; # gives a warning: untie attempted while 2 inner refer +ences still exist undef $x; # uncomment to reduce next warning to 1 instead of 2; +comment to keep next warning at 2 untie @x; # warning, but with only 1 warning if previous line un +commented print "y = (@y)\n"; print "x = (@x)\n"; # so what's the second inner reference?
  • Comment on Re^11: Getting for() to accept a tied array in one statement [TANGENT: untie attempted while # references still exist]
  • Select or Download Code

Replies are listed 'Best First'.
Re^12: Getting for() to accept a tied array in one statement [TANGENT: untie attempted while # references still exist]
by hdb (Monsignor) on Apr 18, 2019 at 18:03 UTC

    Thanks for pointing to this! So it turns out that I am not an expert on classes either...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1232768]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found