perl几个文件操作例子
作者:junjie 发布时间:2022-03-23 09:24:09
标签:perl,文件操作
perl用的最多的地方就算是文件处理了,下面我就总结了一下perl文件操作的一些东西,并且有具体的例子,通过下面的例子,加强我们对perl文件操作的理解。
删除文件
使用unlinke函数,比如unlink $file, unlink $file1, $file2, $file3
打开文件
使用三参数的形式打开文件,这样非常便于区分模式和文件名,perl 5.6之后的版本都支持这种方式。
#Open the 'txt' file for reading
open FH, '<', "$file_name.txt" or die "Error:$!n"; #Open the 'txt' file for writing. Creates the #file_name if it doesn't already exist #and will delete/overwrite a pre-existing file of the same name open FH, '>', "$file_name.txt" or die "Error:$!n";
#Open the 'txt' file for appending. Creates the #file_name if it doesn't already exist
open FH, '>>', "$file_name.txt" or die "Error:$!n";
#Open the 'txt' file for a 'read/write'. #Will not create the file if it doesn't #already exist and will not delete/overwrite #a pre-existing file of the same name
open FH, '+<', "$file_name.txt" or die "Error:$!n"; #Open the 'txt' file for a 'read/write'. Will create #the file if it doesn't already exist and will #delete/overwrite a pre-existing file #of the same name open FH, '+>', "$file_name.txt" or die "Error:$!n";
#Open the 'txt' file for a 'read/append'. Will create #the file if it doesn't already exist and will #not delete/overwrite a pre-existing file #of the same name
open FH, '+>>', "$file_name.txt" or die "Error:$!n";
一次性读入整个文件
使用<>在标量环境下一次读入一行,而在列表环境下一次读入所有行,$/存储的是行分隔符,默认是换行符,我们先将$/改掉,这样就可 以在标量环境下一次读入所有行了(这时已经没有行的概念了,就是读入整个文件),你也可以用列表读入所有行然后再将所有行拼到一起,但那样速度很慢。用完记得将$/改回来。
#!/usr/bin/perl
use strict ;
use warnings ;
sub test{
open FILE, '<', "d:/code/test.txt" or die $! ;
my $olds = $/ ;
$/ = undef ;
my $slurp = ;
print $slurp, "n" ;
$/ = $olds ;
close FILE;
}
&test() ;
也可以使用local关键字来将$/设置为局部变量,这样跳出作用域后,$/又恢复了原来的值。
#!/usr/bin/perl
use strict ;
use warnings ;
sub test{
local $/ ; #??? local $/ = undef ;
open FILE, '<', "d:/code/zdd.txt" or die $! ;
my $slurp = ;
print $slurp, "n" ;
}
&test() ;
最好的方法是使用模块,这样比自己写安全,File::Slurp、IO::All都可以的。
打开文件请用双引号
open文件时,如果文件名有变量替换,最好用双引号而不是单引号,因为单引号无视变量内插。
open FILE "<$file" or die $! ; #这样可以。
open FILE '<$file' or die $! ; #这样就不可以,因为$file不会被解释成变量内插。同样<也不会被解释成输入
文件句柄作参数
假设有一个函数test,它有一个参数,是某个文件句柄,那么该如何传递这个参数呢?
方法一,传递参数时,在句柄前面加*
sub main {
open FILE, '+<', 'test.data' or die $!;
&test(*FILE);
close FILE;
}
方法二,使用open my $FILE的形式打开文件
sub main {
open my $FILE, '+<', 'test.data' or die $!;
&test($FILE);
close $FILE;
}


猜你喜欢
- 前言学python对selenium应该不陌生吧Selenium 是最广泛使用的开源 Web UI(用户界面)自动化测试套件之一。Selen
- 如下所示:#!/usr/bin/env python3# -*- coding: utf-8 -*-import sqlite3conn =
- 1.连接查询作用:当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回2、连接类型内连接定义:内连接查询:查询
- 一个很普通的网页中显示LOGO图像,按照以往的页面制作经验,基本是在页面中插入图像即可(<img src="logo.gif
- 字典树(Trie)可以保存一些字符串->值的对应关系。基本上,它跟 Java 的 HashMap 功能相同,都是 key-value
- 1、读文件import csvcsv_reader = csv.reader(open("data.file", enc
- PyCharm是一款很好用很流行的python编辑器。Anaconda是专注于数据分析的Python发行版本,包含了conda、Python
- 其中用到urllib2模块和正则表达式模块。下面直接上代码:[/code]#!/usr/bin/env python#-*- coding:
- 最近在学习django,学到第五章模型时,需要连接数据库,然后,在这里分享一下方法。起初是不知道怎样配置mysql数据库,但是还好,djan
- 前言用python编程绘图,其实非常简单。中学生、大学生、研究生都能通过这10篇教程从入门到精通!快速绘制几种简单的柱状图。1垂直柱图(普通
- 本文主要分享的是一则python+opencv实现任意角度的透视变换的实例,具体如下:# -*- coding:utf-8 -*-impor
- 第一种,使用reversed 函数,reversed返回的结果是一个反转的迭代器,我们需要对其进行 list 转换listNode = [1
- 首先在程序中引入Requests模块import requests一、获取不同类型的响应内容在发送请求后,服务器会返回一个响应内容,而且re
- 一、功能简述番茄钟即番茄工作法,番茄工作法是简单易行的时间管理工具,使用番茄工作法即一个番茄时间共30分钟,25分钟工作,5分钟休息;特点一
- 大家都知道当任务过多,任务量过大时如果想提高效率的一个最简单的方法就是用多线程去处理,比如爬取上万个网页中的特定数据,以及将爬取数据和清洗数
- 目录前言数据结构常规实现string转[]byte[]byte转string高效实现性能测试总结前言当我们使用go进行数据序列化或反序列化操
- 图像的二值化或阈值化(Binarization)旨在提取图像中的目标物体,将背景以及噪声区分开来。通常会设定一个阈值T,通过T将图像的像素划
- 本代码主要实现的是利用网络传输图片,用在我的树莓派项目之上。该项目在PC上运行服务端,树莓派上运行客户端,两者连接到同一局域网中,修改代码中
- 方法一、使用os模块的system方法os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0--表示she
- 装饰器是程序开发中经常会用到的一个功能,也是python语言开发的基础知识,如果能够在程序中合理的使用装饰器,不仅可以提高开发效率,而且可以