在bin /中创建包含Perl 5实用程序脚本的Perl 6模块

Chr*_*oms 5 perl6

Perl 5模块分发中的Perl 6脚本

我可以在Perl 5模块分发中包含Perl 6脚本:

# Create a new module
dzil new My::Dist
cd My-Dist/

# Add necessary boilerplate
echo '# ABSTRACT: boilerplate' >> lib/My/Dist.pm

# Create Perl 6 script in bin directory
mkdir bin
echo '#!/usr/bin/env perl6' > bin/hello.p6
echo 'put "Hello world!";' >> bin/hello.p6

# Install module
dzil install

# Test script
hello.p6
    # Hello world!

# See that it is actually installed
which hello.p6
    # ~/perl5/perlbrew/perls/perl-5.20.1/bin/hello.p6
Run Code Online (Sandbox Code Playgroud)

Perl 6模块分发中的Perl 5脚本

但是,我很难在Perl 6发行版中包含Perl 5脚本.

在模块目录中是一个META6.json文件和一个名为的子目录bin.In bin是一个名为的Perl 5文件hello.pl.

zef install .在顶级目录中运行没有错误.但是当试图运行时hello.pl,我收到一个错误.来发现,已经安装了Perl 6包装脚本hello.pl,这就是给我错误的原因.如果我hello.pl直接运行原件,它工作正常.

META6.json

{
"perl"         : "6.c",
"name"         : "TESTING1234",
"license"      : "Artistic-2.0",
"version"      : "0.0.2",
"auth"         : "github:author",
"authors"      : ["First Last"],
"description"  : "TESTING module creation",
"provides"     : {
                 },
"depends"      : [ ],
"test-depends" : [ "Test", "Test::META"  ]
}
Run Code Online (Sandbox Code Playgroud)

bin/hello.pl

#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;

say 'Hello world!';
Run Code Online (Sandbox Code Playgroud)

这安装没有错误,但是当我尝试运行时hello.pl,我收到以下错误:

===SORRY!===
Could not find Perl5 at line 2 in:
    /home/username/.perl6
    /path/to/perl6/rakudo-star-2017.07/install/share/perl6/site
    /path/to/perl6/rakudo-star-2017.07/install/share/perl6/vendor
    /path/to/perl6/rakudo-star-2017.07/install/share/perl6
    CompUnit::Repository::AbsolutePath<64730416>
    CompUnit::Repository::NQP<43359856>
    CompUnit::Repository::Perl5<43359896>
Run Code Online (Sandbox Code Playgroud)

which hello.pl从命令行表明它已安装在/path/to/perl6/rakudo-star-2017.07/install/share/perl6/site/bin/hello.pl.该文件实际上是以下代码:

/path/to/perl6/rakudo-star-2017.07/install/share/perl6/site/bin/hello.pl

#!/usr/bin/env perl6
sub MAIN(:$name is copy, :$auth, :$ver, *@, *%) {
    CompUnit::RepositoryRegistry.run-script("hello.pl", :dist-name<TESTING1234>, :$name, :$auth, :$ver);
}
Run Code Online (Sandbox Code Playgroud)

我提交了一份Rakudo错误报告(https://rt.perl.org/Ticket/Display.html?id=131911),但我并不完全相信没有简单的解决方法.

Chr*_*oms 3

作为示例,我cat在 Perl 5 中创建了一个简单的替换,并创建了一个“包裹”它的 Perl 6 模块(如果您想下载代码并亲自尝试,请参阅GitHub 存储库)。

以下是相关文件的副本。创建这些文件后,运行zef install .Rakudo Star 2017.07 安装即可正常安装。这会run_cat在您的 Rakudobin目录中安装一个可执行文件。

看起来秘密就是制作一个 Perl 6 模块文件来包装 Perl 5 脚本和相应的 Perl 6 脚本来使用 Perl 6 模块。


Perl 5 脚本

resources/scripts/cat.pl

#!/bin/env perl
use v5.10;
use strict;
use warnings;

while(<>) {
    print;
}
Run Code Online (Sandbox Code Playgroud)

包装脚本

模块:lib/catenate.pm6

unit module catenate;

sub cat ($filename) is export {
    run('perl',%?RESOURCES<scripts/cat.pl>,$filename);
}
Run Code Online (Sandbox Code Playgroud)

可执行文件:bin/run_cat

#!/bin/env perl6
use catenate;

sub MAIN ($filename) {
    cat($filename);
}
Run Code Online (Sandbox Code Playgroud)

样板和测试

META6.json`

{
"perl"         : "6.c",
"name"         : "cat",
"license"      : "Artistic-2.0",
"version"      : "0.0.9",
"auth"         : "github:author",
"authors"      : ["First Last"],
"description"  : "file catenation utility",
"provides"     : { "catenate" : "lib/catenate.pm6" },
"test-depends" : [ "Test", "Test::META"  ],
"resources"    : [ "scripts/cat.pl" ]
}
Run Code Online (Sandbox Code Playgroud)

t/cat.t

#!/bin/env perl6
use Test;

constant GREETING = 'Hello world!';
my $filename = 'test.txt';
spurt($filename, GREETING);

my $p5 = qqx{ resources/scripts/cat.pl $filename };
my $p6 = qqx{ bin/run_cat              $filename };

is $p6, $p5, 'wrapped script gives same result as original';

is $p6, GREETING, "output is '{GREETING}' as expected";

unlink $filename;

done-testing;
Run Code Online (Sandbox Code Playgroud)

感谢@moritz 和@ugexe 让我指明了正确的方向!