in reply to matching problem
Balaton: Athanasius has replied:
... I think it’s highly unlikely that this line:
if ($line =~ ModuleMatching::MatchLAC($locus_acc_no)) {
can be correct. But without knowing what sub ModuleMatching::MatchLAC is supposed to do, it’s hard to give advice.
I agree that the definition of ModuleMatching::MatchLAC() given in the OP is most likely some sort of dummy placeholder, but in any event, the given function can be explained as follows (this is for Balaton; I believe Athanasius understands all this quite clearly):
- The function returns the result of a match of the passed argument $_[0] against a literal regex (i.e., a regex having no interpolations (Update: literal regex: my terminology may be a bit off here));
- The function is called in scalar context imposed by the =~ operator, so the result of the match within the function is either 1 (successful match) or '' (the empty string; match failed);
-
The '' or 1 returned by the function is then converted to a regex (with 1 stringized to '1') and a match is made against $line. If the match is against /1/ the result is obvious.
If the match is against // (the empty regex, created from the empty string), the result will come from a match against the regex most recently matched or, if no regex has ever been matched, against the null regex, which matches anything.Straight from the docs: "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)."
The problematic results of these matches can be illustrated as follows:
>perl -wMstrict -le "for my $line ('', 'X', 'Y') { for my $locus_acc_no ('', 'X', 'Y') { if ($line =~ MatchLAC($locus_acc_no)) { print qq{ match: '$line' =~ MatchLAC('$locus_acc_no')}; } else { print qq{NO match: '$line' =~ MatchLAC('$locus_acc_no')}; } } } ;; sub MatchLAC { return $_[0] =~ /^X$/; } " match: '' =~ MatchLAC('') NO match: '' =~ MatchLAC('X') match: '' =~ MatchLAC('Y') match: 'X' =~ MatchLAC('') NO match: 'X' =~ MatchLAC('X') match: 'X' =~ MatchLAC('Y') match: 'Y' =~ MatchLAC('') NO match: 'Y' =~ MatchLAC('X') match: 'Y' =~ MatchLAC('Y')
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: matching problem
by Athanasius (Archbishop) on Feb 26, 2013 at 04:13 UTC | |
by AnomalousMonk (Archbishop) on Feb 26, 2013 at 20:49 UTC |
In Section
Seekers of Perl Wisdom