niner / Inline-Perl5

Use Perl 5 code in a Raku program
Artistic License 2.0
94 stars 30 forks source link

.call does not find 'print' in main namespace #157

Closed stuart-little closed 3 years ago

stuart-little commented 3 years ago

I have the following script:

#!/usr/bin/env perl6
use v6;
use Inline::Perl5;

my $p5 = Inline::Perl5.new;
$p5.call('print', 'Hello World');

Running it errors out:

Undefined subroutine &main::print called.

  in method call at /root/repos/rakudo/install/share/perl6/site/sources/BCB33D644DD230F3DF84812EA6C53F24FE4BA414 (Inline::Perl5) line 424
  in block <unit> at /root/bin/p6.p6 line 6
niner commented 3 years ago

Perl builtin functions are not actually in the main namespace. Their proper full name is e.g. CORE::print. However, CORE is not really a package and CORE:: is merely a syntactic hint to the compiler. Furthermore, many builtin functions are not even available as Perl functions at all. You cannot e.g. call print with ampersand syntax: &CORE::print("Hello World"). This will fail with Undefined subroutine &CORE::print called while e.g. &CORE::hex("F") works just fine.

While there would probably be some elaborate way to get at these builtin C functions, I just don't think it'd be worth to support them. After all Raku's print function works just fine ;)

lizmat commented 3 years ago

It's been a while since I dabbled in Perl, but this does seem to give something:

$ perl -E 'say \&CORE::GLOBAL::print'
CODE(0x7fb946803418)
niner commented 3 years ago

But then, so does this:

> perl -E 'say \&CORE::NoIReallyDontExist::print'
CODE(0x561106d214d8)
lizmat commented 3 years ago

Ah, indeed, I was just remembering tricks like this:

$ perl -E 'BEGIN { *CORE::GLOBAL::bless = sub { say "foo" } }; bless 42'
foo

But that doesn't work for print.

stuart-little commented 3 years ago

Fair enough, but the documentation tripped me up. Have a look at the second code display here: it's the code that failed for me..

use Inline::Perl5;
my $p5 = Inline::Perl5.new;
$p5.call('print', 'Hello World');

If this is expected to fail, then surely the documentation, at least, needs fixing?