ikegami has asked for the wisdom of the Perl Monks concerning the following question:
[This post delves into the internals of Perls. Most won't find it useful, but some might find it interesting.]
I've used the following to process the contents of an array while emptying it:
f( splice(@a) )
Someone asked what @a, @a=() did on StackOverflow. Well, that's the what the title of their question was, but the actual question was a bit different. But I thought it might be an alternative to splice(@a), so I put it to the test.
$ perl -e' use strict; use warnings; use feature qw( say ); my @a = qw( a b c d ); say for @a, @a=(); say "--"; say for @a; ' Use of freed value in iteration at -e line 7.
That was a first for me! Anyone want to venture a guess as to why it does that?
Note that the problem is not specific to loops, contrary to what your research might initially lead you to believe.
$ perl -e' use strict; use warnings; use feature qw( say ); my @a = qw( a b c d ); my @b = ( @a, @a=() ); say for @b; say "--"; say for @a; ' Use of uninitialized value $_ in say at -e line 8. Use of uninitialized value $_ in say at -e line 8. Use of uninitialized value $_ in say at -e line 8. Use of uninitialized value $_ in say at -e line 8. --
Hint: The following avoids the problem:
$ perl -e' use strict; use warnings; use feature qw( say ); my @a = qw( a b c d ); my $r = sub{ \@_ }->(@a); say for @a, @a=(); say "--"; say for @a; ' a b c d --
I'll post the answer.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Use of freed value
by ikegami (Patriarch) on Jun 26, 2019 at 16:48 UTC | |
|
Re: Use of freed value
by shmem (Chancellor) on Jun 26, 2019 at 17:35 UTC | |
|
Re: Use of freed value
by LanX (Saint) on Jun 26, 2019 at 16:51 UTC | |
by ikegami (Patriarch) on Jun 26, 2019 at 17:05 UTC | |
by LanX (Saint) on Jun 26, 2019 at 17:25 UTC | |
by ikegami (Patriarch) on Jun 27, 2019 at 11:48 UTC | |
by LanX (Saint) on Jun 27, 2019 at 22:43 UTC | |
|