1、返回文件的第一行
CODE:
#!/usr/bin/perl
#
use IO::File; #使用IO:File 模块
my $file=shift; #取得文件名
my $fh=IO::File ->new($file); #打开文件
my $line=<$fh>; #读取第一行
print $line; #打印出来2、打印整个文件内容CODE:
#!/usr/bin/perl
#
use IO::File;
my $file=shift;
my $fh=IO::File ->new($file);
while (<$fh>){ #用while 方法读取文件句柄
print; #打印,实际上是 print $_; $_可以省略
}3、返回服务器的提示的第一行CODE:
#!/usr/bin/perl
#
use IO::Socket;
my $server=shift;
my $fh=IO::Socket::INET->new($server);
my $line=<$fh>;
print $line;执行:%./socket.pl localhost:smtp
220 ****.com.cn ESMTP Mail System
4、统计一个TEXT文件的行数
CODE:
use strict;
use IO::File;
my $file=shift; #取得文件名
my $counter=0; #计数器初始
my $fh=IO::File->new($file) or die "Can't open $file:$!\n"; #打开文件
while (defined (my $line=$fh->getline)){ #读取每一行
$counter++; #统计
}
STDOUT->print ("counted $counter lines. \n"); #打印结果5、将主机名转换为IP地址CODE:
#!/usr/bin/perl
#
use Socket;
while (<>){ #这里是读取输入
chomp; #去掉一些特殊符号,\t \n 之类的
my $packed_address=gethostbyname($_); #调用 gethostbyname 函数
unless ($packed_address){ #如果$packed_address 为空,即没有返回IP
print "$_ => ?\n";
next;
}
my $dotted_quad =inet_ntoa($packed_address); #inet_ntoa 将IP转换为 0.0.0.0格式
print "$_ => $dotted_quad\n";
}%./ip_trans.pl <hostwww.163.com => 202.108.9.16
www.sina.com => 202.108.33.32
From: http://www.extmail.org/forum/read.php?tid=1356&fpage=2