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

while loop break when sub call

by papillon (Initiate)
on Jan 09, 2015 at 14:54 UTC ( [id://1112755]=perlquestion: print w/replies, xml ) Need Help??

papillon has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm calling a sub inside a while loop and it break the $_ from getgrent. I tried to used local($_) before calling the sub but to no avail.

while( (my $name,$passwd,$gid,$members) = getgrent() ){ if ( $name =~ m/^sometext_(\w+)/ ) { local($_); my $var = sub($name, $1); print ($name."\n"); } }

the while loop execute correctly until it reach a $name that start with sometext_. At that point, the while loop just repeat the over the same group and never exit.

Is there a way to let getgrent continue where it was?

Thanks,

Mélanie

Replies are listed 'Best First'.
Re: while loop break when sub call
by kennethk (Abbot) on Jan 09, 2015 at 17:04 UTC
    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.

      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.

Re: while loop break when sub call
by toolic (Bishop) on Jan 09, 2015 at 15:06 UTC
    Update your post using "code" tags around your Perl code: Writeup Formatting Tips

    Your code does not compile for me. I get this error:

    Illegal declaration of anonymous subroutine at ...

    and with diagnostics:

    Illegal declaration of anonymous subroutine at ... (#2) (F) When using the sub keyword to construct an anonymous subroutin +e, you must always specify a block of code. See perlsub.

    There are other errors if you use strict.

Re: while loop break when sub call
by sundialsvc4 (Abbot) on Jan 09, 2015 at 15:09 UTC

    You really need to post the code within <code> tags as described in Markup in the Monastery at the bottom of the submit-page.

    If you suspect that someting in sub() might be adversely affecting repeated calls to getgrent(), consider defining an array and pushing all matching names onto it.   Then, after the getgrent loop has finished, use a second loop to process the entries in the array.   Similar side-effects can happen e.g. when processing files within a loop that is scanning directories, on some types of machines.   So, first do one, then the other.

      Hello,

      Sorry for forgetting the <code> tags.

      I tried to fill an array for matching groups name and then iterating that array and it work

      Thanks

Re: while loop break when sub call
by LanX (Saint) on Jan 09, 2015 at 15:24 UTC
    Did you really name your function "sub"?

    That's a bad idea, sub is a reserved word.

    Cheers Rolf

    PS: Je suis Charlie!

      no, I did not do that mistake, it was just for the example. Should have named it to something else anyway

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1112755]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found