Skip to content

[doc] perlnumber: Negative octal number as a string evaluates to decimal number in numeric operations #18583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rsFalse opened this issue Feb 15, 2021 · 6 comments · Fixed by #23258

Comments

@rsFalse
Copy link

rsFalse commented Feb 15, 2021

Where

https://perldoc.perl.org/perlnumber#SYNOPSIS

Description

Seems undocumented behavior. Quoted negative octal number evaluates to decimal:

(perl v5.32.0)

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $A = -010;
my $B = "-010";
my $C = "- 010";

print map "[$_]", 
	$A, $B, $C,
	;

print map "[$_]", map $_ + 1,
	$A, $B, $C,
	;

print map "[$_]", map ++ $_,
	$A, $B, $C,
	;

OUTPUT:

[-8][-010][- 010]
Argument "- 010" isn't numeric in addition (+) at negative_octal_1.pl line 16.
[-7][-9][-9]
[-7][-9][-9]
@Grinnz
Copy link
Contributor

Grinnz commented Feb 15, 2021

"Negative octal number in a string" is not a thing. You can apply the unary minus to an octal numeric literal, as you do with $A, but the other two are not valid ways to create octal numbers - you need the oct or hex function to even create a number from a 0-prefixed string (these are just being interpreted as -10).

@rsFalse
Copy link
Author

rsFalse commented Feb 18, 2021

If my thread name is misleading, feel free to suggest another, I can change.

  1. Strange thing is that warning says "- 010" isn't numeric, but the string still become converted to a number, but not to zero.
  2. Does "-010" look like number? There is no warning. But is it a correctly written decimal?

@briandfoy
Copy link
Member

briandfoy commented Mar 26, 2021

Perl has native numbers and decimal number strings. There is no octal number string. perlnumber is very clear about this. Converting a string to a number works with decimal numbers only. Leading space is ignored.

These all give the answer I expect:

$ perl -E 'say "-010" + 0'
-10
$ perl -E 'say "- 010" + 0'
-10
$ perl -wE 'say "- 010" + 0'
Argument "- 010" isn't numeric in addition (+) at -e line 1.
-10
$ perl -wE 'say " 010" + 0'
10

The weird thing is the warning, and I expect there's a sequencing issue with the number conversion. Note that the last example issues no warning. Perl ignores the leading whitespace before the digits. It's also ignoring the leading whitespace the penultimate example, but it's already seen a non-digit character that is part of the number.

@briandfoy
Copy link
Member

Also, see #18595

@sisyphus
Copy link
Contributor

I think that strings beginning with "0"are being regarded as decimal numbers, and the leading "0" is simply being (correctly) ignored.

C:\>perl -wle "print '- 010' + 0;"
Argument "- 010" isn't numeric in addition (+) at -e line 1.
-10

That warning looks to be unrelated to the leading "0".
Instead, it seems to be in response to the space that follows the minus sign:

C:\>perl -wle "print '- 10' + 0;"
Argument "- 10" isn't numeric in addition (+) at -e line 1.
-10

I don't know if those 2 one-liners should emit the non-numeric warning. or not.
But I think it's correct that they both behave in the same way.

@briandfoy
Copy link
Member

Here are a few other tickets that seem to be related:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants