in reply to Regexp context with ||

|| always imposes a scalar context on its left hand operand, which is also why you can't usefully say @a = @b || @c.

One way to work around that in this case is to use the ternary operator instead:

$a = ("ac" =~ /a(b)c/) ? $1 : 'd'; print $a;

Update: Another approach is to use an array dereference, which is a way of retaining the ||, though I think it is less clear:

$a = ("abc" =~ /a(b)c/)[0] || 'd'; print $a;

Hugo

Replies are listed 'Best First'.
Re: Re: Regexp context with ||
by bsb (Priest) on Apr 09, 2003 at 02:30 UTC
    I see, thanks.

    In perlop I should've read

    @a = @b || @c; # this is wrong @a = scalar(@b) || @c; # really meant this
    instead of misinterpreting
    Scalar or list context propagates down to the right operand if it is evaluated.
Re: Re: Regexp context with ||
by JamesNewman (Initiate) on Apr 09, 2003 at 18:56 UTC

    Personally I prefer the (TEST)?TRUE:FALSE method (C many many years ago) but extending the idea of using a list (but not clarity):-

    ($a,$throw_away) = (("abc" =~ /a(b)c/) , 'd');

    if the RegEx doesn't match then you have a single element list containing 'd', if it does then 1st element is still 'b' the $throw_away variable just captures any remaining elements.

    or
    $a = ( ("abc" =~ /a(b)c/) , 'd' )[0];

    Taking this idea to an extreme, what about pulling set of values from the regex or setting all the variables to a set value.

    ($a,$b,$throw_away) = (("abcde" =~ /a(b)c(d)/),('UNDEFINED') x 3); print $a , "::" , $b ; # prints b::d and ($a,$b,$throw_away) = (("abcde" =~ /a(x)c(x)/),('UNDEFINED') x 3); print $a , "::" , $b ; # prints UNDEFINED::UNDEFINED