None of those are particularly useful in practice since they don't check if the regexp matched successfully (and don't lend well to being modified to do so). When the regexp doesn't match, $1 and similar variables are left unchanged and therefore meaningless. What follows are more practical patterns.
Using ?::
my $re = $left ? qr/ab(..)ef/ : qr/gh(..)jk/; /$re/ or die("Bad data"); print "Caught $1\n"
Using if/elsif:
my $re; if (...) { $re = qr/.../ } elsif (...) { $re = qr/.../ } elsif (...) { $re = qr/.../ } else { die '...' } /$re/ or die("Bad data"); print "Caught $1\n"
Using a lookup table:
my %re_lookup = ( ... => qr/.../, ... => qr/.../, ... => qr/.../, ); /$re_lookup{...}/ or die("Bad data"); print "Caught $1\n"
In reply to Re: ?: and saving backreferences
by ikegami
in thread ?: and saving backreferences
by throop
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |