#!/usr/bin/perl -w $pi = 3.141592654; $circ = 2 * $pi * 12.5; print "The circumference of a circle of radius 12.5 is $circ.\n";
print "Enter first number: "; chomp($one =); print "Enter second number: "; chomp($two = ); $result = $one * $two; print "The result is $result.\n";
print "Enter a string: "; $str =; print "Enter a number of times: "; chomp($num = ); $result = $str x $num; print "The result is:\n$result";
第三章 列表和数组
print "Enter some lines, then press Ctrl-D:\n"; # or maybe Ctrl-Z @lines =; @reverse_lines = reverse @lines; print @reverse_lines;
@names = qw/ fred betty barney dino wilma pebbles bamm-bamm /; print "Enter some numbers from 1 to 7, one per line, then press Ctrl-D:\n"; chomp(@numbers =); foreach (@numbers) { print "$names[ $_ - 1 ]\n"; }
print sort;
第四章 子程序
sub marine { $n += 1; #全局变量$n print "Hello, sailor number $n!\n"; } &marine; #输出Hello, sailor number 1! &marine; #输出Hello, sailor number 2! &marine; #输出Hello, sailor number 3! &marine; #输出Hello, sailor number 4!
$n = &max(44,15,22); print "$n"; sub max{ if(@_!=2){ print "WARNING! &max should get exactly two arguments!\n"; return; } if($_[0] > $_[1]){ print "$_[0]\n"; }else{ print "$_[1]\n"; } return $_[0] + $_[1]; }
sub total { my $sum; # private variable foreach (@_) { $sum += $_; } return $sum; } my @fred = qw{ 1 3 5 7 9 }; my $fred_total = &total(@fred); print "The total of \@fred is $fred_total.\n"; print "The numbers from 1 to 1000 add up to ", &total(1..1000), ".\n";
写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字
sub total { my $sum; # private variable foreach (@_) { $sum += $_; } $sum; } sub average { if (@_ == 0) { return } my $count = @_; my $sum = &total(@_); # from earlier exercise $sum/$count; } sub above_average { my $average = &average(@_); my @list; foreach $element (@_) { if ($element > $average) { push @list, $element; } } @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"; print "(Should be just 100)\n";
第五章 输入与输出
while(){ print "I saw2 $_\n"; }
while(<>){ chomp; print "It was $_ that I saw!\n"; }
输出到文件
$output = "test.txt"; open FH,">", "$output"; print FH "Tristan Blenheim 101\n"; print FH "Betty Boop 201\n"; print FH "Igor Chevsky 301\n"; print FH "Norma Cord 401\n"; print FH "Jonathan DeLoach 501\n"; print FH "Karen Evich 601\n"; close FH;
文件-> 控制台
open IN, "<", "test.txt" or die; @a =; close IN; print "@a";
格式化数据
open FH, "<", "test.txt"; foreach $line(){ $line =~ s/\x0D?\x0A?$//; #取得一行,去掉结尾换行符 @datas = split(" ",$line); #把空格当中分隔符,取得数据 print "@datas\n"; }
输出到错误文件
运行命令chapter5.pl 2> err.txt
print "Hello, World!\nabc"; print STDERR "Hi\n";
第六章 哈希
#my %last_name = qw{ # fred flintstone # barney rubble # wilma flintstone #}; my %last_name; $last_name{"fred"} = "flintstone"; $last_name{"barney"} = "rubble"; $last_name{"wilma"} = "flintstone"; print "Please enter a first name: "; chomp(my $name =); print "That's $name $last_name{$name}.\n";
写一个程序,读入一串单词(一个单词一行)◆,输出每一个单词出现的次数。(提示:如果某个作为数字使用值是
undefined 的,会自动将它转换为0。)如果输入单词为fred, barney, dino, wilma, fred(在不同行中),则输出的fred 将为
3。作为额外的练习,可以将输出的单词按照ASCII 排序。
my(@words, %count, $word); # (optionally) declare our variables chomp(@words =); foreach $word (@words) { $count{$word} += 1; # or $count{$word} = $count{$word} + 1; } foreach $word (keys %count) { # or sort keys %count print "$word was seen $count{$word} times.\n"; }
exists 函数
my %last_name; $last_name{"fred"} = "flintstone"; $last_name{"barney"} = "rubble"; $last_name{"wilma"} = "flintstone"; my $name =; #读入下一行; chomp($name); if(exists $last_name{$name}){ print "Exists"; }else{ print "not exists"; }
第十一章 文件检验
if(-e "test.txt"){ print "e"; }
第十二章 目录操作
列出某个文件夹下的文件
print "Which directory? (Default is your home directory) "; chomp(my $dir =); if ($dir =~ /^\s*$/) { # A blank line chdir or die "Can't chdir to your home directory: $!"; } else { chdir $dir or die "Can't chdir to '$dir': $!"; } my @files = <*>; foreach (@files) { print "$_\n"; }
删除文件
foreach (@ARGV) { unlink $_ or warn "Can't unlink '$_': $!, continuing...\n"; }
移动文件
use File::Basename; use File::Spec; my($source, $dest) = @ARGV; if (-d $dest) { my $basename = basename $source; $dest = File::Spec->catfile($dest, $basename); } rename $source, $dest or die "Can't rename '$source' to '$dest': $!\n";
第十一章 字符串和排序
数字排序
my @numbers; while (<>) { push @numbers, split; } foreach (sort { $a <=> $b } @numbers) { printf "%20g\n", $_; }
字母排序
my @letters; while (<>) { push @letters, split; } foreach (sort { $a cmp $b } @letters) { printf "$_\n"; }
第十四章 进程管理
调用系统命令
chdir "c:/" or die "Can't chdir to root directory: $!"; exec "dir" or die "Can't exec dir: $!";