Learning Perl学习笔记(3)第四章:子程序

本章要点:
(1)在perl里,&fred和$fred虽然名字一样,但是是两种东西。一个是调用名为fred的子程序;一个是名为fred的标量。
(2)用sub来定义子程序,并用{}来界定子程序的内容。
(3)如果你在一个脚本里定义了两个名字相同的子程序,第二个将会覆盖第一个。
(4)调用子程序,使用&字符。比如:&marine。但是这个符号是可以省略的,除非你的子程序名字与perl的内置函数名字相同,如果是这样的情况,就必须用&来调用你的子程序。
(5)想要立即终止子程序,使用return。
(6)使用my,可以在子程序里设置变量,尽管每次调用子程序时,必须再次定义它们。使用state,可以将私有变量的作用域限定在子程序中,但是Perl将在每次调用之间保留它们的值。
(7)defined:判断一个值是否是undef而不是空字符串,使用defined函数,该函数对undef返回false,对其他所有值返回true:

$next_line = <STDIN>;
if ( defined($next_line) ) {
print "The input was $next_line";
} else {
print "No input available!\n";
}

(8)shift:去掉数组里的第一个元素。也就是卸载参数,当你只需要一个参数时使用shift;读取多个参数时使用列表赋值。(参考:第五章 Perl函数)。

参考文章:
1.Perl 变量
2.第五章 Perl函数

课后练习:

第一题:

Write a subroutine, named total, which returns the total of a list of numbers.
Hint: the subroutine should not perform any I/O; it should simply process
its parameters and return a value to its caller. Try it out in this sample program,
which merely exercises the subroutine to see that it works. The first group of
numbers should add up to 25.

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: ";
my $user_total = total(<STDIN>);
print "The total of those numbers is $user_total.\n";

Note that using <STDIN> in list context like that will wait for you to end input in
whatever way is appropriate for your system.

脚本:

#!/usr/bin/perl
use warnings;
use v5.24;

sub total {
        my $sum;
        foreach ( @_ ) {
                $sum += $_;
        }
        $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(<STDIN>);
print "The total of those numbers is $user_total.\n";
$ ./practice.pl
The total of @fred is 25.
Enter some numbers on separate lines:
6
8
3
The total of those numbers is 17.

第二题

Using the subroutine from the previous problem, make a program to calculate
the sum of the numbers from 1 to 1,000.

脚本:

#!/usr/bin/perl
use warnings;
use v5.24;

sub total {
        my $sum;
        foreach ( @_ ) {
                $sum += $_;
        }
        $sum;
}

my @fred = (1..1000);
my $fred_total = total(@fred);
print "The total of 1 to 1000 is $fred_total.\n";

运行结果:

$ ./practice.pl
The total of 1 to 1000 is 500500.

第三题

Extra credit exercise: write a subroutine, called &above_average, which takes
a list of numbers and returns the ones above the average (mean). (Hint: make
another subroutine that calculates the average by dividing the total by the number
of items.) Try your subroutine in this test program:
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
use warnings;
use v5.24;

sub total {
        my $sum;
        foreach (@_){
                $sum +=$_;
        }
        $sum;
}

sub average {
        my $count =@_; #数组赋值给标量,返回数组元素个数
        my $sum =total(@_); #调用上一个子程序total,这里省略了&符号
        $sum/$count;
}

sub above_average {
        my $average = average(@_); #调用上一个子程序average
        my @list;
        foreach my $number (@_){
                if ($number > $average){
                        push @list,$number; #push的意思是把元素添加到list的最后
                } 
        }
                @list; #返回添加完所有符合要求的元素的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";

运行:

$ ./practice.pl
6 7 8 9 10 is 6 7 8 9 10
(Should be 6 7 8 9 10)
100 is 100
(Should be just 100)

第四题

Write a subroutine named greet that welcomes the person you name by telling
them the name of the last person it greeted:
greet( "Fred" );
greet( "Barney" );
This sequence of statements should print:
Hi Fred! You are the first one here!
Hi Barney! Fred is also here!

脚本:

#!/usr/bin/perl
use warnings;
use v5.24;

sub greet {
        state $last_person; #state声明变量,使该变量为本子程序私有变量
        my $name = shift; #标量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;
}
greet( 'Fred' ); #当第一个参数赋值为Fred时,name标量的undef被Fred取代,执行打印“Hi Fred!”。而defined判断last_person是空字符串,所以if循环执行else的命令。随后,name的参数传递给last_person。
greet( 'Barney' );#第二次调用子程序的时候,name标量被Barney取代,执行操作打印“Hi Barney!”。这时if循环里,defined判断last_person标量里不是空字符串,而是之前name传递过来的Fred,所以执行第一条命令打印“Fred is also here!”

运行结果:

$ ./practice1.pl
Hi Fred! You are the first one here!
Hi Barney! Fred is also here!

第五题

Modify the previous program to tell each new person the names of all the
people it has previously greeted:
greet( "Fred" );
greet( "Barney" );
greet( "Wilma" );
greet( "Betty" );
This sequence of statements should print:
Hi Fred! You are the first one here!
Hi Barney! I've seen: Fred
Hi Wilma! I've seen: Fred Barney
Hi Betty! I've seen: Fred Barney Wilma

脚本:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

sub greet {
        state $last_person;
        state @name; #定义一个数组
        my $name;
        foreach my $name(@_){
                print "Hi $name! "; #打印出标量
                if( defined $last_person ) {
                        print "I'v seen: @name\n"; #这里打印出数组里全部参数,所以每调用一次,打印的参数就会多一个
                }
                else {
                        print "You are the first one here!\n";
                }
                $last_person = $name;
                push @name, $name;#每运行一次,把参数添加到@name数组里
        }
}
greet( 'Fred' ); 
greet( 'Barney' );
greet( 'Wilama');
greet(' betty');

运行结果:

$ ./practice1.pl
Hi Fred! You are the first one here!
Hi Barney! I'v seen: Fred
Hi Wilama! I'v seen: Fred Barney
Hi betty! I'v seen: Fred Barney Wilama
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355