in reply to Re: Re: parsing comments in newline-delimited files as lists
in thread parsing comments in newline-delimited files as lists

You pushed my "$1 used outside the context of a conditional" button here. That'd be failed in a code review if I were running the show. And yes, after I stared at the code for a minute or so, I can see that the assertion from the previous line ensures that there's always a match. But in that case, why not make the match, the match!
while (<DATA>) { chomp; s/#.*$//; next unless /([^\s]+)/; push(@enabled_lines, $1); }
There: it's now clear to me that we can't get to the push unless the match succeeds. I'd let this stand in a code review, but if I was looking for further optimization, I'd just keep pressing forward for more clarity:
while (<DATA>) { chomp; s/#.*$//; push(@enabled_lines, $1) if /([^\s]+)/; }
Nicer. Tighter. Dare I say, "faster" as well? But I see some equivalances that are down in the "nice" category (first was "must", second was "want", now "nice"):
while (<DATA>) { chomp; s/#.*$//; push @enabled_lines, $1 if /(\S+)/; }
There. Clean, maintainable, pretty. I don't know if this does what the original poster wanted, but I didn't change the meaning at all from the node to which I'm replying.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Re: Re: parsing comments in newline-delimited files as lists
by Anonymous Monk on Dec 28, 2001 at 01:21 UTC
    Without taking this code twisting too far, I think this is even nicer:
    while (<DATA>) { s/\s*#.*//; push @enabled_lines, /\s*(.+)/; }