我可以在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
Perl 6模块分发中的Perl 5脚本
但是,我很难在Perl 6发行版中包含Perl 5脚本.
在模块目录中有一个Meta6.json文件和一个名为bin的子目录.在bin中是一个名为hello.pl的Perl 5文件.
zef安装.在顶级目录中运行没有错误.但是当试图运行hello.pl时,我收到一个错误.来发现,已经为hello.pl安装了一个Perl 6包装器脚本,这就是给我错误的原因.如果我直接运行原始的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" ] }
斌/ hello.pl
#!/usr/bin/env perl use v5.10; use strict; use warnings; say 'Hello world!';
这安装没有错误,但是当我尝试运行hello.pl时,我收到以下错误:
06003
来自命令行的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,:$ver); }
我提交了一份Rakudo错误报告(https://rt.perl.org/Ticket/Display.html?id=131911),但我并不完全相信没有简单的解决方法.
解决方法
以下是相关文件的副本.创建这些文件后,运行zef install.我的Rakudo Star 2017.07安装安装得很好.这将在Rakudo bin目录中安装run_cat可执行文件.
似乎秘密就是制作一个Perl 6模块文件来包装Perl 5脚本和相应的Perl 6脚本以使用Perl 6模块.
Perl 5脚本
资源/脚本/ cat.pl
#!/bin/env perl use v5.10; use strict; use warnings; while(<>) { print; }
包装脚本
module:lib / catenate.pm6
unit module catenate; sub cat ($filename) is export { run('perl',%?RESOURCES<scripts/cat.pl>,$filename); }
可执行文件:bin / run_cat
#!/bin/env perl6 use catenate; sub MAIN ($filename) { cat($filename); }
锅炉和测试
Meta6.json`
{ "perl" : "6.c","name" : "cat","version" : "0.0.9","description" : "file catenation utility","provides" : { "catenate" : "lib/catenate.pm6" },"Test::Meta" ],"resources" : [ "scripts/cat.pl" ] }
吨/ 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;
感谢@moritz和@ugexe让我指出了正确的方向!