加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 23:34:18 所属栏目:大数据 来源:网络整理
导读:Perl 5模块分发中的Perl 6脚本 我可以在Perl 5模块分发中包含Perl 6脚本: # Create a new moduledzil new My::Distcd My-Dist/# Add necessary boilerplateecho '# ABSTRACT: boilerplate' lib/My/Dist.pm# Create Perl 6 script in bin directorymkdir bin
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

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),但我并不完全相信没有简单的解决方法.

解决方法

作为一个例子,我在Perl 5中创建了一个简单的cat替换,并创建了一个“包裹”它的Perl 6模块(如果您想下载代码并自己尝试,请参阅 GitHub repository for it).

以下是相关文件的副本.创建这些文件后,运行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让我指出了正确的方向!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读