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

Note that using 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();
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

你可能感兴趣的:(Learning Perl学习笔记(3)第四章:子程序)