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 | |
by ikegami (Patriarch) on May 27, 2009 at 14:17 UTC | |
by rovf (Priest) on May 27, 2009 at 14:57 UTC | |
by ikegami (Patriarch) on May 27, 2009 at 15:01 UTC | |
by rovf (Priest) on May 27, 2009 at 15:08 UTC | |
by Anonymous Monk on May 27, 2009 at 14:01 UTC |