glasswalk3r,
Your code doesn't do what you think it does. You are saying if $text contains dog or $_ contains cow or $_ contains pig. This can be solved 1 of 2 ways.
local $_ = 'The dog is black cat is white and the fox does not like th +e cow or pig'; print "match found\n" if /dog/ || /cow/ || /pig/; # or by explicitly stating print "match found\n" if $text =~ /dog/ || $text =~ /cow/ || $text =~ +/pig/;
It is worth noting that alternation in regexen can be expensive but demerphq's patch to bleed perl (and hopefully the recently released 5.8.8) can make it much less so.

Update: At bobf's request, I am adding the following note as FYI.
The binding operator =~ has a higher precedence than || so /dog/ is seen by itself. Perl's do what you mean (DWYM) attitude allows that to be a valid expression by assuming you meant the binding operator with the default variable $_. See perlop for more information on operator precedence and perlvar for more info on variables.

Additionally, The order in which place the alternation should represent the order the items are most likely to appear. This is because the || short-circuits when it knows at least 1 condition is true. Use && when you want all conditions to be met. Since perl, like math, uses precedence in order to know which order things should be evaluated - be sure to read up in perlop. There are lower precedence versions of || and && (or, and) but nothing higher than that of the binding operator.

Cheers - L~R


In reply to Re^2: Understanding alternation by Limbic~Region
in thread Understanding alternation by Anonymous Monk

Title:
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, details, 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, summary, 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.