当我尝试在Mac OS X v10.9(Mavericks)和MacPorts上安装perlbrew时,会发生以下情况:
~$ curl -L http://install.perlbrew.pl | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 315 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 1020 100 1020 0 0 649 0 0:00:01 0:00:01 --:--:-- 3217
## Download the latest perlbrew
## Installing perlbrew
perlbrew is installed: ~/perl5/perlbrew/bin/perlbrew
perlbrew root (~/perl5/perlbrew) is initialized.
Append the following piece of …Run Code Online (Sandbox Code Playgroud) 重新提出的问题 - 抱歉,这有点长.
例如,有一个简单的包
package My;
use Moose;
use namespace::sweep;
sub cmd1 {1}
sub smd2 {2}
__PACKAGE__->meta->make_immutable;
1;
Run Code Online (Sandbox Code Playgroud)
我希望允许My其他方法扩展其他方法,例如
package My::Cmd3;
use Moose;
extends 'My';
sub cmd3 {3}
1;
Run Code Online (Sandbox Code Playgroud)
这允许使用"基础" My和My::Cmd3下一个方法:
use My::Cmd3;
my $obj = My::Cmd3->new();
say $obj->cmd1(); #from the base My
say $obj->cmd3(); #from the My::Cmd3;
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的.我不想use My::Cmd3;,(这里会有更多扩展包),我想要use My;.
使用角色是NICER,如:
package My;
use Moose;
with 'My::Cmd3';
sub cmd1 {1}
sub cmd2 {2}
__PACKAGE__->meta->make_immutable;
1;
package …Run Code Online (Sandbox Code Playgroud) 有一个下一个perl代码
use 5.012;
use warnings;
#make some random numbers
my @list = map { rand } 1..10;
say "print all 10 numbers";
say $_ for @list;
say "print only first 5";
my $n=0;
for (@list) {
say $_ if $n++ < 5;
}
Run Code Online (Sandbox Code Playgroud)
一些更紧凑的形式,用于打印任何阵列的第一个(最后一个)N个元素?
接下来是语法错误......
$n=0;
#say $_ if($n++ < 5) for @list;
Run Code Online (Sandbox Code Playgroud) 以下是对一些问题(特别是对于shell)的许多评论,这些问题基本上表示以下一个或多个:
$filaneme是目录而不是常规文件,这将失败,虽然我知道每个脚本都需要自己的测试环境,但这些是脚本应该免受攻击的常见问题.
因此,我的目的是编写一个脚本,它将创建一些带有"特制"文件名的目录层次结构,以便进行测试.
问题是:什么"特殊"文件名对这个测试有好处?
目前我有(脚本创建文件和目录):
- (比如命令参数)# (评论char)! (命令历史)| 炭(管)() 字符*和?(通配符)还有什么我不应该错过的想法吗?
如何决定 - 下一个代码片段的推荐方法是什么?
我有一个基于Moose的模块,其中一些数据很简单HashRef.
有可能写作 - 像Mooseish HashRef一样,如:
package Some;
has 'data' => (
isa => 'HashRef',
builder => '_build_href',
init_arg => undef,
lazy => 1,
);
sub _build-href {
my $href;
$href = { a=>'a', b=>'b'}; #some code what builds a href
return $href;
}
Run Code Online (Sandbox Code Playgroud)
VS
sub data {
my $href;
$href = { a=>'a', b=>'b'}; #some code what builds a href
return $href;
}
Run Code Online (Sandbox Code Playgroud)
有什么不同?我问,因为在打电话时:
my $obj = Some->new;
my $href = $obj->data;
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我得到一个正确的HashRef.那么什么时候建议使用Moose-ish has …
它持续了两个星期search.cpan.org多次下降.是的,我知道这里是一个metacpan,但是从网点到链接的zilion,search.cpan.org所以metacpan不是"唯一的"解决方案.
想要通过输入以下内容来制作本地重定向器/etc/hosts:
search.cpan.org 127.0.0.1
Run Code Online (Sandbox Code Playgroud)
并运行一个简单的PSGI(或Apache)服务器localhost:80应该做什么重定向.因此,"search.cpan.org"的请求将在"localhost"处理,并且应该返回有效的302响应并重定向到metacpan.org.
@Mpapec指出mcpan.org- 我正在寻找什么,所以现在enoudh将每个请求重定向到"mcpan.org".
编辑/etc/hosts完我之后,尝试了下一个apache配置:
<VirtualHost *:80>
ServerName search.cpan.org
RedirectPermanent / http://search.mcpan.org/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
但不起作用.能得到一些帮助会很高兴.或者另一种选择,一个简单的app.psgi脚本也会很好.
所以,问题是:
app.psgi的运行它与plackup -p 80相同的功能和一个offtopic问题:
search.cpan.org吗?如果没有为perl脚本提供文件名,则寻找使用DATA句柄的方法.
我对perl不是很熟练.
就像是:
use strict;
use warnings;
use autodie;
use diagnostics;
my $fd;
if( $ARGV[0] && -f $ARGV[0] ) {
open $fd, "<", $ARGV[0];
} else {
$fd = "DATA"; #what here?
}
process($fd);
close($fd); #closing the file - or the DATA handle too?
sub process {
my $handle = shift;
while(<$handle>) {
chomp;
print $_,"\n";
}
}
__DATA__
default content
Run Code Online (Sandbox Code Playgroud) 需要每天随机运行一次shell脚本.(所以每天00:00-23:59之间一次).
我知道sleep命令,cron也是,但是
cron还没有随机时间sleep解决方案 - 不是很好 - 我的想法是每隔午夜启动脚本并在脚本开始时随机休眠.这里的东西更优雅吗?
对不起有点长的代码,但想提供一个近乎有效的例子.
package MLQ::Jobs::Job {
use 5.016;
use Moose;
use warnings;
has 'name' => (is=>'rw', isa=>'Str');
has 'status' => (is=>'rw', isa=>'Str');
}
package MLQ::Jobs {
use 5.016;
use Moose;
use warnings;
#use MLQ::Jobs::Job;
has jobs => (
is => 'rw',
isa =>'ArrayRef[MLQ::Jobs::Job]', #jobs - an array of zero to many jobs
builder => '_scan_jobs', #initializing it when creating the object Jobs
);
sub _scan_jobs {
my $self = shift;
#test code
foreach my $i (1..10) {
my $j = MLQ::Jobs::Job->new();
$j->name("name …Run Code Online (Sandbox Code Playgroud) 场景:
My::Module和Path::Class... 创建...specific methodHashRef地方的values是字符串,所以像(非常简短):
package Some;
sub filename { file('/tmp/xxx') } #Path::Class the filename->stringify' returns the string value
sub myobj { My::Module->new(....) } #the myobj->stringvalue returns the string value
sub text { 'some text' }
sub data {
my $strhr;
$strhr->{$_} = $self->make_string $self->$_ for(qw(filename myobj text));
return $strhr;
}
sub make_string {
my($self, $val) = @_;
return $val->stringify if( VAL_IS_PATH::CLASS_OBJ ); #need help here
return …Run Code Online (Sandbox Code Playgroud) 想为一些外来的perl模块写一个简单的包装器.简化示例:
use 5.014;
use warnings;
#foreign package
package Some {
sub func {
my($x,$y) = @_;
return $x.$y;
}
};
#my own packages
package My {
#use Some ();
sub func { Some::func(@_); }
}
package main {
#use My;
say My::func("res","ult");
}
Run Code Online (Sandbox Code Playgroud)
这工作正常并打印result.
但现在我遇到了一个使用原型的模块,例如上面的内容如下:
package Some {
sub func($$) { # <-- prototype check
my($x,$y) = @_;
return $x.$y;
}
};
Run Code Online (Sandbox Code Playgroud)
当试图使用My包装器包时 - 它说:
Not enough arguments for Some::func at ppp line 16, near "@_)" …Run Code Online (Sandbox Code Playgroud) 我在perl doc中读到的perl标识符应该是word character.(不计算特殊的perl变量).
在网上某处我看到了这个示例脚本:
use 5.018;
use utf8;
my($ , $ , $ , $ ) = (1..4);
say "$ " if( $ + $ == $ );
Run Code Online (Sandbox Code Playgroud)
它没有任何错误.那么,perl允许一些"骨骼和箭头"字符作为变量名称?
在我的perl程序中需要
use strictures 1;
while(<>) {
chomp;
my($action, $v1, $v2) = map { s/XX/42/g; $_ } split /\s+/;
print "=$action=$v1=$v2=\n";
do_someting( $action, func1($v1), func2($v2) );
}
Run Code Online (Sandbox Code Playgroud)
问题是,当输入包含<3"字段"并且我想要确保所有不存在的字段都被定义并包含"DEFAULT".所以想要下一个:输入 - >分配输出到变量
"mk aXXb ac" -> "mk" "a42b" "ac"
"deXX ab" -> "de42" "ab" "DEFAULT"
Run Code Online (Sandbox Code Playgroud)
等等..
尝试了下一个,但不起作用.
my($action, $v1, $v2) = map { s/XX/42/g; $_ // "DEFAULT" } split /\s+/;
Run Code Online (Sandbox Code Playgroud)
这看起来比我误解了map和/或//操作员的工作方式.