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
base: blead
Are you sure you want to change the base?
Add builtin::numify
#21982
Conversation
SV *ssv = *PL_stack_sp; | ||
if(SvNOK(ssv)) |
There was a problem hiding this comment.
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
else | ||
sv_setiv(TARG, SvIV_nomg(ssv)); |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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);
Adds a new builtin function,
builtin::numify
, the number equivalent ofbuiltin::stringify
.While here also remembers to add a test and documentation on how
stringify
handlesundef
. No behavioural change to stringify itself.