From: Dossy Date: 2002-05-19T22:13:56+09:00 Subject: Re: Ruby regex question On 2002.05.19, David Alan Black wrote: > > 3086 # read header > > 3087 my($line_count,$last_updated); > > 3088 while (@lines) { > > 3089 my $shift = shift(@lines); > > 3090 last if $shift =~ /^\s*$/; > > 3091 $shift =~ /^Line-Count:\s+(\d+)/ and $line_count = $1; > > 3092 $shift =~ /^Last-Updated:\s+(.+)/ and $last_updated = $1; > [...] > > > > Maybe this portion of CPAN.pm wasn't written by a Perl guru. > > But in this example, the matches are indeed being checked, prior to > saving $1. True enough, we're relying on $line_count being undef from line 3087. Gotta love Perl! > > You are right though, that it is more common to find regex > > success immediately with the regex evaluation. Perhaps > > it's because with Perl, you can't know if $1 is valid (applies > > to the regex you just evaluated) _unless_ you test if the > > regex succeeded, which _forces_ you to test for the regex > > success. Ruby doesn't force you to do this, so the only > > reason for it to be a Ruby idiom is because it's a carry-over > > from Perl. > > I agree. Of course there's nothing wrong with checking for the match, > and there are situations where it's precisely what one wants to do. Right. When you're trying to match purely to see if a string matches a regex, then of course, check it then. But, when you're using regexes to parse, let the objects downstream do the checking -- they have to do it anyway, why duplicate code checking it twice? Anyway, this thread is totally OT now. ;-) -- Dossy -- Dossy Shiobara mail: dossy@panoptic.com Panoptic Computer Network web: http://www.panoptic.com/ "He realized the fastest way to change is to laugh at your own folly -- then you can let go and quickly move on." (p. 70)