Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

[perl6] More Inline::Perl5 musings

by syphilis (Archbishop)
on Mar 17, 2016 at 11:59 UTC ( [id://1158072]=perlquestion: print w/replies, xml ) Need Help??

syphilis has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Inline::Perl5 is probably not the recommended entry point to perl6.
But it's, so far, the only aspect of perl6 I've come across that has piqued my interest in perl6 - and it looks like it's going to become *my* entry point (for that given reason).
Nevertheless, I'm happy to apologise for this unorthodoxy on my part if that's deemed appropriate.
(Blame Doug Hunt on the PDL mailing list for having mentioned Inline::Perl5 on the PDL-dev mailing list ;-)

So ... I have a perl5 module named Math::Float128. Basically, it does __float128 arithmetic, and it builds fine on most platforms if you use a recent port of the gcc compiler.
As an exercise, I thought it would be interesting (for me) to access that module and its functionality from perl6.
On Ubuntu 14.04 I have:
sisyphus@sisyphus5-desktop:~/p6$ perl6 -v This is Rakudo version 2015.12 built on MoarVM version 2015.12 implementing Perl 6.c. sisyphus@sisyphus5-desktop:~/p6$ perl -v This is perl 5, version 22, subversion 0 (v5.22.0) built for x86_64-li +nux ....
And, after muddling my way through the installation of Inline::Perl5 (with the assistance of a number of patient monks), I've come up with this perl6 script (which works fine):
use Inline::Perl5; my $p5 = Inline::Perl5.new; $p5.use('Math::Float128'); my $f128 = $p5.invoke('Math::Float128', 'new', '1.2345'); say "$f128"; $f128.print; say ''; # insert the newline say "ok";
That correctly outputs:
sisyphus@sisyphus5-desktop:~/p6$ perl6 use.pl 1.23450000000000000000000000000000009e+00 1.23450000000000000000000000000000009e+00 ok
Based on that it seems to me that the perl5 overloading of "" is being correctly accessed - as that's the only way I can think of that both

say "$f128";
and
$f128.print;

would render correctly.

Is that assessment correct ? (That's my first question).

Inspired by the success of

$f128.print;
I tried
$f128.say;

But that just outputs:
Inline::Perl5::Perl5Object.new(ptr => NativeCall::Types::Pointer.new(1 +00938200), perl5 => Inline::Perl5.new)
Why ? (That's my second question).

Is there a better way to access __float128 arithmetic using perl6 ? (My third and last question ... for tonight ;-)

Cheers,
Rob

Replies are listed 'Best First'.
Re: [perl6] More Inline::Perl5 musings
by Corion (Patriarch) on Mar 17, 2016 at 12:08 UTC

    I think that in Perl 6, .print outputs the stringification, while .say outputs the gist of a value, which is something more suitable for debugging/inspecting, as it returns something informative for the structure.

    Please note that this is a factoid I think I picked up during one of the Perl 6 talks. I have not used it myself and have very little active experience in running Perl 6 code.

    I find your approach of using Inline::Perl5 very interesting and think it's a good bridge to start moving a task that exists as a program in Perl 5 partially over to Perl 6 while not being hampered by the lack of Perl 6 modules or unknown APIs.

Re: [perl6] More Inline::Perl5 musings
by Tux (Canon) on Mar 17, 2016 at 13:25 UTC

    say is not the way to debug in perl6 as it is in perl5, and Num values are native in perl6, so Math::Float128 is a module that is nut useful to link to perl6 with Inline::Perl5:

    $ perl6 -e'my $f = 1.2345; $f.say; $f.perl.say; $f.gist.say; $f.nude.say; dd $f'
    1.2345
    1.2345
    1.2345
    (2469 2000)
    Rat $f = 1.2345
    $ perl6 -e'my $f = 1 / 3.33; $f.say; $f.perl.say; $f.gist.say; $f.nude.say; dd $f'
    0.300300
    <100/333>
    0.300300
    (100 333)
    Rat $f = <100/333>
    $ perl6 -e'say π ** 25'
    2683779414317.76
    $ perl6 -e'.say for π ** 259'
    5.77852541576782e+128
    

    Enjoy, Have FUN! H.Merijn
Re: [perl6] More Inline::Perl5 musings
by duelafn (Parson) on Mar 17, 2016 at 16:28 UTC

    The Perl6 Spec allows for native num128, but MoarVM seems to only support native 32 and 64-bit nums (It's unclear to me how to actually reach the "long double" case). Eventually one should be able to do this:

    # Not yet: $ perl6 -e 'my num128 $one = Num(1); $one += 1e-30; say $one == 1' False

    I have no idea or intuition for how hard it might be to get num128 to work in MoarVM, but apparently num128 isn't on the roadmap for the near future.

    In response to Tux (who probably already knows this), Perl6 Rats are great for general-purpose computing (and completely awesome in financial software), but only higher precision will do for numerical simulation. Until we have num128, there is value to Math::Float128:

    $ perl6 -e 'my $one = 1; $one += 1e-16; say $one == 1' True $ perl6 -e 'say pi ** 621' Inf $ perl6 -e 'say sin(pi/2).WHAT' (Num) $ perl6 -e 'my $val = sin(pi/2) + 0.1; my $eleven = 0; $eleven += $val + for 1..10; say $eleven == 11' False

    Good Day,
        Dean

      Thanks Corion, Tux, duelafn.
      Not much to add except that the level of support for perl5 extensions seems pretty good.

      In general, I haven't yet found a way to access overloaded operators directly.
      Instead I'm calling the overloading subroutines by name - eg:
      use Inline::Perl5; my $p5 = Inline::Perl5.new; $p5.use('Math::Float128'); my $f1 = $p5.invoke('Math::Float128', 'new', '2.5'); my $f2 = $p5.invoke('Math::Float128', 'new', '3.0'); my $swap = 0; # Set to true if order of # operands is to be swapped # Set $f3 to $f1 divided by $f2 my $f3 = $f1._overload_div($f2, $swap); say "# $f3"; # Outputs, as expected: # 8.33333333333333333333333333333333365e-01
      I was hoping to be able to do something like:
      my $f3 = $f1 / $f2;
      but that doesn't work, so I have to resort to calling Math::Float128::_overload_div() by name.

      That's no big deal, but it still puzzles me that say "# $f3"; apparently manages to access the Math::Float128 overloading of "" without any need for me to call Math::Float128::_overload_string() by name.
      Yet for all other overloaded operators I'm having to call the relevant subroutine.
      The penny will drop when it's ready to fall ;-)

      I'm also wondering whether Inline::Perl5 will work on MS Windows. Any experience of that here ?
      I think it should be ok:
      C:\>perl -V:useshrplib useshrplib='true';

      At some stage I'll grab a Windows version of perl6 ... and find out ...

      Cheers,
      Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1158072]
Approved by jellisii2
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found