Is there any ->DESTROY of the tied class being called?

It depends where the @ary is defined. In the solution suggested by hdb, there is a DESTROY:

use warnings; use strict; print "--- Begin loop ---\n"; for ( @{ tie my @ary, 'MyArray', qw/ x y z /; \@ary } ) { print "<$_>\n"; last if /y/; } print "--- End loop ---\n"; BEGIN { package MyArray; sub TIEARRAY { my $c = shift; bless { arr=>[@_] }, $c } # largely borrowed from Tie::StdArray sub FETCH { $_[0]{arr}[$_[1]] } sub STORE { $_[0]{arr}[$_[1]] = $_[2] } sub FETCHSIZE { scalar @{$_[0]{arr}} } sub STORESIZE { $#{$_[0]{arr}} = $_[1]-1 } sub EXTEND { $#{$_[0]{arr}} = $_[1]-1 } sub CLEAR { @{$_[0]{arr}} = () } sub POP { pop @{$_[0]{arr}} } sub SHIFT { shift @{$_[0]{arr}} } sub PUSH { my $o=shift; push @{$$o{arr}}, @_ } sub UNSHIFT { my $o=shift; unshift @{$$o{arr}}, @_ } sub EXISTS { exists $_[0]{arr}[$_[1]] } sub DELETE { delete $_[0]{arr}[$_[1]] } sub UNTIE { %{$_[0]}=(); return } sub DESTROY { %{$_[0]}=(); return } sub SPLICE { my $ob = shift; my $sz = $ob->FETCHSIZE; my $off = @_ ? shift : 0; $off += $sz if $off < 0; my $len = @_ ? shift : $sz-$off; return splice(@{$$ob{arr}}, $off, $len, @_); } # debug stuff: use Class::Method::Modifiers qw/around/; use Data::Dump qw/pp/; my @m = qw/ CLEAR DELETE DESTROY EXISTS EXTEND FETCH FETCHSIZE POP PUSH SHIFT SPLICE STORE STORESIZE TIEARRAY UNSHIFT UNTIE /; for my $m (@m) { around $m => sub { my $orig = shift; my $self = shift; my @args = @_; if (wantarray) { my @rv = $orig->($self, @_); print STDERR $m," ",pp(@args)," => ",pp(@rv),"\n"; return @rv; } # else my $rv = $orig->($self, @_); print STDERR $m," ",pp(@args)," => ",pp($rv),"\n"; return $rv; }; } } __END__ --- Begin loop --- TIEARRAY ("x", "y", "z") => bless({ arr => ["x", "y", "z"] }, "MyArray +") FETCHSIZE () => 3 FETCH 0 => "x" <x> FETCHSIZE () => 3 FETCH 1 => "y" <y> DESTROY () => undef --- End loop ---

In reply to Re^2: Getting for() to accept a tied array in one statement by haukex
in thread Getting for() to accept a tied array in one statement by perlancar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.