Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

last/next/redo usages

by hurricup (Pilgrim)
on Mar 20, 2018 at 20:29 UTC ( [id://1211372]=perlquestion: print w/replies, xml ) Need Help??

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

Perldoc says pretty same for them, like

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.

I read cannot as doesn't work. But. In some modules, like Par::Dist I can see code like:

File::Find::find( sub { next unless $File::Find::name; (-r && !-d) and push ( @files, substr($File::Find::name, 5) ); } , 'blib' );

What is it? Outdated documentation? Do I get cannot wrong way? Some tricky next, like sub?

Also, TIL, missing in docs, that last/next/redo may be used in statement modifiers, like say $_ and last for 1,2,3

I'm working on new code inspection, so question is not rythorical.

Replies are listed 'Best First'.
Re: last/next/redo usages
by haukex (Archbishop) on Mar 20, 2018 at 20:36 UTC

    Good question where it's documented, but yes, it is possible, although it warns:

    use warnings; use strict; use diagnostics; sub foo { last if /e/; next if /c/; redo if s/b/B/ } my @x = qw/a b c d e f/; for (@x) { foo; print "$_\n"; } __END__ a Exiting subroutine via redo at test.pl line 6 (#1) (W exiting) You are exiting a subroutine by unconventional means, +such as a goto, or a loop control statement. B Exiting subroutine via next at test.pl line 6 (#1) d Exiting subroutine via last at test.pl line 6 (#1)

    Update: See also e.g. Exiting subroutine via next

      Here is related bug and devs discussion.

Re: last/next/redo usages
by LanX (Saint) on Mar 20, 2018 at 23:32 UTC
    I seem to remember that last/next/redo are implemented as edge cases of goto LABEL.

    Provided the target label is set it's possible to goto out of a sub into a scope in the dynamic caller chain. (One use case is to short circuit leaving a recursion, without needing to step thru hundreds of returns*)

    So this behavior doesn't surprise me. (Though I would have expected to see a label)

    My bet is the documentation is wrong or badly worded or at least outdated.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

    update

    *) demo

    use strict; use warnings; use 5.12.0; sub count_down { my ($a)=@_; say --$a; goto EXIT unless $a; count_down($a) } count_down(10); say "never reached"; EXIT: say "byebye";

    9 8 7 6 5 4 3 2 1 0 byebye
Re: last/next/redo usages
by stevieb (Canon) on Mar 22, 2018 at 01:17 UTC

    I suspect, after realizing the author of the post, that this is research for either the IntelliJ Perl plugin, or the associated IDE debug CPAN integration.

    Kudos hurricup, you've done a spectacular job, and I must admit, that you've allowed me to streamline my C, C++, C#, Python and Perl programming tremendously by authoring your software.

    Please keep up keeping up on such wonderful work.

    (I normally don't praise anyone so loudly, but definitely ++).

    Thankfully you've come to the best of places to get feedback on such research.

    -stevieb

      Yes, it is for new inspection in the plugin and thank you, glad that plugin helps!

        Any one of my CPAN distributions I've added or edited within the last two-or-so years have been worked on wholly or at least partially using your plugin, and I've even gone as far as to have acquired a JetBrains Open Source license for their entire software suite thanks to the integration of Perl with their tools. (I just recently (February) received my second consecutive one-year-term OS license from them).

        The Open Source license is technically assigned to my berrybrew work, but if it weren't for your dedication to the IntelliJ platform, even though I use PyCharm professional for $work, I wouldn't even have learned about their Open Source dedication.

        Thanks again hurricup.

A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-26 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found