我有一个JSON字符串,例如
use JSON::XS qw(decode_json);
say Dumper( decode_json($json) );
Run Code Online (Sandbox Code Playgroud)
将产生:
$VAR1 = {
'Fname' => 'SomeFname',
'Lname' => 'SomeLname',
'Addr' => {
'Street => 'Somestreet',
'Zip' => '00000',
},
};
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种简单的方法,如何将JSON字符串(或perl结构)"转换"为Perl/Moose对象,如:
package My;
use Moose;
has 'Fname' => (is => 'rw', isa => 'Str');
has 'Lname' => (is => 'rw', isa => 'Str');
has 'Addr' => (is => 'rw', isa => 'My::Addr');
Run Code Online (Sandbox Code Playgroud)
和
package My::Addr;
use Moose;
has 'Street' => (is => 'rw', isa => 'Str');
has 'Zip' => (is => …Run Code Online (Sandbox Code Playgroud) 我经常需要运行一些 Perl 语句来快速进行数据操作,例如
some_command | perl -lne 'print if /abc/'
Run Code Online (Sandbox Code Playgroud)
从管道读取,我不需要围绕命令 arg 文件名进行循环。我怎样才能实现下一个?
some_command | perl -lne 'print if /$ARGV[0]/' abc
Run Code Online (Sandbox Code Playgroud)
这给出了错误:
Can't open abc: No such file or directory.
Run Code Online (Sandbox Code Playgroud)
我知道“-n”的作用是
while(<>) {.... }
Run Code Online (Sandbox Code Playgroud)
在我的程序周围,<>将args作为文件名,但每次都执行下一个有点不切实际
#/bin/sh
while read line
do
some_command | perl -lne 'BEGIN{$val=shift @ARGV} print if /$val/' "$line"
done
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法可以进入 Perl ONE-LINER 命令行参数的“内部”而不将它们解释为文件名?
我有这个:
use Plack::Builder;
my $config_app = sub {...};
my $app = sub {...}
builder {
mount "/admin" => $config_app;
mount "/" => $app;
};
Run Code Online (Sandbox Code Playgroud)
将$config_app配置值保存到文件中app.cfg并将其$app加载为config-file.不需要在每个请求中读取配置文件.需要在应用程序开始时阅读它,并在更改时重新读取它.
实现这一目标的最佳方式是什么?
我唯一的想法是:应用程序将记住最后一个config_read_time,并在每个请求中将检查修改时间app.cfg.如果文件被修改,将重新读取它.
这里有更好的解决方案吗?(表示$ config_app和$ app之间的一些消息传递,例如当$ config_app保存新配置时will send some message to $app: re-read the config.
需要每5秒运行一个子程序,但需要在系统时钟标记处测量.所以,需要开始于0,5,10,15 .... 45,50%,55其次,它每分钟(确切地,与0.1秒precistion).
就像是:
for(;;) {
do_sleep(); #time need to sleep to the next 5 second mark
run_this();
}
Run Code Online (Sandbox Code Playgroud)
所述run_this子可快可慢(其0.2之间运行时- 120秒).当它运行超过5秒时 - 无论其运行时间如何,下一次运行必须精确到5秒.
例如,当 run_this
问题是:如何编写do_sleep?
寻找通用方法如何按修改时间对文件的随机列表进行排序,如下所示:
./make_list_of_files | some_sorter_by_mtime
Run Code Online (Sandbox Code Playgroud)
我现在的解决方案是(这make_list_of_files是find命令):
find / -type f -print |\
perl -nle 'push @in,$_;END {@out = sort{ (stat($a))[9] <=> (stat($b))[9] } @in; $,="\n";print @out}'
Run Code Online (Sandbox Code Playgroud)
存在一些更简单的解决方案(例如没有perl)?
在我的上一个问题中 @Borodin评论了我的问题:
您应该首先删除Modern :: Perl和namespace :: sweep.应该避免使用行为为pragma的模块.
我有点困惑,因为:
namespace::autoclean.使用namespace :: autoclean位只是很好的代码卫生,因为它会在包的编译周期结束时从类的命名空间中删除导入的符号,包括Moose关键字.一旦构建了类,就不需要这些关键字.(最好不要在包装的末尾放置Moose).
在书中Intermediate perl建议使用namespace::autoclean也.
是的,我用的,而不是autoclean在sweep因为一次-模块从doccu
编写这个pragma是为了解决优秀的namespace :: autoclean的一些问题.特别是,namespace :: autoclean将删除由重载安装的特殊符号,因此您不能对重载Perl运算符的对象使用namespace :: autoclean.
......
在大多数情况下,namespace :: sweep应该作为namespace :: autoclean的替代品.发布后,除了自己的以外,该pragma还会传递所有namespace :: autoclean的测试.
而且因为我是一个初学者,我真的很困惑.对我来说,当我阅读:这个模块解决了另一个模块的一些问题 - 意思是:使用这个.
'手册(从我应该学习的地方)说"使用它"和stackoverflow teling的专家:不要使用它.
所以,请有人解释我:
namespace::sweep或我应该使用namespace::autoclean或不使用它们?对于'ModernPerl'.当然,我可能不太了解并且"确切地"知道是什么.我所知道的,(再次来自它的doccu)
这将启用严格和警告编译指示,以及Perl 5.10中提供的所有功能.它还启用了perldoc mro中记录的C3方法解析顺序,并加载IO :: File和IO :: Handle,以便您可以在文件句柄上调用方法.将来,它可能包括其他核心模块和编译指示.
当然,不要深刻理解mro,只能认为它是多重遗传情况下"死亡致命钻石"问题的答案.
到目前为止,我对它非常满意,因为它为我提供了所需的pragma:
use strict;
use warnings;
use feature …Run Code Online (Sandbox Code Playgroud) 我试图决定是否可以在我的项目中使用angular.js(和anguar-ui-bootstrap).
Web应用程序应该可以与任何合理的现代浏览器一起使用,我很惊讶在angular-ui-bootstrap站点上的权限,在点击"创建构建"按钮后服务器响应:
您当前的浏览器不支持创建自定义构建.请花一点时间升级到更现代的浏览器(Safari除外).
链接到http://browsehappy.com,其中列出了我的Safari(v.8),但angular-ui-bootstrap明确排除了它.
在angular.js的FAQ中是:
AngularJS是100%JavaScript,100%客户端,兼容桌面和移动浏览器.
引导程序3本身也是兼容的.
由于我是angular.js的完全新手,只是下载并尝试学习它,但老实说,我不想浪费几天/几周学习发现:它不完全支持Safari.所有合理的现代浏览器的支持对项目至关重要.
因此问题是:
compatibility cross-browser angularjs angular-ui angular-ui-bootstrap
寻找一种方法,如何从另一个列表定义的顺序中获取哈希值.
"演示"代码(实际值不同):
use 5.014;
use warnings;
my $href = {
'Long one' => 'v1',
'xxx two' => 'v2',
'another3' => 'v3',
'xfour' => 'v4',
'some5' => undef,
};
#keys from the $href in defined order
my @order = ('Long one', 'xfour', 'nono', 'another3', 'some5', 'xxx two');
#in the real code:
#my $href = some_sub(......); my @order = another_sub(....);
#cleanup the @order form undefined values
@order = grep { exists $href->{$_} && defined $href->{$_} } @order;
#my input
while(<DATA>) {
chomp; …Run Code Online (Sandbox Code Playgroud) 以下产生我想要的东西.
#!/usr/bin/env perl
use 5.020;
use warnings;
use Data::Dumper;
sub command {
<DATA>
#in the reality instead of the DATA I have
#qx(some weird shell command what produces output like in the DATA);
}
my @lines = grep { !/^\s*$/ } command();
chomp @lines;
my $data;
#how to write the following nicer - more compact, elegant, etc.. ;)
for my $line (@lines) {
my @arr = split /:/, $line;
$data->{$arr[0]}->{text} = $arr[1];
$data->{$arr[0]}->{par} = $arr[2];
$data->{$arr[0]}->{val} = $arr[3];
}
say …Run Code Online (Sandbox Code Playgroud) 想要准备一个字符串,perl以便在其中进行评估bash:
str="$( perl -E '$sq=chr(047);say qq{var1=${sq}hello world${sq}}' )" #this just for demo - the real perl scirpt is ofcourse real complex script ...
echo "got for eval: [[$str]]"
eval "$str"
echo "var1 : [[$var1]]"
Run Code Online (Sandbox Code Playgroud)
它打印:
got for eval: [[var1='hello world']]
var1 : [[hello world]]
Run Code Online (Sandbox Code Playgroud)
或者当然eval是危险的,所以perl脚本需要做一些准备.
有一点是,用'值替换值中的所有(单引号)'"'"',因此分配don't do this到var它中的时间应打印为
var='don'"'"'t do this' #safe for eval.
Run Code Online (Sandbox Code Playgroud)
在这里一些considartions我应该在perl脚本遵循准备变量assingnment( var='some content')字符串安全 eval?
来自我的真实剧本的片段:
#!/usr/bin/env perl
use strict; …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过-s和设置perl的变量-var=value.
perl -s -E 'say $xx if $xx' -xx=abc
Run Code Online (Sandbox Code Playgroud)
版画
No Perl script found in input
Run Code Online (Sandbox Code Playgroud)
它可能不能-E和我一起做错了.(perlrun没有提及错误的组合-E和-s)
有这样的XML文件 - t.xml
<?xml version="1.0"?>
<ArrayOfFiles xmlns="Our.Files" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<File>
<DownloadCount>1</DownloadCount>
<Id>11</Id>
</File>
<File>
<DownloadCount>2</DownloadCount>
<Id>22</Id>
</File>
</ArrayOfFiles>
Run Code Online (Sandbox Code Playgroud)
该xmlns声明是无效的,在xmlstarlet抱怨它,例如使用:
xmlstarlet sel -t -v "//File/Id" t.xml
Run Code Online (Sandbox Code Playgroud)
版画
t.xml:2.32: xmlns: URI Our.Files is not absolute
<ArrayOfFiles xmlns="Our.Files" xmlns:i="http://www.w3.org/2001/XMLSchema-instan
Run Code Online (Sandbox Code Playgroud)
可能出于同样的原因,我也无法使用以下perl代码:
use 5.014;
use warnings;
use XML::LibXML;
my $dom = XML::LibXML->new->parse_file('t.xml');
my $res = $dom->findnodes('//File/Id');
say $_->textContent for $res->get_nodelist;
Run Code Online (Sandbox Code Playgroud)
当我省略xmlns声明时,例如尝试解析这个修改过的XML文件
<?xml version="1.0"?>
<ArrayOfFiles>
<File>
<DownloadCount>1</DownloadCount>
<Id>11</Id>
</File>
<File>
<DownloadCount>2</DownloadCount>
<Id>22</Id>
</File>
</ArrayOfFiles>
Run Code Online (Sandbox Code Playgroud)
上面的代码DWIM - 并打印:
11
22
Run Code Online (Sandbox Code Playgroud)
问题是,如何解析原始XML文件,因为它是从外部站点下载的 …
扩展我的基本正则表达式知识,有些事情对我来说不清楚.
如果\b匹配词边界具有相同的意思的下两个正则表达式 - 例如将匹配相同的字符串?
/\bword\b/
/(^|\W)word(\W|$)/m #when multi-line is turned on
/(\A|\W)word(\W|\z)/
Run Code Online (Sandbox Code Playgroud)
问,因为\b手段词边界.这个词是\w+,所以\b必须是什么不是\w,例如它必须是\W或字符串或行的开头或结尾.(或没有?)(不计算捕获组,可能会更好地使用一些非捕获外观 - 某处).
那两个?
/word\B/
/word\w/
Run Code Online (Sandbox Code Playgroud)
如果单词最后必须是"非字边界",则表示该单词必须后跟\w(单词)字符.(或没有?)