in reply to Did I match or didn't I?

My take is that the double-pipe "binary" OR operator is converting your list (the results from the m//) into a scalar, which in this case is 1 for a singular match. The 'or' OR operator does seem to preserve lists in their entirety, not converting them at all.

So, the fix, as mentioned, is:
my ($result) = m[pattern(group)] or "default";
The two OR operators actually behave quite differently, much in the same way that '==' and 'eq' do similar things, but in a different way.

The ?: trick is also valid:
my ($result) = m[pattern(group)]? $1 : "default";
Since in this case, you are evaluating the result of the match explicitly, then returning either $1 or the string. The former, using 'or', is probably the best way to handle this, since ?: is a bit confusing to some.

Replies are listed 'Best First'.
Re: Re: Did I match or didn't I?
by John M. Dlugosz (Monsignor) on Oct 18, 2001 at 19:38 UTC
    re: "The two OR operators actually behave quite differently"

    That is a whole new issue, since perlop says, "It's equivalent to || except for the very low precedence."

    What are the differences? And lets let the doc pumpking know.

    Updated: The precidence is indeed the only difference. The "as previously noted"

    my ($result) = m[pattern(group)] or "default";
    is not correct.
      The only difference between or and || is precedence. || has high precedence; or has low precedence.

        my @list = func_returning_list() || 'default'; This is parsed as my @list = (func_returning_list() || 'default');, which fails because || forces func_returning_list() into scalar context.   my @list = func_returning_list() or 'default'; This is parsed as (my @list = func_returning_list()) or 'default';, which fails because 'default' is not assigned to @list when func_returning_list() returns an empty list.

      Using ?: is the right solution.

      In perl6, the logic operators will allow their context to propagate to both their operands, so the first snippet will work as intended.