2010年1月27日

用 Perl 寫簡單的 nl command

Perl的基本語法

#!/usr/bin/perl
# filename: nl.pl
use 5.006;
use strict;
use warnings;

my $fileHandle = *STDIN;

if (@ARGV > 0) {
open FILE, "<", $ARGV[0];
$fileHandle = *FILE;
}

while(<$fileHandle>) {
printf "%7.d\t%s", $., $_;
}

close $fileHandle;

__END__
$ perl --version | ./nl.pl
1
2 This is perl, v5.10.0 built for x86_64-linux-gnu-thread-multi
3
4 Copyright 1987-2007, Larry Wall
5
6 Perl may be copied only under the terms of either the Artistic License or the
7 GNU General Public License, which may be found in the Perl 5 source kit.
8
9 Complete documentation for Perl, including FAQ lists, should be found on
10 this system using "man perl" or "perldoc perl". If you have access to the
11 Internet, point your browser at http://www.perl.org/, the Perl Home Page.
12

沒有留言:

網誌存檔