无涯教程-Perl - readline函数

231 阅读1分钟

描述

此函数从EXPR引用的文件句柄中读取一行,并返回输出。如果要直接使用FILEHANDLE,则必须将其作为typeglob传递。

Simply readline function is equvivalent to <>.

语法

以下是此函数的简单语法-

readline EXPR

返回值

此函数在标量context中仅返回一行,而在列表context中,返回到文件末尾的行列表

以下是显示其基本用法的示例代码-

#!/usr/bin/perl -w

my($buffer)="";
open(FILE, "/etc/services") or
   die("Error reading file, stopped");

$buffer=<FILE>;
print("$buffer");

$buffer=readline( *FILE );
print("$buffer");

close(FILE);

执行上述代码后,将产生以下输出-

# /etc/services:
# $Id: services,v 1.33 2003/03/14 16:41:47 notting Exp $

参考链接

www.learnfk.com/perl/perl-r…