That's a pity. My testing was brief because I was about to leave for a long motorcycling weekend. I've had a look again on my return and it seems that my method would only work if I add anchors to the match. In that case it would only be equivalent to the exact match if ( $var_a =~ /^abcdefgh$/ || ... and not the more general match of the OP. Here is a modified version

use strict; use warnings; my $w = q{}; my $x = q{}; my $y = q{}; my $z = q{}; cmpOr(); $x = q{abc}; cmpOr(); $z = q{abcdefghij}; cmpOr(); $z = q{abcdefgh}; cmpOr(); $y = q{abcdefgh}; cmpOr(); $z = q{abcdefghij}; cmpOr(); sub cmpOr { print qq{Comparing OR methods with:-\n}, qq{ \$w is ->$w<-\n}, qq{ \$x is ->$x<-\n}, qq{ \$y is ->$y<-\n}, qq{ \$z is ->$z<-\n}; if ($w =~ /abcdefgh/ || $x =~ /abcdefgh/ || $y =~ /abcdefgh/ || $z =~ /abcdefgh/) { print qq{ orig: true\n}; } else { print qq{ orig: false\n}; } if (q{abcdefgh} =~ /^(?:$w|$x|$y|$z)$/) { print qq{johngg: true\n}; } else { print qq{johngg: false\n}; } print qq{\n}; }

When run this produces

Comparing OR methods with:- $w is -><- $x is -><- $y is -><- $z is -><- orig: false johngg: false Comparing OR methods with:- $w is -><- $x is ->abc<- $y is -><- $z is -><- orig: false johngg: false Comparing OR methods with:- $w is -><- $x is ->abc<- $y is -><- $z is ->abcdefghij<- orig: true johngg: false Comparing OR methods with:- $w is -><- $x is ->abc<- $y is -><- $z is ->abcdefgh<- orig: true johngg: true Comparing OR methods with:- $w is -><- $x is ->abc<- $y is ->abcdefgh<- $z is ->abcdefgh<- orig: true johngg: true Comparing OR methods with:- $w is -><- $x is ->abc<- $y is ->abcdefgh<- $z is ->abcdefghij<- orig: true johngg: true

In the particular circumstance of wanting each variable to match the string exactly, this method could save a deal of typing but it's no use in the general case as you pointed out.

Just out of curiosity I had a try at an alternative to if ( $var_a =~ /^abcde$/ && $var_b =~ /^abcde$/ && .... This is what I came up with.

if (q{abcde} =~ /^(?=$var_a$)(?=$var_b$)(?=$var_c$)/)

Cheers,

JohnGG


In reply to Re^3: howto make a very long IF statment shorter by johngg
in thread howto make a very long IF statment shorter by jeanluca

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.