Perl 句柄
句柄的定义
- Perl通过句柄和“外面”的世界连接
- 句柄是一个顺序号,对于打开的文件是唯一的识别依据
- 是一种特殊的数据类型
输入输出句柄
间接文件句柄
- 任何的有效的Perl标识符 ,由大小写字母、数字、下划线组成
- 间接文件句柄没有强制性的前缀
- 经常以全部大写字母表示它们
- 常用输入输出句柄:
STDIN
,STDOUT
,STDERR
STDIN
- Standardinpit
- 默认的输入间接Perl文件句柄
- 文件句柄读取的数据取自用户选择的输入设备,通常是键盘
print "please input the data\n"; # 提示用户输入数据
my $data = <STDIN>; # 用户从外部(键盘)输入数据:ABC(回车)
print "The data is $data\n"; # 打印出用户输入的数据:The data is ABC
STDOUT
- Standardout
- 默认的输出间接Perl文件句柄
- 发送给该句柄的数据在用户指定的输出设备上显示,通常为命令窗口。
print "Here is the example\n"
# Here is the example
print STDOUT "Here is the example\n"
# Here is the example
STDERR
- Standarderror
- 用于错误信息、诊断、调试和其他类似的偶发输出
- 默认情况下,
STDOUT
和STDERR
使用相同的输出设备
自定义句柄
- 由用户自己指定文件,获取该文件的标识
打开文件
使用open
函数打开文件
- 语法:
open(filehandle, pathname)
- 打开成功,返回一个非0值, 否则,返回
undef
(假)
if (open (MYFILE, "mydatafile.txt"))
{
print "we open the file successfully\n";
}
else
{
print "Cannot open the file\n";
exit 1;
}
die
函数
-
在Perl中,
die
函数可以用来再出现错误的时候通知解释程序的运行 -
并输出一条有意义的出错信息。
Died at scriptname line XXX
-
语法:
open(FILE, pathname) || die
open(FILE, pathname) or die
-
函数也可以带有自定义的参数,这些参数将取代默认消息被输出。
die <info>
print "script start!\n";
open (MYFILE, "mydatafile.txt") || die "This file not exists";
- 使用
$!
得到系统所需要的最后一个操作的出错信息。
可以用来提示出错的位置和信息。
open (MYFILE, "test.txt") || die "Cannot open
myfile: $!\n";
# Cannot open myfile: No such file or directiry
use strict;
# 打开`*.fq.gz` 或者`*.fq`文件
my ($read1, $read2) = @ARGV;
if ($read1 =~ /\.gz$/) {
open(R1, "gzip -dc $read1|") || die $!;
}
else {
open(R1, "$read1") || die $!;
}
if ($read2 =~ /\.gz$/) {
open(R2, "gzip -dc $read1|") || die $!;
}
else {
open(R2, "$read1") || die $!;
}
warn
函数
- 用来发出一个警告
- 不会终止程序
if (!open(MYFILE, "output.txt"))
{
warn "Cannot read output: \n $!";
}
else
{
print "start to read output file";
}
print "here is another command\n";
# Cannot read output:
# No such file or directory at C:\Perl\File
# here is another command
读写文件
读取文件
- 使用文件输入操作符(
<filehandle>
)读入文件 - 语法:
open (FILE, "finename") || die "Cnnot open the file";
$line = <FILE>; # 读取文件
close FILE; # 关闭文件句柄
说明:
- 当将文件内容赋值给变量时,尖括号运算
<>
符每次只能读取文件的一行输入 - 利用循环结构来读取所有数据
- 文件被读完时,返回值为
undef
open (FILE, "myfile.txt") || die "Cnnot open the file: $!\n";
while ($a = <FILE>)
{
print $a; # 每次打印文件的一行内容,直到读取完毕。
}
close FILE; # 关闭文件句柄
- 通过数组接收所有输入数据 (小型文件比较好用,大文件可能导致内存溢出)
当将文件内容赋值给数组时,尖括号运算<>
将读取文件的所有输入,然后全部赋值给数组
open (FILE, "myfile.txt") || die "Cnnot open the file: $!\n";
my @arr = <FILE>;
print @arr; # 打印文件的全部内容
close FILE; # 关闭文件句柄
写入文件
- 语法:
-- 覆盖写入文件
open(FILE, ">pathname")
-- 追加写入文件
open(FILE, ">>pathname")
写入具体内容 - 使用
print
函数 - 语法:
print finehandle 具体写入对象
open (MYFILE, ">mydatafile.txt"); # 定义以覆盖的方式写入
print MYFILE "abc"; # 写入字符串abc到文件mydatafile.txt
close MYFILE;
关闭文件句柄
每次打开句柄后,都要记得关闭
文件测试运算符
测试文件的必要性
- 实际的读写速度比较闷
- 读写时容易产生错误
- Perl提供了文件测试运算符
文件测试运算符
-r
read 例如,-r 'file'
: 可以读取'file'文件,则返回为真
-w
write 例如,-w $a
:$a
中包含的文件名是可以写入的文件,则返回为真
-e
exist 例如, -e 'myfile'
: 'myfile'文件存在,则返回为真
-z
zero 例如, -z 'data'
: 'data'文件存在,但是文件为空,则返回为真
-s
size 例如, -s 'data'
: 'data'文件存在,则返回'data'的大小(字符个数)
-f
file 例如,-f 'novel.txt'
:'novel.txt'是个普通文件(区别于二进制文件、可执行文件等),则为真
-d
directory 例如,-d '/tmp'
: '/tmp'是个目录,则为真
-T
text 例如,-T 'unkonw'
:'unkonw'为文本文件,则为真
-B
bin 例如,-B 'unkonw'
:'unkonw'为二进制文件,则为真
-M
例如,-M 'foo'
: 返回'foo'文件被修改后经过的时间
网友评论