http://qs321.pair.com?node_id=1214123


in reply to Re: Can I check if a loop's scope is entered for the first time?
in thread Can I check if a loop's scope is entered for the first time?

Thanks Damian, but I'm afraid this is not solving my problem, just another one. *

From what I see in the sources of Var::Pairs are you binding a cycling iterator to the call place with the help of Devel::Callsite .

This is just a more secure way of using line and file from caller .

But this will only work well if you always finish the loop without exiting in the middle, since you are only initializing once by the first entry and letting the iteration starting new after a full loop.

What I'm looking for is to re-init the iterator when it is re-entered again. Hence to safely use last and return or other loop exits.

Consider this example showing that your iterators are not reset after breaking the loop.

use warnings; use strict; use feature 'say'; use Var::Pairs; my %hash; @hash{"a".."d"} =1..4; sub till { my $goal = shift; while (my $next = each_pair %hash) { say $next->key, ' had the value ', $next->value; last if $next->key eq $goal; } say "-"x10 . "exit"; } till("a"); till("a"); till("a"); till("a");

/usr/bin/perl -w /home/lanx/pm/t_var_pairs.pl a had the value 1 ----------exit d had the value 4 b had the value 2 c had the value 3 ----------exit a had the value 1 ----------exit d had the value 4 b had the value 2 c had the value 3 ----------exit Compilation finished at Sun May 6 21:54:21

update

NB: each has of course the same (and more) limitations.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

*) There are different kinds of iterators, please correct me if I'm missing a way to apply this to my kind of iterators ...

Replies are listed 'Best First'.
Re^3: Can I check if a loop's scope is entered for the first time? (Devel::Callsite)
by TheDamian (Vicar) on May 07, 2018 at 04:11 UTC
    Apologies, Rolf. I misunderstood the actual problem.
    And, yes, it is an issue with Var::Pairs too.
    And, yes, I will now look at solving it. ;-)

    Damian

      Well, that took longer than I expected. ;-)

      I just uploaded a new release of Var::Pairs (version 0.003005)
      that solves the problem of resetting an iterator on premature termination
      of the surrounding loop. It does so by using Scope::Upper::reap()
      to install a "destructor" for each iterator when execution leaves
      the surrounding scope.

      Take another look; I think it's likely this technique would also work for you.

      Damian

        I think it's likely this technique would also work for you.

        In fact, more than just likely...

        sub from (&$) { use Devel::Callsite qw< callsite context >; use Scope::Upper qw< reap UP >; my ($iterator_source) = @_; # Uniquely identify this call, according to its lexical context... my $ID = callsite() . context(); # Install the iterator... state %iterator_for; $iterator_for{$ID} //= $iterator_source->(); # Install destructor for iterator at end of caller's block... reap { delete $iterator_for{$ID} } UP UP; # Iterate... return $iterator_for{$ID}->($_[1]); } for my $limit (reverse 1..5) { while ( from{countdown($limit)} my $a ) { print "$a: "; } print "\n"; } sub countdown{ my $val = shift; my $iter = sub { if ($val--) { $_[0]=$val; return 1; } return; # stop iteration }; return $iter; }