#!/usr/bin/perl -w
use experimental ‘smartmatch’;
use 5.010;
my
v
e
r
b
o
s
e
=
verbose=
verbose=ENV{VERBOSE} // 1;
my $secret=int( 1 + rand 100);
print “don’t tell anyone, but the secret number is $secret.\n” if $verbose;
LOOP:{
print “please enter a guess from 1 to 100:”;
chomp(my $guess=);
my
f
o
u
n
d
i
t
=
0
;
g
i
v
e
n
(
found_it=0; given(
foundit=0;given(guess){
when(!/\A\d+\Z/){say “not a number!”}
when(
>
_>
>secret){say “too high!”}
when(
<
_<
<secret){say “too low!”}
default {say “just right!”;$found_it++}
}
last LOOP if KaTeX parse error: Expected 'EOF', got '}' at position 25: … redo LOOP; }̲ #在第一个when中,我们先…ENV{PATH});
#在Perl 5.10中引入了“定义或”操作符//,在发现左边已定义的值时进行短路,无论作弊的值是真是假。
#这些定义或(defined-or)操作符会跟那些逻辑或操作符一样,只是它们测试的是否已经定义,而不是布尔值。
#通过使用//操作符,我们在KaTeX parse error: Expected 'EOF', got '#' at position 25: …SE}未定义的时候设置它为1 #̲windows跑strawbe…found_it++这个必须得有,否则无法终止循环
#循环退出条件,$found_it为1的时候
#转一圈 没猜对 found_it就还是0 就继续循环
#!/usr/bin/perl -w
use experimental ‘smartmatch’;
use 5.010;
given($ARGV[0]){
when(not $_ % 3){say ‘Fizz’;continue}
when(not $_ % 5){say ‘Bin’;continue}
when(not $_ % 7){say ‘sausage’}
}
#given-when结构,根据输入的数字,如果它能被3整除,就打印“Fizz”
#如果它能被5整除,就打印"Bin"
#如果它能被7整除,就打印出"Sausage"
#比如输入数字15,程序应该打印"Fizz"和“Bin"
#!/usr/bin/perl -w
use 5.010;
my $SourcePath =
A
R
G
V
[
0
]
;
o
p
e
n
d
i
r
M
Y
D
I
R
,
ARGV[0]; opendir MYDIR,
ARGV[0];opendirMYDIR,SourcePath or die “can’t open
S
o
u
r
c
e
P
a
t
h
:
SourcePath :
SourcePath:!”;
chdir $SourcePath;
foreach (readdir MYDIR){
say “========================================”;
when(-x KaTeX parse error: Expected '}', got 'EOF' at end of input: _){say "SourcePath/$_ executable!";continue}
when(-r KaTeX parse error: Expected '}', got 'EOF' at end of input: _){say "SourcePath/$_ readable !";continue}
when(-w KaTeX parse error: Expected '}', got 'EOF' at end of input: _){say "SourcePath/KaTeX parse error: Expected 'EOF', got '}' at position 16: _ writable ! "}̲ } closedir MYD…ARGV[0]>";
given(KaTeX parse error: Expected '}', got '\A' at position 19: …V[0]){ when (!/\̲A̲\d+\Z/){ say "n…);
my @empty;
when (@divisors~~@empty){say “number is prime”}
default{say "$ is divisible by @divisors"}
}
sub divisors{
my $number=shift;
my @divisors=();
foreach my
d
i
v
i
s
o
r
(
2..
divisor(2..
divisor(2..number/2){
push @divisors,$divisor unless $number % $divisor;
}
return @divisors;
}
#使用given和智能匹配,从命令行得到一个数字,打印出这个数字除了1和它本身以外的因数
#比如输入99,应该报告3,9,11和33这4个数
#如果输入的数字就是一个质数,程序要报告说明这是质数
#如果输入的不是数字,也应该报告说明输入有误,不会继续计算
#首先汇报正在处理的数字,用尖括号来区分KaTeX parse error: Expected 'EOF', got '#' at position 18: …GV[0]和字符串的其余部分 #̲在given中,我们写了两个w…_,每个when条件都尝试用智能匹配对KaTeX parse error: Expected 'EOF', got '#' at position 7: _进行测试 #̲Perl 5.10 版本中的智…ARGV[0]>";
my
f
a
v
o
r
i
t
e
=
42
;
g
i
v
e
n
(
favorite =42; given(
favorite=42;given(ARGV[0]){
when (!/\A\d+\Z/){ say “not a number!”}
my @divisors =divisors($ARGV[0]);
print “@divisors\n”;
when ((
s
a
y
"
_%2 eq 0)){ say "
say"_ is even";
continue;
}
when(!(
s
a
y
"
_%2 eq 0)){ say "
say"_ is odd";
continue;
}
foreach(@divisors){
if(
f
a
v
o
r
i
t
e
=
=
favorite==
favorite==){
say "KaTeX parse error: Expected 'EOF', got '}' at position 41: …rite number"; }̲ } when(favorite){#
_~~
favorite
say "$ is my favorite number";
continue;
}
my @empty;
when (@divisors~~@empty){say "number is prime"}
default{say "$_ is divisible by @divisors"}
}
sub divisors{
my $number=shift;
my @divisors=();
foreach my
d
i
v
i
s
o
r
(
2..
(
divisor(2..(
divisor(2..(ARGV[0]/2+1)){
push @divisors,$divisor unless $number % $divisor;
}
return @divisors;
}
#odd number意思是奇数。 even number意思是偶数
#even匹配所有索引值为偶数的元素,从 0 开始计数
#odd匹配所有索引值为奇数的元素,从 0 开始计数
#报告数字的奇偶情况,是否是质数(除了1和本身外没有其他课整数的数)
#是否可以被某个你最喜欢的数字整除
#一旦获得@divisors数组,就可以用智能匹配来检查其中内容
#如果2在@divisors里面,就可以断定这是偶数
#智能匹配操作符(~~)会根据需要选择恰当的方式比较两端的操作数。它只用于判断操作数是否相同,在比较大小时就不能用了,