打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Perl语言入门_中文版_第六版程序

#Chapter1 简介
#!/usr/bin/perl -w
use strict;
use 5.010;
say "HelloWorld !!!";

#!/usr/bin/perl -w
use strict;
use 5.010;
my @lines = `perldoc -u -f atan2`;
foreach (@lines) {
    s/\w<([^>]+)>/\U$1/g;
    print;
}

#Chapter2 标量数据
#!/usr/bin/perl -w
use strict;
use 5.010;
my $what = "brontosaurus steak";
my $whats = "hello";
my $n = 3;
print "fred ate $n $whats.\n";
print "fred ate $n ${what}s.\n";
print "fred ate $n $what"."s.\n";
print 'fred ate ' . $n . ' ' . $what."s.\n";
#!/usr/bin/perl -w
use strict;
use 5.010;
my $alef = chr(0x05D0);
my $alpha = chr(hex('03B1'));
my $omega = chr(0x03C9);
my $code_point = ord('? ');
print "$alef\t$alpha\t$omega\t$code_point";
# if 控制结构
#!/usr/bin/perl -w
use strict;
use 5.010;
my @names= qw \barney fred wilam dino\;
foreach my $name(@names) {
    if ($name gt 'fred')
    {
        print"$name comes after 'fred' in sorted order.\n";
        }
}
#!/usr/bin/perl -w
use strict;
use 5.010;
my @names1= qw \barney fred wilam dino\;
foreach my $name(@names1) {
    if ($name gt 'fred')
    {
        print"$name comes after 'fred' in sorted order.\n";
    } else {
        print "$name does not come after 'fred'.\n";
        print "Maybe it's the same string, in fact.\n";
    }
}
#布尔值
#!/usr/bin/perl -w
use strict;
use 5.010;
my @names2= qw \barney fred wilam dino\;
my $is_bigger;
foreach my $name(@names2) {
    $is_bigger = $name gt 'fred';
    if ($is_bigger)
    {
        print"$is_bigger comes 1.\n";
    } else {
        print "$is_bigger does 0\n";
        print "$is_bigger is maybe 0.5\n";
    }
}
#!/usr/bin/perl -w
use strict;
use 5.010;
my @names3= qw \barney fred wilam dino\;
my $is_bigger1;
foreach my $name(@names3) {
    $is_bigger1 = $name gt 'fred';
    if (!$is_bigger1)
    {
        print"$is_bigger1 comes 1.\n";
    } else {
        print "$is_bigger1 does 0\n";
        print "$is_bigger1 is maybe 0.5\n";
    }
}
#获取用户输入
#!/usr/bin/perl -w
use strict;
use 5.010;
my $line=<STDIN>;
if ($line eq "\n") {
    print "That was just a blank line!\n";
} else {
    print "That line of input:$line";
}
#chomp操作符
#!/usr/bin/perl -w
use strict;
use 5.010;
my $text="a line of text\n\n\n";
my $txt = chomp($text);
print "$txt : How are you!";
#!/usr/bin/perl -w
use strict;
use 5.010;
my $food=<STDIN>;
my $betty = chomp $food;
print "$betty : How are you!";
#while控制结构
#!/usr/bin/perl -w
use strict;
use 5.010;
my $count = 0;
   print "count is now: ";
while ($count < 10) {
    $count += 2;
    print "$count ";
}
print "\n";
# undef值
#!/usr/bin/perl -w
use strict;
use 5.010;
my $n = 1;
while ($n < 10) {
    my $sum += $n;
 $n += 2;
}
print "The total was $sum.\n";
#defined函数
#!/usr/bin/perl -w
use strict;
use 5.010;
my $madonna = <STDIN>;
if (defined($madonna)) {
    print "The input was : $madonna";
} else {
    print "No input availble!\n";
}
#Chapter3 列表与数组
#!/usr/bin/perl -w
use strict;
use 5.010;
(my $fred, my $barney, my $dino) = ("flintstone", "rubble", undef);
($fred, $barney) = ($barney, $fred);
my @rocks = qw/ bedrock slate lava/;
#$dino = "granite";
my @tiny = ();
my @quarry = (@rocks, "crushed rock", @tiny, $dino);
#!/usr/bin/perl -w
use strict;
use 5.010;
my @array = 5..9;
my $fred = pop(@array);
print "$fred (@array)\n";
my $barney = pop @array;
print "$barney (@array)\n";
pop @array;
print "@array\n";
push(@array, 0);
print "@array\n";
push @array, 8;
print "@array\n";
push @array, 1..10;
print "@array\n";
my @others = qw/9 0 2 1 0/;
push @array, @others;
print "@array";
#!/usr/bin/perl -w
use strict;
use 5.010;
my @array = qw# dino fred barney #;
my $m = shift(@array);
print "$m (@array)\n";
my $n = shift @array;
print "$n (@array)\n";
shift @array;
print "@array\n";
my $o = shift @array;
print "$o (@array)\n";
unshift(@array, 5);
print "@array\n";
unshift @array, 4;
print "@array\n";
my @others = 1..3;
unshift @array, @others;
print "@array";
#!/usr/bin/perl -w
use strict;
use 5.010;
my @array = qw#pebbles dino fred barney betty#;
my @remove = splice @array, 1, 2;
print "Aremove : @remove\tArray : @array\n";
my @removed = splice @array, 1, 2, qw(wilam);
print "Aremoved : @removed\tArray : @array\n";

