Perl学习笔记(5)glob的使用

本文详细解析了read()函数中的offset参数的含义及使用方法,提供了优化技巧,并介绍了如何利用glob函数读取目录下所有文件的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

######################read()函数中的offset参数含义
read (FILEHANBD, $var, $length, $offset)
read 函数有四个参数,最容易误解的是$offset
它的含义看官方文档,解释是:

An OFFSET may be specified to place the read data at some place in the string other than the beginning. A negative OFFSET specifies placement at that many characters counting backwards from the end of the string. A positive OFFSET greater than the length of SCALAR results in the string being padded to the required size with "\0" bytes before the result of the read is appended.


#####################优化技巧

函数的开销是很大的。
A循环地调用一个函数
B在函数中循环地进行一个操作
从性能上说,B比A好。


#####################glob的用法--读取一个目录下的所有文件
1. 比如要读取/home/globtest目录中的所有文件,可以这样写:@plFiles = glob '/home/globtest/*.*';
上面的glob的用法相当于如下
opendir(Dir,"/home/globtest");#注意这是在Linux的写法,若在Windows下,应该写为opendir(Dir,"F:\\home\\globtest");
@Files=readdir(Dir);
closedir(Dir);
foreach $Cur(@Files){
$File="/home/globtest".$Cur;
open(IN,$File);
while(<IN>){
......
                   }
close(IN);
}


2 . @many = glob "{apple,tomato,cherry}={green,yellow,red}";
把@many数组输出就能看到会显示:
1 apple=green
2 apple=yellow
3 apple=red
4 tomato=green
5 tomato=yellow
6 tomato=red
7 cherry=green
8 cherry=yellow
9 cherry=red









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值