in reply to ||= oddity

$ perl -MO=Deparse -e '@bar ||= (1,2,3)' Can't modify array dereference in logical or assignment (||=) at -e li +ne 1, at EOF -e had compilation errors. @bar ||= ('???', '???', 3); $ perl -MO=Deparse -we '@foo ||= 1' Can't modify array dereference in logical or assignment (||=) at -e li +ne 1, at EOF -e had compilation errors. BEGIN { $^W = 1; } @foo ||= 1;
So it seems that ||= generally isn't supported in perl.

Update: ok, other monks explained why. ||= returns a scalar.

A quick code search shows that all examples "out there" use it only in lines that are commented out, so it seems to be a common mistake actually

Replies are listed 'Best First'.
Re^2: ||= oddity
by throop (Chaplain) on May 28, 2008 at 22:39 UTC
    Cool. I just learned how to do a Google code search.
      Yea me too, excellent. *veers off on a tangent* I've often wondered why Google doesn't allow regular expressions, or even most symbols in their regular search. Any ideas?
      ........
      Those are my principles. If you don't like them I have others.
      -- Groucho Marx
      .......
        Performance and convenience.

        I think that indexes work far better for constant strings than for regular expressions, and the regular search has a much larger index. It also means that they don't have to index non-word-characters, many of which are garbage anyway (programming languages being an exception, for which the code search was made).

        Convenience because you want to be able to search for CoolProduct, even if the official name is CoolProduct!. And usually you want an exact phrase to match even if some punctuation differs.

Re^2: ||= oddity
by jds17 (Pilgrim) on May 29, 2008 at 14:00 UTC
    As an aside: Arrays are treated in the same way when doing smart matching:
    use feature qw(say); say qq(no match) unless (1, 4, 18) ~~ 4; say qq(match) if (1, 4, 18) ~~ 18;
    results in
    no match match
    I tripped over this, too, see http://www.perlmonks.org/?node_id=687969.