in reply to Why do I need parentheses here?

"?" can start both a term (?PATTERN?) and an operator (the conditional operator). Perl has no way of knowing which one you intended here. Since it followed a sub call, Perl assumed you were specifying arguments, so it treated the "?" as the start of a term. Same goes for "/", unary-"-" and unary-"+".

Solutions:

func() ? '-N ' : ''
(func) ? '-N ' : ''
sub func() {} func ? '-N ' : ''

Is it just to comply with those internal functions such as grep or split, which accept a regexp as first argument, or is there a deeper reason for this seemingly oddity?

No. func ?PATTERN?; is no different than func $x+1; (and even more similar to func -3;). There's nothing special (or odd) about it.

Replies are listed 'Best First'.
Re^2: Why do I need parentheses here?
by rovf (Priest) on May 27, 2009 at 13:57 UTC
    func ?PATTERN?; is no different than func $x+1;. There's nothing special (or odd) about it.

    I think I got it. So func ?PAT? is just another way of writing

    func($_ =~ ?PAT?)

    -- 
    Ronald Fischer <ynnor@mm.st>
      Yes. ?PAT? and $_ =~ ?PAT? are equivalent, just like /PAT/ and $_ =~ /PAT/ are. (Aforementioned split is exceptional since it treats /PAT/ as qr/PAT/ or similar.)

        Is there any difference between /PAT/ and qr/PAT/ ??? I thought this is *always* the same.

        -- 
        Ronald Fischer <ynnor@mm.st>
      Checking ;)
      C:\>perl -MO=Deparse,-p -e"sub foo{} foo ?bar? sub foo { } foo(?bar?); -e syntax OK
      Yes