#字符串中数组内插
#!/usr/bin/perl -w
my @rocks=qw{flintstone alate rubble};
print "quartz @rocks limestone\n";
my @empty;
my @rocks=qw{flintstone alate rubble};
    foreach my $rocks (@rocks){
            print "quartz $rocks limestone\n";
    }
print "Three rocks are: @rocks.\n";
print "There's nothing in the (@empty) parents here..\n";
my $email;
$email="fred@bedrock.edu";
print "$email\n";
$email="fred\@bedrock.edu";
print "$email\n";
$email='red@bedrock.edu';
print "$email\n";
my @fred=qw(hello dolly);
my $y=2;
my $x;
$x="This is $fred[1]'s place";
print "x1=$x\n";
$x="This is $fred[$y-1]'s place";
print "x2=$x\n";
$y="2*4";
$x="This is $fred[$y-1]'s place";
print "x3=$x\n";
my @fred=qw(eating rocks is wrong);
$fred="right";
print "this is $fred[3]\n";
print "this is ${fred}[3]\n";
print "this is $fred"."[3]\n";
print "this is $fred\[3]\n";
foreach $rock(qw/bedrock slate lava/)
{
    print "One rock is $rock.\n";
}
my @rocks=qw/bedrock slate lava/;
foreach my $rock(@rocks)
{
    $rock="\t$rock";
    $rock.="\n";
}
print "The rocks are:\n",@rocks;
#Perl最喜欢的默认变量: $_
foreach(1..10){
    print "I can count to $_\n";
}
$_="Tabba dabba doo\n\n";
print;
#reverse
@fred=6 .. 10;
print "@fred\n";
@barney = reverse(@fred);
print "@barney\n";
@wilma=reverse 6 .. 10;
print "@wilma\n";
@fred = reverse @fred;
print "@fred\n";
#sort
my @rocks=qw/bedrock slate rubble grsanite/;
print "@rocks\n";
my @sorted=sort(@rocks);
print "@sorted\n";
my @back=reverse sort @rocks;
print "@back\n";
@rocks=sort @rocks;
print "@rocks\n";
my @numbers=sort 97 .. 102;
print "@numbers\n";
#!/usr/bin/perl -w
use strict;
@rocks=qw/bedrock slate rubble granite/;
@sorted=sort@rocks;
print("@sorted\n");
#each
#! c:\perlfile -w
use strict;
use 5.012;
my @rocks=qw/bedrock slate rubble grsanite/;
while (my($index, $value) = each @rocks) {
    say "$index: $value";
}
#! c:\perlfile -w
use strict;
use 5.012;
my @rocks=qw/bedrock slate rubble grsanite/;
foreach my $index (0..$#rocks){
    say "$index: $rocks[$index]";
}
#context标量上下文与列表上下文
$something=3;
$q=4+$something;
print("$q\n");
my @people=qw(fred barney betty dandong);
my @sorted=sort @people;
print("@sorted\n");
my $number=42+@people;
print("$number\n");
my @list=@people;
print("@list\n");
my $n=@people;
print("$n\n");
#在标量上下文中使用产生列表的表达式
my @backwards=reverse qw/yabba dabba doo/;
print("@backwards\n");
my $backwards=reverse qw/yabba dabba doo/;
print("$backwards\n");
#在列表上下文中使用产生标量的表达式
#强制指定标量上下文
my @rocks=qw(talc quartz jade obsidian);
print "How many rocks do you have?\n";
print "I have ",@rocks, " rocks!\n"; #WRONG
print "I have ",scalar @rocks, " rocks!\n";
#列表上下文的<STDIN>
#file结尾 Unix Ctrl+D DOS Windows Ctrl+Z
#变长参数列表
#!/usr/bin/perl -w
use 5.012;
use strict;
my @rocks = qw/12 34 27/;
max();
sub max {
    if (@_ != 2) {
        print"WARNING! &max should get exactly two arguments!\n";
    }
    else {
        print"quitS\n";}
}

