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

Hi,

I wrote a program on my workstation using 5.10.1, and used (?| ... ) in my regex which made life very easy for using the capturing parenthesis in an expression w/ alternatives ... is there a simple way to achieve a similar result in perl 5.8.8?

(?|OTHFIL\.(A-Z{3})|(A-Z{3})FIL)

Cheers

Replies are listed 'Best First'.
Re: perl 5.8.8 equivalent of ?|
by JavaFan (Canon) on Dec 13, 2010 at 01:10 UTC
    There isn't any simple way of archieving the same thing in 5.8.8 (although for individual examples, there may be). If there was, there wouldn't have been a need for (?|).
Re: perl 5.8.8 equivalent of ?|
by Anonyrnous Monk (Hermit) on Dec 13, 2010 at 00:38 UTC

    In this particular case, you could probably do something like this (as the non-matching captures aren't defined):

    #!/usr/bin/perl -l for (qw(OTHFIL.FOO BARFIL)) { my ($foo) = grep defined($_), /OTHFIL\.([A-Z]{3})|([A-Z]{3})FIL/; print $foo; } __END__ FOO BAR
Re: perl 5.8.8 equivalent of ?|
by ambrus (Abbot) on Dec 13, 2010 at 07:52 UTC

    Just use a simple /(?: instead of /(?|, and then if there's a mathc, the magical $+ variable will contain what you need.

      Using $+ only works with one capture per alternation, though.  In other words, it's not a general substitute for (?|...).

        True. The general substitute (even more general than /(?| is) is named captures, but sadly those too aren't available in perl 5.8.8; or the defined-or operator like $1//$2, which too isn't in perl 5.8.8.