###ex4_1.pl
#!/usr/bin/perl -w
use strict;
sub total {
my (
s
u
m
,
sum,
sum,number);
$sum=0;
foreach $number (@_){
s
u
m
+
=
sum+=
sum+=number;
}
KaTeX parse error: Expected 'EOF', got '#' at position 5: sum;#̲这个将sum返回给调用者
}
#子程序total使用$sum来存储总和
my @fred = qw{ 1 3 5 7 9 };
my $fred_total = total(@fred);
print “The total of @fred is $fred_total.\n”;
print “Enter some numbers on separate lines:\n”;
my $user_total = total();
print “The total of those numbers is $user_total.\n”;
####ever think $number=shift @_;
#!/usr/bin/perl -w
use strict;
sub total {
my (
s
u
m
,
sum,
sum,number);
$sum=0;
foreach $number (@_){
s
u
m
+
=
sum+=
sum+=number;
}
KaTeX parse error: Expected 'EOF', got '#' at position 5: sum;#̲这个将sum返回给调用者
}
print “the sum of 1000! is “,total(1…1000),”.\n”;
#注意不能在双括号括住的字符串里直接调用子程序,所以子程序调用的是print的另一个独立参数
###ex4_3.pl
#!/usr/bin/perl -w
use strict;
sub total {
my (
s
u
m
,
sum,
sum,number);
$sum=0;
foreach $number (@){
s
u
m
+
=
sum+=
sum+=number;
}
KaTeX parse error: Expected 'EOF', got '}' at position 7: sum; }̲ sub average { …all)=1;
my($result)=shift@;
foreach(@){
r
e
s
u
l
t
+
=
result+=
result+=;
a
l
l
=
all=
all=all+1;
}
return
r
e
s
u
l
t
/
result/
result/all;
}
sub above_average {
my($mean)=&average(@_);
my $every;
my @list;
foreach KaTeX parse error: Expected '}', got 'EOF' at end of input: …y (@_){ if (every > KaTeX parse error: Expected '}', got 'EOF' at end of input: … push @list,every;#此处必须加$every,否则无法输出?
}
}
@list;
}
my @fred = above_average(1…10);
print “@fred is @fred\n”;
print “(Should be 6 7 8 9 10)\n”;
my @barney = above_average(100,1…10);
print “@barney is @barney\n”;
print “(Should be just 100)\n”;
#!/usr/bin/perl -w
use strict;
sub greet {
my
f
i
r
s
t
=
s
h
i
f
t
@
;
p
r
i
n
t
"
H
I
,
first=shift @_; print "HI,
first=shift@;print"HI,first!you are the first one here!\n";
my
l
a
t
e
r
=
s
h
i
f
t
@
;
p
r
i
n
t
"
h
i
,
later=shift @_; print "hi,
later=shift@;print"hi,later,$first is also here!\n";
}
greet (“fred”,“barney”)
#!/usr/bin/perl -w
use strict;
sub greet {
my
f
i
r
s
t
=
s
h
i
f
t
@
;
p
r
i
n
t
"
H
I
,
first=shift @_; print "HI,
first=shift@;print"HI,first!you are the first one here!\n";
my
s
e
c
o
n
d
=
s
h
i
f
t
@
;
p
r
i
n
t
"
h
i
,
second=shift @_; print "hi,
second=shift@;print"hi,second! I’ve seen: $first\n";
my
t
h
i
r
d
=
s
h
i
f
t
@
;
p
r
i
n
t
"
h
i
,
third=shift @_; print "hi,
third=shift@;print"hi,third! I’ve seen: $first $second\n";
my
f
o
u
r
t
h
=
s
h
i
f
t
@
;
p
r
i
n
t
"
h
i
,
fourth=shift @_; print "hi,
fourth=shift@;print"hi,fourth! I’ve seen: $first $second $third\n";
}
greet (“fred”,“barney”,“wilma”,“betty”)
小笔记
@是函数传参时放置参数的数组,可以从中取实参
比如my(
p
a
1
,
pa1,
pa1,pa2…)=@,函数调用时填了几个参数,便可以从该数组中取到几个参数
$_是默认参数的意思,指的是在不指定的情况下,程序处理的上一个变量
欣洁的代码运行出来从第二行开始前面出现空格的问题:
不要加双引号,因为数组里的元素默认是要用空格分割的,用双引号把所有元素拼成一个字符串,不用双引号则分别输出各个元素
perl的内置警告信息
use warnings;
warnings比-w更灵活,它可以选择只对文件中使用了该编译指令的部分代码开启警告功能,而-w选项则不加区分,对整个程序中涉及的所有代码都开启警告功能
-w
use diagnostics; 这个编译命令报告更为详尽的问题描述;输出相关的错误信息,但程序启动变慢,可以通过-Mdiagnostics 来实现
W的意思是警告级别属于普通警告,numeric是警告类型属于数字操作一类