#习题
#1
#! c:\perlfile -w
use strict;
use 5.012;
print "Ente some lines, then press Ctrl-Z: \n";
print reverse <STDIN>;
#2
#! c:\perlfile -w
use strict;
use 5.012;
my @names = qw/fred betty barney dino wilma pebbles bamm-bamm/;#when input one number,enter
print "Ente some numbers from 1 to 7, one per line, then press Ctrl-Z: \n";
chomp( my @numbers = <STDIN> );
foreach(@numbers){
    print "$names[$_ - 1]\t";
}
#3
#! c:\perlfile -w
use strict;
use 5.012;
chomp(my @lines = <STDIN>);
my @sorted = sort @lines;
print "@sorted\n";
#! c:\perlfile -w
use strict;
use 5.012;
print sort <STDIN>;
#Chapter4 子程序
#定义子程序
my $n;
sub marine {
    $n += 1;
    print "Hello, sailor number $n !\n"
}
#调用子程序
&marine;
&marine;
&marine;
&marine;
#通过scalar获取数组长度
#!/usr/bin/perl -w
use strict;
use 5.012;
use warnings;
my @array = qw(this program shows haw to use scalar funcation to get the array length);
my $length = scalar(@array);
print $length;
#返回值
#!/usr/bin/perl -w
use strict;
use 5.012;
chomp(my ($fred, $barney) = <STDIN>);
sub sum_of_fred_and_barney {
    print "Hey, you called the sum_of_fred_and_barney subroutine !\n";
    $fred + $barney;
    print "Hey, I'm returning a value now!\n";
}
my $wilam = &sum_of_fred_and_barney;
print "\$wilam is $wilam.\n";
my $betty = 3 * &sum_of_fred_and_barney;
print "\$betty is $betty.\n";
sub large_of_fred_or_barney {
    if ($fred>$barney) {
        $fred;
    }else{
        $barney;
    }  
}
$fred=123;$barney=345;
$wilam=&large_of_fred_or_barney;
print "\$wilam is $wilam\n";
#参数
sub max{
    if ($_[0]>$_[1]) {
        $_[0];
    }else{
        $_[1];
    }
}
my $n = &max(10,15);
print "Max number is $n\n";
#!/usr/bin/perl -w
use strict;
use 5.012;
my $wilam = &large_of_fred_or_barney_or_dino(12,7,25);
print "\$fred or \$barney or \$dino max is $wilam.\n";
sub large_of_fred_or_barney_or_dino{
    if ($_[0]>$_[1] && $_[0]>$_[2]) {
        $_[0];
    }elsif ($_[1]>$_[2]){
        $_[1];
    } else {
        $_[2];
 
   }
}
#子程序 私有变量
#!/usr/bin/perl -w
use strict;
use 5.012;
my $mv=&max(12,7);
print "\$mv. $mv\n";
sub max {
    my($m, $n);
    ($m, $n) = @_;
    if ($m > $n) {$m}
    else{$n}    
}
#变长参数列表
#!/usr/bin/perl -w
use strict;
use 5.012;
my $mv = &max(7, 3, 12);
print "\$mv's max is $mv\n";
sub max {
    if(@_ != 2){
        print "WARNING! &max should get exactly two arguments!\n";
    }
    my($m, $n, $p);
    ($m, $n, $p) = @_;
    if ($m > $n && $m >$p) {$m}
    elsif($n > $p){$n}
    else{$p}
}
#改进的&max子程序
sub max {
    my ($max_so_far) = shift @_;
    foreach(@_){
     if ($_ > $max_so_far){
         $max_so_far = $_;
     }
    }
    $max_so_far;
}
my $maximum = &max(3,5,10,4,6);
print "Max value is : ", $maximum,"\n";
#空参数列表
#关于词法(没有)变量
foreach(1..10){
    my($square) = $_ * $_;
    print "$_ squared is $square.\n";
}
#!/usr/bin/perl -w
use strict;
use 5.012;
foreach my $rock (qw/bedrock slate lava/){
    print "One rock is $rock.\n";
}
#use strict编译指令
#return操作符
#!/usr/bin/perl -w
use strict;
my @names = qw/ fred barney betty dino wilam pebbles bamm-bamm/;
my $result = &which_element_is("dino", @names);
sub which_element_is {
    my($what, @array) = @_;
    foreach(0..$#array){
        if ($what eq $array[$_]) {  
            return $_;
        } 
    }
    -1;
}
my @names = qw/fred barney betty dino Wilam pebbles bam-bamn/;
my $result = &which_element_is("fred",@names);
sub which_element_is{
    my($what,@array) = @_;
    foreach(0..$#array){
       if($what eq $array[$_]){
        print $_, " ---> ", $array[$_];
        return $_;
    }
}   print "Error";
    -1;
}
#省略与号
sub division {
 $_[0] / $_[1];
}
my $quotient = division 355, 113;
print "\$quotient is $quotient.\n";
#!/usr/bin/perl -w
use strict;
use 5.012;
sub chomp {
    print "Much, much!\n";
}
&chomp;
#非标量返回值
#!/usr/bin/perl -w
use strict;
use 5.012;
my $fredman = 5;
my $barneyman = 10;
my @c = &list_from_fred_to_barney ($fredman,$barneyman);
sub list_from_fred_to_barney {
    my ($fred,$barney) = @_;
    if ($fred < $barney) {
        #Cont upwards from $frd to $barney
        $fred..$barney;
    } else {
        #Count downwards from $fred to $barney
        reverse $barney..$fred;
    } 
}
print "@c\n";

