一个签到函数

来源:互联网 发布:知乎手机版怎么引用 编辑:程序博客网 时间:2024/05/01 21:24
[root@mysql1 perl]# cat 27.pl 
#!/usr/bin/env perl


use strict;
use warnings FATAL => 'all';
use 5.010;


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;



sub greet1 {
  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
}


greet('fred');
greet('barney');


greet1('fred');
greet1('barney');
greet1('wilma');
greet1('betty');






















[root@mysql1 perl]# ./27.pl   
hi fred! you are the first one here!
hi barney! fred is also here!
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
0 0