Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

regex in list context

by azadian (Sexton)
on Oct 28, 2009 at 13:15 UTC ( #803670=perlquestion: print w/replies, xml ) Need Help??
azadian has asked for the wisdom of the Perl Monks concerning the following question:

According to The Book, a regex which fails in a list context returns a null list. Here's my test: perl -e 'print "MATCHED\n"  if @list = q(foo bar baz) !~ m/\bbar\b/' This yields "MATCHED" for me with Perl version 5.8.8. Am I missing something, or is this a Perl bug? Looking at it more closely, I discover that what I get is a list with a single (empty) element.

Replies are listed 'Best First'.
Re: regex in list context
by jakobi (Pilgrim) on Oct 28, 2009 at 13:22 UTC

    -MO=Deparse shows print "MATCHED\n" if @list = !('foo bar baz' =~ /\bbar\b/);, which explains your observation. Which I read as an implicit "cast" from scalar not to list context for the assignment, similar to @list=5 implying @list=(5).

    (initially I thought about about =~ possibly being used in list context as well, but that's nonsensical).

    I've problems imagining sensible semantics for an alternate implementation of list context (matching substrings) on a non-match operator like !~, so the current solution above seems sanest to me.

    However man perlop could be more explicit: Binary "!~" is just like "=~" except the return value is negated in the logical sense. Elsewhere, same document: When there are no parentheses in the pattern, the return value is the list "(1)" for success. With or without parentheses, an empty list is returned upon failure.

    cu & HTH, Peter -- hints may be untested unless stated otherwise; use with caution & understanding.

      I think this explains why I don't like using !~. Ages ago, it "didn't do what I expected" and I learned from this only "don't use !~". Time passed, and I forgot what I was trying to do that didn't work, but I was still avoiding !~ in places it might have worked. I think this just might explain where my superstition came from.

      Thank you!

        Now that I see the answer I remember discussing this issue and IMHO it makes some sense.

        What kind of list should be the logical result of a doesn't-match operator?

        Anyway it breaks the believe in orthogonality and that doubling negations leads to the same results...

        ... which is indeed not exceptional in perl's "DWIM universe".

        So the OP should better use unless and =~.

        Cheers Rolf

Re: regex in list context
by azadian (Sexton) on Oct 28, 2009 at 13:57 UTC
    Thanks, that's what I needed to know. "Could be more explicit" indeed. It even offers "doesn't contain" as an improved pronunciation for !~
Re: regex in list context
by ikegami (Pope) on Oct 28, 2009 at 17:39 UTC

    You're not assigning the result of the match, you're assigning the result of !~.

    The result of != is the logical negation of the result of its RHS argument.

    (The result of ~= is the result of its RHS argument.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://803670]
Approved by moritz
Front-paged by almut
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-02-24 13:28 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (354 votes). Check out past polls.