mike65535 has asked for the wisdom of the Perl Monks concerning the following question:

Monk-dom,

I have some code that's wrapped with a test:

if( $ARGV[1] eq "ALL" )
I want to improve that test to be case-Insensitive and also to include the numeric '0'.

Like this

if ( $ARGV[1] eq (/^ALL|0)$/i) )
but it appears that's not a valid implementation (not sure what it does!)

Thanks for your help!

Replies are listed 'Best First'.
Re: Testing a command line argument
by toolic (Bishop) on Dec 10, 2015 at 17:35 UTC
    Use the bind operator (=~) instead of eq for regular expressions, and the parens are in the wrong place:
    if ( $ARGV[1] =~ /^(ALL|0)$/i ) {
      Thanks for the "bind" tip.

      Sorry for the parens mistake (and the ensuing trouble it caused!) I have to use a separate computer to develop code on from the one on which I have Internet access so it's difficult to lift the code directly (without burning a CD, believe it or not).

      Thanks again.

Re: Testing a command line argument
by BillKSmith (Monsignor) on Dec 10, 2015 at 20:33 UTC
    Fixing your operator as others have suggested will give you valid perl code. It still may not do exactly what you want. First note that ARGV[1] refers to the second argument, not the first. A regular expression treats the argument as a string. Your regex will match any string that begins with the digit "0" or the characters "ALL" (case insensitive). I am not sure that this agrees with your idea of numeric '0'. (it will match the strings such as "0h, my" or "007") You probably do not intend it to match a mixed case string such as "aLl oR NoTHinG", but it will. It is often difficult to specify all the special cases correctly. It is impossible to get the regular expression right before you do.
    Bill
Re: Testing a command line argument
by james28909 (Deacon) on Dec 10, 2015 at 17:37 UTC
    try:
    if ( $ARGV[1] =~ (/^ALL|0)$/i) )
    Ive noticed that when using 'eq' it likes to use '' instead of //. So try swapping the eq to =~
      That's wrong.
      Unmatched ) in regex; marked by <-- HERE in m/^ALL|0) <-- HERE $/
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
    A reply falls below the community's threshold of quality. You may see it by logging in.