Respected Monks,

Please let me know if there is a way that I can insert the logical OR or AND operator in an if loop.

Reason to do this is, I have a condition wherein I need to check if a string contains colon ":" or if the string is not equal to 16 characters, the script should exit.

I know I can write if elsif else, but, am trying to find a way if the || operator, or the && operator can be inserted in an if loop so that I can say something like the following. I tried it out, but its not working.

if ($string=~m/:/g || length($string)!=16) { print "Either : already in string on the length is invalid\n"; exit; } else { work on that string }

Please let me know. Kindly note that I am not a trained programmer and though I have some extremely primitive C knowledge, I am a newbie to Perl, so any mistakes/misconceptions on my understanding of the if loop, please pardon me.

Perl Version - (v5.14.2) MSWin32-x64-multi-thread on Windows 7 64 Bit.

In reply to How to insert || and && in an if loop by perl514

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.