Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Pass =finish POD data to Perl 5 as DATA handle
use Foo:from<Perl5>;
=finish
This data can be read by Foo by slurping <DATA>
  • Loading branch information
niner committed Feb 5, 2017
1 parent e307c35 commit f461fab
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/Inline/Perl5.pm6
Expand Up @@ -1081,6 +1081,12 @@ method init_callbacks {
($p6) = @_;
}
sub init_data {
my ($data) = @_;
no strict;
open *{main::DATA}, '<', \$data;
}
sub uninit {
undef $p6;
}
Expand Down Expand Up @@ -1451,6 +1457,10 @@ class X::Inline::Perl5::NoMultiplicity is Exception {
}
}

method init_data($data) {
self.call('v6::init_data', $data);
}

method BUILD(*%args) {
my &call_method = sub (Int $index, Str $name, Int $context, Pointer $args, Pointer $err) returns Pointer {
my $p6obj = $objects.get($index);
Expand Down Expand Up @@ -1510,6 +1520,13 @@ method BUILD(*%args) {
}
}

if ($*W) {
use QAST:from<NQP>;
my $block := { self.init_data(CALLER::<$=finish>) if CALLER::<$=finish> };
$*W.add_object($block);
my $op := $*W.add_phaser(Mu, 'INIT', $block, class :: { method cuid { (^2**128).pick }});
}

$!external_p5 = %args<p5>:exists;
if $!external_p5 {
$!p5 = %args<p5>;
Expand Down
13 changes: 13 additions & 0 deletions t/data.t
@@ -0,0 +1,13 @@
use Test;
#
# DATA handle is only initialized if Inline::Perl5 is loaded at compile time of a compilation unit
use Data::Dumper:from<Perl5>;

is EVAL('<DATA>', :lang<Perl5>), "trailing data found in DATA handle\n";

done-testing;

=finish
trailing data found in DATA handle

# vim: ft=perl6
7 changes: 7 additions & 0 deletions t/data_in_module.t
@@ -0,0 +1,7 @@
use lib 't/lib';
use Test;
use Data;

is look_for_data(), "trailing data found in DATA handle\n";

done-testing;
11 changes: 11 additions & 0 deletions t/lib/Data.pm
@@ -0,0 +1,11 @@
use lib:from<Perl5> 't/lib';
use LookForData:from<Perl5>;

sub look_for_data() is export {
return LookForData::return_data;
}

=finish
trailing data found in DATA handle

# vim: ft=perl6
10 changes: 10 additions & 0 deletions t/lib/LookForData.pm
@@ -0,0 +1,10 @@
package LookForData;
use strict;
use warnings;

sub return_data {
my $handle = do { no strict 'refs'; \*{"main::DATA"} };
return scalar <$handle>;
}

1;

0 comments on commit f461fab

Please sign in to comment.