[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

[perl #57706] Unary minus on 'numeric' inputs like '-1'



Hi,

> % perl -E 'say -"-1"'
> +1
> % perl -E 'say - -1'
> 1
> % perl -E 'say 0 - "-1"'
> 1
>
> [...]
>
> I would expect to see
>
> % perl -E 'say - -1'
> 1
> % perl -E 'say -"-1"'
> 1
> % perl -E 'say -"-foo"'
> +foo
>
> To me, that matches the documentation better than the current
behaviour.

The documentation is a bit inconsistent:

perlop says "Otherwise, if the string starts with a plus or minus, a
string starting with the opposite sign is returned."

Your first example is a string. So the current behaviour seems to be
correct.

OTOH, perlnumber says "The binary operators +  -  *  / %  ==  !=  > < >=
 <=  and the unary operators -  abs and --  will attempt to convert
arguments to integers"

...

The attached patch adds the conversion if the value of the SV
looks_like_number...

Nevertheless, the documentation should be fixed, no matter what
behaviour is "correct".

See also the discussion in #36675
(http://rt.perl.org/rt3/Public/Bug/Display.html?id=36675)

Cheers,
Renée

57706.patch


[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]