#!/usr/bin/perl -w
my $secret_num = int(1 + rand 100);
print “Don’t tell anyone, but the secret number is $secret_num.\n”;
while(1) {#无限的while循环
print “Please enter a guess from 1 to 100:\n”;
chomp(my KaTeX parse error: Expected 'EOF', got '#' at position 27: …N>); #̲(/^quit|^exitKaTeX parse error: Expected group after '^' at position 2: |^̲\s*/) { # quit,exit,空白行
if($guess =~ /quit|exit|\A\s*\z/i){#匹配的是空白行 \A匹配绝对开头,匹配失败不会顺移 \s 空格,和 [\n\t\r\f] 语法一样 i 忽略模式中的大小写\z匹配绝对末尾,
print "Sorry you gave up. The number was KaTeX parse error: Expected 'EOF', got '\n' at position 12: secret_num.\̲n̲";
last;#last…guess<KaTeX parse error: Expected '}', got '\n' at position 45: …all. Try again!\̲n̲";
}elsif(guess==$secret_num){
print “That was it!\n”;
last;
}else{
print “Too large. Try again!\n”;
}
}
#此程序第一行会从范围1到100挑出一个秘密数字,运作细节如下。首先,rand是perl的随机数函数,
#所以rand 100会产生0以上100以下的随机数,也就是说,该表达式的最大值差不多是99.999
#加1之后,数字的范围将会是1到100.999,然后使用int函数取出整数部分,这就是我们所需要的范围1到100的数字
#while(2)?
#while语句的原型是while(表达式)语句,当表达式为非0值时,执行while语句中的嵌套语句。
#while(1)其中1代表一个常量表达式,它永远不会等于0。循环会一直执行下去。除非你设置break等类似的跳出循环语句循环才会中止。
#!/usr/bin/perl -w
use 5.010;
my $Debug = $ENV{DEBUG}//1;
my $secret_num = int(1 + rand 100);
print "Don’t tell anyone, but the secret number is KaTeX parse error: Expected 'EOF', got '\n' at position 12: secret_num.\̲n̲";
while(Debug) {#无限的while循环
print “Please enter a guess from 1 to 100:\n”;
chomp(my KaTeX parse error: Expected 'EOF', got '#' at position 27: …N>); #̲(/^quit|^exitKaTeX parse error: Expected group after '^' at position 2: |^̲\s*/) { # quit,exit,空白行
if($guess =~ /quit|exit|\A\s*\z/i){#匹配的是空白行 \A匹配绝对开头,匹配失败不会顺移 \s 空格,和 [\n\t\r\f] 语法一样 i 忽略模式中的大小写\z匹配绝对末尾,
print "Sorry you gave up. The number was KaTeX parse error: Expected 'EOF', got '\n' at position 12: secret_num.\̲n̲";
last;#last…guess<KaTeX parse error: Expected '}', got '\n' at position 45: …all. Try again!\̲n̲";
}elsif(guess==KaTeX parse error: Expected '}', got '\n' at position 37: …t "That was it!\̲n̲";
last;
…Debug变量为真的时候调用print,而KaTeX parse error: Expected 'EOF', got '#' at position 26: …自于环境变量,要么是默认值1
#̲通过使用//操作符,我们在ENV{DEBUG}未定义的时候设置它为1
#use 5.010;
#my $last_name = KaTeX parse error: Expected '}', got 'EOF' at end of input: last_name{someone} // ‘(No last name)’;
#上面的含义为:如果last_name哈希中没有someon的键时就是用//后面的值作为last_name的值。
#四个操作符 &&、 ||、 // 和?: 都有一个共性:根据左边的求值决定是否计算右侧的表达式
#perl脚本里的ENV?
#%ENV,perl中的哈希变量,里面保存的是环境变量。键是环境变量名,值是环境变量值。
#例如,有一个环境变量是PATH,其值为C:\windows,那么,打印这个环境变量的方法就是:
#print($ENV{PATH});
#!/usr/bin/perl -w
use 5.010;
$ENV{“ZERO”} = 0;
$ENV{“EMPTY”} = ‘’;
$ENV{“UNDEFINED”} = undef;
my $longest= 0;
foreach my $key (keys %ENV)
{
my keylength=length(key); longest=key_length if keylength>longest;
}
foreach my KaTeX parse error: Expected '}', got 'EOF' at end of input: …)
{
printf "%-{longest}s %s\n", $key, KaTeX parse error: Expected '}', got 'EOF' at end of input: ENV{key} // “(undefined)”; #左对齐
}
#在程序开头,我们设置了环境变量的值,键ZERO和EMPTY有假值(而非未定义值),而键UNDEFINED没有值
#之后在printf的参数列表中,使用//操作符来只在KaTeX parse error: Expected '}', got 'EOF' at end of input: ENV{key}为未定义值的时候打印字符串(undefined)
#通过使用//,可以确保对ZERO和EMPTY键对应的假值不做处理
#宽度字符是负的,是让字符串左对齐,一一对应
#-----------------------------------------------------------#
如果value=′′,那么value || “(undefined)” 返回"(undefined)"
如果value=′′,那么value // “(undefined)” 返回’’;
如果value=undef,value || “(undefined)”
和$value // “(undefined)“都返回”(undefined)”
#-----------------------------------------------------------#
|
请发表评论