One, array application
Copy Code code as follows:
#!/usr/bin Env Perl
@zeng = ' top-b-n 1 ';
foreach (@zeng) {
print "$_\n";
}
second, the use of say: Automatic line wrapping
Copy Code code as follows:
#!/user/bin Env Perl
Use 5.010; The version of #表示使用perl 5.10.
Say "Hello world!";
My @fred = Above_average (1..10);
Print "\ @fred is @fred \ n";
Print "(Should be 6 7 8 9) \ n";
My @barney = above_average (100, 1..10);
Print "\ @barney is @barney \ n";
Print "(Should be just) \ n";
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;
}
Copy Code code as follows:
#!/usr/bin/perl-w
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 one here!\n";
}
$last _person = $name;
}
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 one here!\n";
}
Push @names, $name;
}