#持久性私有变量
#!/usr/bin/perl -w
use strict;
use 5.012;
sub marine{
    state $n = 0;
    my $name = $_[0];
    $n+=1;
    print "Hello, sailor number ", $n, " <---> ", $name, " !\n";
}
marine("dino");
marine("barnet");
marine("fred");
marine("wiliam");
#!/usr/bin/perl -w
use strict;
use 5.012;
running_sum(5, 6);
running_sum(1..3);
running_sum(4);
sub running_sum {
    state $sum = 0;
    state @numbers;
    foreach my $number (@_) {
        push @numbers, $number;
        $sum += $number;
    }
    say "The sum of (@numbers) is $sum";
}

#习题
#1
#! c:\perlfile -w
use strict;
use 5.012;
my @fred = qw(1 3 5 7 9);
print "\@fred is @fred\n";
my $fred_total = total(@fred);
print "The total of \@fred is $fred_total\n";
print "Enter some numbers on separate lines: ";
my $user_total = total(<STDIN>);
print "The total of those numbers is $user_total.\n";
sub total{
    my $sum;
    foreach(@_){
        $sum += $_;
    }
    $sum;
}
#2
#! c:\perlfile -w
use strict;
use 5.012;
my @numbers = 1..1000;
my $number_total = total(@numbers);
print "The total of (1...1000) is $number_total\n";
sub total{
    my $sum;
    foreach(@_){
        $sum += $_;
    }
    $sum;
}
#3
#! c:\perlfile -w
use strict;
use 5.012;
my @numbers = 1..1000;
my $number_total = total(@numbers);
my $number_average = average(@numbers);
my @number_above_average = above_average(@numbers);
print "The total of (1...1000) is $number_total\n";
print "The average of (1...1000) is $number_average\n";
print "The above_average of (1...1000) is @number_above_average\n";
sub total{
    my $sum;
    foreach(@_){
        $sum += $_;
    }
    $sum;
}
sub average {
    if (@_ == 0) {return}
    my $count = @_;
    my $sum = total(@_);
    $sum/$count;
}
sub above_average{
    my $average = average(@_);
    my @list;
    foreach my $element (@_) {
        if ($element > $average) {
            push @list, $element;
        }      
    }
    @list;
}
#4
#!c:/perl -w
use strict;
use 5.010;
greet('Fred');
greet('Barney');
sub greet {
    state $last_person;
    my $name = shift;
    print "Hi $name! ";
   
    if (defined $last_person) {
        print "$last_person is also here!\n";
    }
    else {
        print "You are the first one here!\n";
    }
    $last_person = $name;
}
#5
#!c:/perl -w
use strict;
use 5.010;
greet('Fred');
greet('Barney');
greet('Wilma');
greet('Betty');
sub greet {
    state @names;
    my $name = shift;
    print "Hi $name! ";  
    if (@names) {
        print "I've seen: @names !\n";
    }
    else {
        print "You are the first one here!\n";
    }
    push @names, $name;
}
 
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
perl基本语法
Spreadsheet::ParseExcel
Lighttpd + mod_cgi + python+Perl简单配置
那一片海----漂泊的灵魂‘Blog - IIS下支持CGI、PERL
perl几个读取文件命令测试比较
python3 bytes.decode()与python3 str.encode()
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服