如何检查不仅仅是一个-a或-b或-c定义?
所以不在一起-a -b,也不-a -c,也不-b -c,也不-a -b -c。
现在有
use strict;
use warnings;
use Carp;
use Getopt::Std;
our($opt_a, $opt_b, $opt_c);
getopts("abc");
croak("Options -a -b -c are mutually exclusive")
if ( is_here_multiple($opt_a, $opt_c, $opt_c) );
sub is_here_multiple {
my $x = 0;
foreach my $arg (@_) { $x++ if (defined($arg) || $arg); }
return $x > 1 ? 1 : 0;
}
Run Code Online (Sandbox Code Playgroud)
以上是有效的,但不是很优雅。
这里已经有类似的问题,但这是不同的,因为检查两个 …
有这个:
use utf8;
package ÁngryBird; #note the Á in the package name
Run Code Online (Sandbox Code Playgroud)
perl -c结果是syntax OK.
use utf8;
package ÁngryMoose;
use Moose;
Run Code Online (Sandbox Code Playgroud)
perl -c 说,
ÁngryMoose is not a module name at
/opt/local/lib/perl5/site_perl/5.12.4/darwin-multi-2level/Class/MOP/Package.pm
line 209.
Run Code Online (Sandbox Code Playgroud)
那么,代码中有什么问题?
我输入的文件结构如下:
a1
b1
c1
c2
c3
b2
c1
d1
d2
b3
b4
a2
a3
b1
b2
c1
c2
Run Code Online (Sandbox Code Playgroud)
每个级别缩进2个空格.所需的输出是:
a1/b1/c1
a1/b1/c2
a1/b1/c3
a1/b2/c1/d1
a1/b2/c1/d2
a1/b3
a1/b4
a2
a3/b1
a3/b2/c1
a3/b2/c2
Run Code Online (Sandbox Code Playgroud)
它就像一个文件系统,如果下一行有更大的缩进,当前的那个就像一个"目录",当有相同的缩进时,它就像一个"文件".需要打印"文件"的完整路径.
尝试在没有任何高级语言的情况下解决这个问题,例如python,perl只使用基本的bash命令.
我当前的代码/想法是基于递归函数调用和使用堆栈,但是"逻辑"有问题.代码目前输出下一个:
a1 b1 c1
a1 b1
a1
DD: line 8: [0-1]: bad array subscript
Run Code Online (Sandbox Code Playgroud)
只有第一行是正确的 - 所以处理递归是错误的...
input="ifile.tree"
#stack array
declare -a stack
#stack manipulation
pushstack() { stack+=("$1"); }
popstack() { unset stack[${#stack[@]}-1]; }
printstack() { echo "${stack[*]}"; }
#recursive function
checkline() {
local …Run Code Online (Sandbox Code Playgroud) Plack :: Builder的概要以及这个答案说:
# in .psgi
use Plack::Builder;
my $app = sub { ... };
builder {
mount "/foo" => builder {
enable "Foo";
$app;
};
mount "/bar" => $app2;
mount "http://example.com/" => builder { $app3 };
};
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
use Plack::Builder;
my $app1 = sub { return [200, ['Content-Type' => 'text/plain'], [ "Hello 1"] ]; };
my $app2 = sub { return [200, ['Content-Type' => 'text/plain'], [ "Hello 2"] ]; };
my $app3 = sub { return …Run Code Online (Sandbox Code Playgroud) 我刚才看到这个问题,大约在Perl优化特定的正则表达式。我想知道我的机器可以进行多少次匹配,因此我尝试了以下简单基准测试:
qr/regex/匹配use 5.014;
use warnings;
use Benchmark qw(:all);
my $str = "SDZ";
my $qr = qr/S?T?K?P?W?H?R?A?O?\*?E?U?F?R?P?B?L?G?T?S?D?Z?/;
say "match [$&]" if( $str =~ $qr );
my $res = timethese(-10, {
stdrx => sub { $str =~ /S?T?K?P?W?H?R?A?O?\*?E?U?F?R?P?B?L?G?T?S?D?Z?/ },
qr_rx => sub { $str =~ $qr },
});
cmpthese $res;
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,结果如下:
use 5.014;
use warnings;
use Benchmark qw(:all);
my $str = "SDZ";
my $qr = qr/S?T?K?P?W?H?R?A?O?\*?E?U?F?R?P?B?L?G?T?S?D?Z?/;
say "match [$&]" if( $str =~ $qr );
my …Run Code Online (Sandbox Code Playgroud) 假设有一个genpairs生成以null结尾的字符串的命令.
key1 \0 val1 \0 key2 \0 val2 \0
Run Code Online (Sandbox Code Playgroud)
想要将上面的输入成对读入bash变量.以下不适合我:
genpairs() { #for the demo
printf "%d\0x\0" 1
printf "%d\0y\0" 2
printf "%d\0z\0" 3
}
#the above generates 1 \0 x \0 2 \0 y \0 3 \0 z \0 etc...
while IFS= read -r -d '' key val; do
echo "key:[$key] val:[$val]"
done < <(genpairs)
Run Code Online (Sandbox Code Playgroud)
版画
key:[1] val:[]
key:[x] val:[]
key:[2] val:[]
key:[y] val:[]
key:[3] val:[]
key:[z] val:[]
Run Code Online (Sandbox Code Playgroud)
例如,读取有点不会$'\0'分成两个变量.
想要的输出:
key:[1] val:[x]
key:[2] val:[y]
key:[3] …Run Code Online (Sandbox Code Playgroud) 更容易显示为试图用文字描述.
find . -name jo\* -print > list
cat list
#./jo1
#./jo2
#./jo3
# the "file" by reading the list of files from the file "list"
file -f list
#./jo1: ASCII text
#./jo2: ASCII text
#./jo3: ASCII text
#now with process substitution
file -f <(find . -name jo\* -print)
Run Code Online (Sandbox Code Playgroud)
没有输出..;(
#repeat with -x
set -x
file -f <(find . -name jo\* -print)
set +x
#shows
+ file -f /dev/fd/63
++ find . -name 'jo*' -print
+ set +x
Run Code Online (Sandbox Code Playgroud)
所以,它应该 …
有这个bash脚本:
future="${1:-Dec 08 2017 22:00:00}"
t1=$(date -j -f "%b %d %Y %H:%M:%S" "$future" +%s) #using OS X
t0=$(date +%s)
echo "Current: $(date)"
echo "Future : $future"
echo "Diff : $(( $t1 - $t0 )) secs"
Run Code Online (Sandbox Code Playgroud)
它打印:
Current: pi 8. december 2017 21:25:25 CET
Future : Dec 08 2017 22:00:00
Diff : 2075 secs
Run Code Online (Sandbox Code Playgroud)
结果(差异)是正确的.
现在尝试使用perl做同样的事情:
use strict;
use warnings;
use feature 'say';
use Time::Piece;
my $format = '%b %d %Y %H:%M:%S';
my $future = shift // 'Dec 08 …Run Code Online (Sandbox Code Playgroud) 试图了解麋:
use Modern::Perl;
package FOO {
use Moose;
sub rep { say " <report></report>"; }
sub doc {
say "<document>";
inner();
say "</document>";
}
}
package BAR {
use Moose;
extends 'FOO';
around 'rep' => sub {
my $orig = shift;
my $self = shift;
say "<document>";
$self->$orig(@_);
say "</document>";
};
augment 'doc' => sub {
say " <report></report>";
};
}
package main {
BAR->new->rep;
say "===";
BAR->new->doc;
}
Run Code Online (Sandbox Code Playgroud)
产生...
<document>
<report></report>
</document>
===
<document>
<report></report>
</document>
Run Code Online (Sandbox Code Playgroud)
...相同的结果。在设计“模型(对象层次结构)”时-基于我应该决定何时使用around …
如何搜索文件ack以查找包含所有 (或任何)已定义模式的行?
在任何一个(OR)是很容易,比如:
ack 'pattern1|pattern2|pattern3'
Run Code Online (Sandbox Code Playgroud)
但如何写AND(ALL)?例如,如何写以下内容:
if( $line =~ /pattern1/ && $line =~ /pattern2/ && $line =~ /pattern3/ ) {
say $line
}
Run Code Online (Sandbox Code Playgroud)
用ack?
或者更确切地说,有可能创建一个正则表达式logical and?