perl 通过<<和文件句柄将数据写入到文件中去

2022-12-30,,,,

可以通过文件句柄和<<运算符将文件内容写入到文件中去

#!usr/bin/perl -W

use strict;
use Spreadsheet::ParseExcel; use utf8;
#引入utf8模块 脚本内的字符串使用utf8作为编码格式
binmode(STDOUT,":encoding(gbk)");
#标准输出使用gbk作为编码格式,也可以把gbk改为gb2312
binmode(STDIN,":encoding(gbk)");
#如果涉及到输入流,例如读文件,不加这条读中文文件会出现乱码
binmode(STDERR,":encoding(gbk)");
#如果异常错误中的字符串有中文,请加上STDERR,否则也会出现乱码 #以写的方式打开文件,如果没有该文件,尝试创建该文件,如果文件存在,那么文件内容被清空
if(open(data, ">test1.txt"))
{
print "以写的方式打开文件成功\n";
#写入数据
print data <<IT1;
#region Auto Generated
using System.Collections.Generic; namespace Common.RTSystem
{
public static class InterlockDict
{
public static readonly Dictionary<IOID, List<IOID>> DOLIST = new Dictionary<IOID, List<IOID>>
{
IT1 print data <<IT2;
public static readonly List<ECID> BPSLIST = new List<ECID>
{
IT2 close(data);
}
else
{
die "test1.txt 文件无法打开, $!\n"; #没有该文件,提示:can't open perl script "operation.pl": no such file or directory
}

上面的IT1和IT2可以自定义,只要保持前后名称一致就可以

执行后在test1.txt中就有写入的数据,如下

#region Auto Generated

using System.Collections.Generic;



namespace Common.RTSystem

{

public static class InterlockDict

{

public static readonly Dictionary<IOID, List<IOID>> DOLIST = new Dictionary<IOID, List<IOID>>

{

public static readonly List<ECID> BPSLIST = new List<ECID>

{


这种方式可以用于向C#的.cs文件中写入代码段

#!usr/bin/perl -W
 
use strict;
use Spreadsheet::ParseExcel;
 
use utf8;
#引入utf8模块 脚本内的字符串使用utf8作为编码格式
binmode(STDOUT,":encoding(gbk)");
#标准输出使用gbk作为编码格式,也可以把gbk改为gb2312
binmode(STDIN,":encoding(gbk)");
#如果涉及到输入流,例如读文件,不加这条读中文文件会出现乱码
binmode(STDERR,":encoding(gbk)");
#如果异常错误中的字符串有中文,请加上STDERR,否则也会出现乱码
 
 
#以写的方式打开文件,如果没有该文件,尝试创建该文件,如果文件存在,那么文件内容被清空
if(open(data, ">test1.txt"))
{
   print "以写的方式打开文件成功\n";
   #写入数据
   print data <<IT1;
#region Auto Generated
using System.Collections.Generic;
 
namespace Common.RTSystem
{
    public static class InterlockDict
    {
        public static readonly Dictionary<IOID, List<IOID>> DOLIST = new Dictionary<IOID, List<IOID>>
        {
IT1
 
  print data <<IT2;
        public static readonly List<ECID> BPSLIST = new List<ECID>
        {
IT2
 
   close(data);
}
else
{  
   die "test1.txt 文件无法打开, $!\n";  #没有该文件,提示:can't open perl script "operation.pl": no such file or directory
}

perl 通过<<和文件句柄将数据写入到文件中去的相关教程结束。

《perl 通过<<和文件句柄将数据写入到文件中去.doc》

下载本文的Word格式文档,以方便收藏与打印。