Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: while loop break when sub call

by kennethk (Abbot)
on Jan 09, 2015 at 17:04 UTC ( [id://1112782]=note: print w/replies, xml ) Need Help??


in reply to while loop break when sub call

The thread to date sounds like you are not posting enough information, and that you've scrubbed your example too much to effectively debug. Please read How do I post a question effectively?, in particular, make sure that you post self-contained code that shows the problem. What happens in the subroutine? Nothing that's been posted should be resetting the getgrent iterator, though if you invoke it in your subroutine, that could mess you up. Note that the one-liner
perl -E 'sub func {"@_"} while (($name) = getgrent){if ($name =~ /^som +etext_(\w+)/){func($name,$1);say $name}}';
runs just as I would expect, with no infinite loop.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: while loop break when sub call
by papillon (Initiate) on Jan 09, 2015 at 18:16 UTC

    Your comment pointed my mistake. In the subroutine, I create a new group, that must be the source of the problem.

    Using an intermediate array solved the problem. One of those cause that is so evident that you don't see it

      Yes, undoubtedly the creation of a new group broke the iterator.   That sort of thing happens a lot, and it goes back to the underlying operating system primitives.   For example, I’ve found that when iterating through files and directories in Microsoft Windows (regardless of language), you mustn’t create/remove files or directories while doing so.   First complete the scan (e.g. of one complete subdirectory), then act upon/iterate through an in-memory list of the things that the scan finds, and all is well.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-26 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found