- 论坛徽章:
- 6
|
本帖最后由 stanley_tam 于 2014-09-08 19:57 编辑
帮一半吧,一个有注释,一个没有
题1:- #!perl
- use strict;
- use FileHandle;
- my %frequency = ();
- ++$frequency{$_} for split /\s+/, do {local $/; FileHandle->new(shift, 'r')->getline};
- printf "%-20s=>%10s$/", $_, $frequency{$_} for sort keys %frequency;
- __END__
复制代码 题2:- #!perl
- use strict;
- use FileHandle;
- use Getopt::Std qw{ getopts };
- sub main;
- sub usage;
- main;
- sub main {
- # get files from parameters
- getopts('f:t:', \my %opts);
- my ($from, $to) = @opts{'f', 't'};
- usage() if not $from or not $to;
- # open files
- my $from_fh = FileHandle->new($from, 'r');
- my $to_fh = FileHandle->new($to, 'w');
- # read and write
- while ( defined(my $line = $from_fh->getline) ) {
- # read one line each time
- $to_fh->print($line);
- }
- # close
- $from_fh->close;
- $to_fh->close;
- }
- sub usage {
- print "Usage:\n\t$0 -f file1 -t file2$/";
- exit 1;
- }
- __END__
复制代码 |
|