| Tom Phoenix 2007-06-28, 3:59 am |
| On 6/27/07, Mihai Vlad <mihai.vlad@axigen.com> wrote:
> $a = 'whatever';
> $b = '';
>
> $test = 'x\ y';
> $test =~ s/\\ / /;
This replaces a backslash and a space with a space. But it's also a
successful pattern match, which will matter in a moment.
> if ( $a =~ m/$b/ ) {
What's that pattern? It's not an empty pattern. According to perlop's
entry on m// :
If the PATTERN evaluates to the empty string, the last successfully
matched regular expression is used instead. .... If no match has
previously succeeded, this will (silently) act instead as a genuine
empty pattern (which will always match).
So by using the empty string, you triggered this obscure feature. You
might have wanted something like this for $b to get a true empty
pattern:
my $b = qr/(?#empty)/;
You're not the first person to discover this feature by accident,
though; that's one of the reasons it's been removed in Perl 6.
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
|