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

I have been using Parallel::ForkManager for some time, but I don't understand the place in the documentation that says The "and next" skips the internal loop in the parent process.

Today I tried to do the fork with the usual $fork_manager->start and next; within a subroutine, but I am getting an error, namely Exiting subroutine via next at ... line ...

Can someone explain the "next" part or help me understand what is going on? I know what "next" does in a while or for loop, I just don't understand it here. Do I need to post a minimal example to make it clear what my question is?

Replies are listed 'Best First'.
Re: "next" in Parallel::ForkManager, and subroutines
by tobyink (Canon) on Apr 04, 2012 at 11:49 UTC

    You use next to jump out of a sub. That's fine. It's supported.

    Minimal example:

    use 5.010; use strict; sub my_sub { next }; for (1..5) { say "Hello"; my_sub(); say "World"; }

    This says "Hello" five times, but does not say "World" because the sub my_sub calls next.

    While it's supported by Perl, it's also considered a slightly odd thing to do - and quite non-obvious (the sub my_sub could be defined hundreds of lines away from the loop which uses it, or even in another file), so Perl warns you about it, just in case you're doing it accidentally. If you run my example with warnings switched on, you'll get five warnings about exiting the sub using next, but these are just warnings, not errors. The script prints out a warning, but keeps on running.

    If you're doing it deliberately, and don't want to be warned about it then:

    no warnings qw[exiting];
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Hmm. The docs for next include:

      "next" cannot be used to exit a block which returns a value such as "eval {}", "sub {}" or "do {}", and should not be used to exit a grep() or map() operation.

      Doesn't sound supported to me.

        I think this is referring to the fact that you can't use next to "just" jump out of a sub (or do or eval) - it needs to jump out of another block (which does not return a value) at a higher level. Compare this:

        sub foo () { next } foo

        with this:

        sub foo () { next } foo for 1

        The first dies with Can't "next" outside a loop block; the second runs successfully, but will warn when warnings are enabled.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: "next" in Parallel::ForkManager, and subroutines
by mrguy123 (Hermit) on Apr 04, 2012 at 11:57 UTC
    I think this post might help you
    Also, if you post a minimum example where the error occurs it will allow us to help you a bit more
    Mr Guy
Re: "next" in Parallel::ForkManager, and subroutines
by Anonymous Monk on Apr 04, 2012 at 12:51 UTC
    Next, use $pm->start to do the fork.   $pm returns 0 for to the child process, and child pid for to the parent process.

    “Short circuit” expression evaluation means that the next statement will only be executed if the first half was True, i.e. not-zero, i.e. we are the parent process.

      I think the point here is:   next does exactly what it always does.   What’s peculiar at first glance is the use of “short circuit” expression evaluation as a substitute for clarity.

      You see the same sort of thing in statements like:   do_something() or die(“horribly”);   What sounds like a not-so veiled threat, is actually just short-circuit again.   If do_something() returns true, the die() part will not be executed.

      Call me a luddite or simply a non-golfer, but I never write it that way.   I don’t mind using two statements where one statement will do, if by doing so I will eliminate the need for threads like these.   To me, perfect clarity always trumps brevity, and there is no place in the world of engineering for religion (or golf).