perl几个文件操作例子(perl基本命令)全程干货

随心笔谈2年前发布 admin
225 0 0

文章摘要

这篇文章详细讨论了如何使用管道符(& lt;、& gt;、& gt;& gt; 和混合操作符)在文本文件之间进行各种操作。作者介绍了如何通过不同的管道符来实现读取、写入、追加以及混合操作,同时说明了这些操作符如何控制文件的创建、删除或覆盖。文章强调了这些操作符的灵活性和适用性,适用于需要对文本文件进行各种操作的场景。

#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”;

© 版权声明

相关文章