Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Peek a hash without breaking iterator

by anonymized user 468275 (Curate)
on Jun 21, 2016 at 08:43 UTC ( [id://1166167]=note: print w/replies, xml ) Need Help??


in reply to Peek a hash without breaking iterator

If your debugger is incompatible with the 'each' magic, then the options are: use a different debugger, or don't use each.

A simple way to work around each would be to load it into an array and simulate each as follows:

$ perl -e ' > my %h=(qw(a b c d e f)); > { my @each = %h; > while (my $k = shift @each) { > my $v = shift @each; > print "$k => $v\n"; > }}' c => d e => f a => b
Update: you could also create an oo version of each that uses a handler object instead of magic so that it can't break under your circumstances. e.g.
package OOEach; sub new { my ($class) = @_; return bless {}, $class; } sub each { my $self = shift; $self->{queue} ||= [@_]; # note: will only init once my $k = shift @{$self->{queue}}; my $v = shift @{$self->{queue}}; return ($k, $v); } ;

One world, one people

Replies are listed 'Best First'.
Re^2: Peek a hash without breaking iterator
by Anonymous Monk on Jun 21, 2016 at 09:18 UTC
    Heheh, another one , hurricup is creating the debugger, hes writing the debugger, he cannot change the users code to suit the debugger,
      Actually he IS changing the user's code to effect debugging. As I understand it he is effectively inserting a command to copy the hash into the code he is debugging. So in fact a custom implementation of each is still one way to stop the real one breaking.

      One world, one people

        How? Code is running, enter inside loop, hash copy resets iterator. Too late for simulation. You mean add simulator before loop, through magic, before its needed because its ok to reset iterator at that point? How many frames back is safe? Too much overhead?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-16 17:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found