perl读写文件代码实例实用指南
平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“perl读写文件代码实例”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。
#mode operand create truncate
#read <
#write > yes yes
#append >> yes
Case 1: Throw an exception if you cannot open the file:
复制代码 代码如下所示:
use strict;
use warnings;
my $filename = 'data.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' with the error $!";
while (my $row = <$fh>) {
chomp $row;
print "$rown";
}
close($fh);
Case 2: Give a warning if you cannot open the file, but keep running:
复制代码 代码如下所示:
use strict;
use warnings;
my $filename = 'data.txt';
if (open(my $fh, '<:encoding(UTF-8)', $filename)) {
while (my $row = <$fh>) {
chomp $row;
print "$rown";
}
close($fh);
} else {
warn "Could not open file '$filename' $!";
}
Case 3: Read one file into array
复制代码 代码如下所示:
use strict;
use warnings;
my $filename = 'data.txt';
open (FILEIN, "<", $filename)
or die "Could not open file '$filename' with the error $!";
my @FileContents = <FILEIN>;
for my $l (@FileContents){
print "$ln";
}
close FILEIN;
end
-
09.04
Handcrafted提示词怎么写-画面元素和风格参数
-
09.04
Professional提示词怎么写-画面元素和风格参数
-
09.04
OpenAI“1号商务员工”将离职年初至今已有十余名高管离任
-
09.04
云计算巨头Coreweave
-
09.04
Glacial提示词怎么写-画面元素和风格参数
-
09.04
掌握Excel AVERAGEIF函数讲了什么-主要信息和内容重点
-
- Lua中使用模块的一些基础知识实用指南
- 09.04
-
-
- SQLite教程(四):内置函数实用指南
- 09.04
-
-
- Sqlite 操作类代码实用指南
- 09.04
-
-
下载
- |
-
-
下载
- 《行尸走肉第一章》免安装中文汉化硬盘版下载
- 单机|436 MB
- 一款以动作冒险为主题的游戏
-
-
下载
- 《街头霸王X铁拳》免安装中文汉化硬盘版下载
- 单机|111MB
- 一款非常好玩的格斗游戏
-
-
下载
- |
-
-
下载
- 《暗黑破坏神3》免安装繁体中文正式版下载
- 单机|7630 MB
- 一款以角色扮演为主题的游戏
-
-
下载
- 《马克思佩恩3》免安装硬盘版下载
- 单机|27033 MB
- 一款以第三人称射击为主题的游戏