Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:41:03+00:00 2026-05-25T20:41:03+00:00

I almost feel like saying it’s me again!. Anyway, here we go. I like

  • 0

I almost feel like saying “it’s me again!”.

Anyway, here we go.

I like using while $object->next() style constructs. They appeal to me and seem “neat”.

Now, when the thing I’m iterating over is an array, it’s straightforward (“shift @ary or return undef“)

sub next {
   my ( $self, $args ) = @_;
   my $next = shift @{ $self->{list_of_things} } or return undef;
   my ( $car, $engine_size, $color )
       = split( /\Q$opts->{fieldsep}/, $next );
   $self->car         = $host;
   $self->engine_size = $engine_size;
   $self->color       = $color;

}

In this example I use AUTOLOAD to create the getters and setters and then have those instance variables available in my object during the while loop.

I’d like to do something similar but with the “list_of_things” being a %hash.

Here’s a non-OO example that doesn’t make it into the first iteration. Any ideas why?

(The total “list_of_things” is not that big – maybe 100 entries – so to do a keys(%{$hash}) every time doesn’t seem too wasteful to me).

use strict;
use warnings;
use Data::Dumper;

my $list_of_things = {
    volvo => {
        color => "red",
        engine_size => 2000,
    },
    bmw => {
        color => "black",
        engine_size => 2500,
    },
    mini => {
        color => "british racing green",
        engine_size => 1200,
    }
};

sub next {
    my $args = $_;
    my @list = keys( %{$list_of_things} );
    return undef if scalar @list == "0";
    my $next = $list_of_things->{ $list[0] };
    delete $list_of_things->{ $list[0] };
    return $next;
}

while ( next()) {
    print Dumper $_;
    print scalar keys %{ $list_of_things }
}

Is there a better way of doing this? Am I doing something crazy?

EDIT:

I tried Ikegami’s suggestion. Of course, Ikegami’s example works flawlessly. When I try and abstract a little, so that all that is exposed to the object is a next->() method, I get the same “perl-going-to-100%-cpu” problem as in my original example.

Here’s a non-OO example:

use Data::Dumper qw( Dumper );

sub make_list_iter {
   my @list = @_;
   return sub { @list ? shift(@list) : () };
}

sub next {
   make_list_iter( keys %$hash );
}

my $hash = { ... };

while ( my ($k) = next->() ) {
   print Dumper $hash->{$k};
}

It does not seem to get past the first step of the while() loop.

I am obviously missing something here…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:41:04+00:00Added an answer on May 25, 2026 at 8:41 pm

    If you don’t want to rely on the hash’s builtin iterator (used by each, keys and values), there’s nothing stopping you from making your own.

    use Data::Dumper qw( Dumper );
    
    sub make_list_iter {
       my @list = @_;
       return sub { @list ? shift(@list) : () };
    }
    
    my $list_of_things = { ... };
    
    my $i = make_list_iter(keys %$list_of_things);
    while (my ($k) = $i->()) {
       local $Data::Dumper::Terse  = 1;
       local $Data::Dumper::Indent = 0;
       say "$k: " . Dumper($list_of_things->{$k});
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some development skills, like refactoring operations, feel like they have an almost unlimited pontential
I am trying to setup the Twitter gem, and I feel like I'm almost
I feel like I'm missing something obvious here. I just wanted to try putting
I feel like I'm missing something simple here (as usual). I'm trying to read
Almost finished my java web app. All working fine. Whats next. I need now
Almost every major news media outlet is reporting that Apple will stop using Google
God i feel like i'm spamming stackoverflow, this is my 3rd post for today.
Okay I almost have this rebase thing figured out. I can feel a breakthrough
What I'd like to do is take some action using the value returned by
It's feel like I'm stuck, my friends. Can somebody explain me pick equations from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.