Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Next from inner loop only works "most of the time"

by jo37 (Deacon)
on May 18, 2021 at 16:01 UTC ( [id://11132714]=note: print w/replies, xml ) Need Help??


in reply to Re: Next from inner loop only works "most of the time"
in thread Next from inner loop only works "most of the time"

I don't think the multiple usage of $_ is problematic here. According to perlsyn the global $_ gets localized in a foreach loop. Indeed:

#!/usr/bin/perl use v5.16; use warnings; foreach (1, 2) { say for qw(a b); say; foreach (10, 20) { say; } say; } __DATA__ a b 1 10 20 1 a b 2 10 20 2

Greetings,
-jo

$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

Replies are listed 'Best First'.
Re^3: Next from inner loop only works "most of the time"
by tobyink (Canon) on May 18, 2021 at 19:07 UTC

      I completely agree except I'd go slight further and suggest that anything that is bad for readability is, perforce, bad for maintainability - if you can't read it you can't maintain it (although you may be able to completely replace it).

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re^3: Next from inner loop only works "most of the time"
by Anonymous Monk on May 18, 2021 at 23:52 UTC
    Nonetheless, "for(each) my $var ..." is what's recommended, and with very good reason. For one thing, inner loops (or the code which they contain) usually needs to be able to refer separately to the loop-indexes of one or more of the outer loops. This achieves that without polluting the namespace.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found