On Tue, Nov 3, 2009 at 4:33 PM, tom smith <[email protected]>wrote:
> Thanks for the tips! More comments below.
> I saw it written the other way somewhere, and I thought it looked cleaner.
> I'll do it your way from now on.
>
>
>>
>> if ($line =~ /\((.*?)\)/) {
>>>
>>> $line =~ s/$1/$i;$j/;
>>>
>>
>> If you have two regular expressions that are doing the same thing then you
>> are probably doing it incorrectly:
>>
>> if ( $line =~ s/\((.*?)\)/($i;$j)/ ) {
>>
>
> Ah. Nice.
>
Wait a minute, on further review I don't understand how that if condition
works. According to perlop:
----
s/PATTERN/REPLACEMENT/egimosx
Searches a string for a pattern, and if found, replaces that
pattern with the replacement text and returns the number of
substitutions made. Otherwise it returns false
(specifically,
the empty string).
....
-----
So if no substitution takes place, s/// returns an empty string. Then in
the case of no substitution, wouldn't that leave me with:
if ($line =~ "")
??? And that condition always evaluates to true as far as I can tell.
Your if statement does work as advertised:
use strict;
use warnings;
$\ = "\n";
my $line = 'hello';
if ($line =~ s/z/e/) { #no z's in 'hello', so no substitution
print "matched!";
print $line;
} else {
print "no match";
print $line;
}
--output:--
not match
hello
...but I don't understand why it works.