Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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')

In reply to Re: matching problem by AnomalousMonk
in thread matching problem by Balaton

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (6)
    As of 2017-10-19 17:06 GMT
    Find Nodes?
      Voting Booth?
      My fridge is mostly full of:

      Results (255 votes). Check out past polls.