Skip to content
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

Add builtin::numify #21982

Draft
wants to merge 2 commits into
base: blead
Choose a base branch
from
Draft

Add builtin::numify #21982

wants to merge 2 commits into from

Conversation

leonerd
Copy link
Contributor

@leonerd leonerd commented Feb 12, 2024

Adds a new builtin function, builtin::numify, the number equivalent of builtin::stringify.

While here also remembers to add a test and documentation on how stringify handles undef. No behavioural change to stringify itself.

Comment on lines +7995 to +7996
SV *ssv = *PL_stack_sp;
if(SvNOK(ssv))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to SvGETMAGIC() before checking the flags:

$ ./perl ../21982.pl 
Built-in function 'builtin::numify' is experimental at ../21982.pl line 9.
Use of uninitialized value $magic in number conversion at ../21982.pl line 9.
0
123
$ cat ../21982.pl
#!perl
use v5.36.0;

my $val;

tie my $magic, "Foo";

$val = "123";
say builtin::numify($magic);
say $magic;

package Foo {
  sub TIESCALAR { bless {}, shift; }


  sub FETCH { $val }
}

Similarly:

$ ./perl -Ilib -E '$! = 0; open my $fh, "<", "unknown"; say builtin::numify($!); say $!+0'
Built-in function 'builtin::numify' is experimental at -e line 1.
0
2

Comment on lines +8000 to +8001
else
sv_setiv(TARG, SvIV_nomg(ssv));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overloaded 0+ that returns a non-integer:

$ ./perl -Ilib ../21982b.pl 
Built-in function 'builtin::numify' is experimental at ../21982b.pl line 11.
3.14159265
3
$ cat ../21982b.pl
#!perl
use v5.36.0;
package Foo {
  use overload
    '0+' => sub { 3.14159265 },
    fallback => 1;
}
my $x = bless {}, "Foo";

say 0+$x;
say builtin::numify($x);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one I'm not so sure about:

$ ./perl -Ilib ../21982c.pl 
Built-in function 'builtin::numify' is experimental at ../21982c.pl line 7.
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
3
$ cat ../21982c.pl
#!perl
use v5.36.0;
use bigfloat;
my $x = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;

say 0+$x;
say builtin::numify($x);

@leonerd leonerd marked this pull request as draft February 18, 2024 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants