in reply to •Re: On Foreach Loops and Maintainability
in thread On Foreach Loops and Maintainability

<aol>me too</aol>

And to take this a bit further, if there's no else, I'd write this like so:

for (map("arg$_", 1..5, 19), "gee37") { $object->method($_) or next; # ... }

Fewer indentation levels are generally easier to read. Finding this at the bottom of a function always makes me scroll up and down a couple times:

# ... } } } # play "guess the block this belongs to" here $_ .= "something"; } } }

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^2: On Foreach Loops and Maintainability
by BrowserUk (Patriarch) on Oct 11, 2002 at 21:11 UTC

    Is there a reason that the idea merlyn mentioned (that I think is a excellent use of perl) was comprehensively glossed over?

    I'd also try and do away with the if block but I think cond or next; reads a little unintuatively and would probably use
    next unless cond;

    for ( 'arg1' .. 'arg5', "arg19", 'gee13' .. 'gee37' ) { next unless $object->method( $_ ); # do stuff }

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      It depends on what I want to emphasize: I use EXPR or next; when a failure at that point is the exception. Of course, looking back at the original poster's code, there's nothing that says it is - and if not, I agree, I'd use a next if. I do so 95% of the time anyway. The flexibility of Perl's syntax allows a natural way of conveying emphasis, and I try to make use of that when appropriate. "Self explanatory" is usually my top goal.

      Makeshifts last the longest.