Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux perl脚本中的读写操作_Linux_Perl - Fatal编程技术网

Linux perl脚本中的读写操作

Linux perl脚本中的读写操作,linux,perl,Linux,Perl,我是Perl脚本的新手 我想对文件执行读写操作。我将以读写模式打开文件(+问题是您的文件句柄位置位于写入的行之后。在再次读取之前,请使用该函数将“光标”移回顶部 例如,带有一些额外注释: #!/usr/bin/env perl # use some recommended safeguards use strict; use warnings; my $filename = 'file.txt'; `touch $filename`; # use indirect filehandle,

我是Perl脚本的新手


我想对文件执行读写操作。我将以读写模式打开文件(+问题是您的文件句柄位置位于写入的行之后。在再次读取之前,请使用该函数将“光标”移回顶部

例如,带有一些额外注释:

#!/usr/bin/env perl

# use some recommended safeguards
use strict;
use warnings;

my $filename = 'file.txt';
`touch $filename`;

# use indirect filehandle, and 3 argument form of open
open (my $handle, "+<", $filename) or die "Can't open file $filename : $!";
# btw good job on checking open sucess!

print $handle "Hello, welcome to File handling operations in perl\n";

# seek back to the top of the file
seek $handle, 0, 0;

my $line = <$handle>;

print "$line\n";

这似乎是一个常见的初学者错误。大多数情况下,你会发现读写同一个文件虽然可能,但不值得费心。正如Joel Berger所说,你可以
查找到文件的开头。你也可以简单地重新打开文件。查找不像逐行读取那样简单,而且会显示你有困难

另外,您应该注意,不需要事先创建空文件。只需执行以下操作:

open my $fh, ">", "file.txt" or die $!;
print $fh "Hello\n";
open $fh, "<", "file.txt" or die $!;
print <$fh>;

谢谢TLP。我有一个问题。我可以根据需要以不同的模式多次重新打开文件吗,例如在while循环中?这会导致任何困难吗?当你重新打开文件句柄时,旧的文件句柄将关闭。它不会导致任何困难,除非你创建一些。:)如果你有特定的问题要解决,你应该询问这个问题,如果你使用更多的I/O操作,它将根据定义导致更多的I/O操作,是的。但是,这不是问题,除非您有大量的操作,否则您不会发现任何巨大的性能损失。但在这种情况下,您可能会想到其他方法来优化代码。这可能更安全,尤其是如果您只想“切换模式”几次。当然,我仍然认为
seek
从技术上讲是原始问题的正确答案:-)@JoelBerger是的,但正确的答案并不总是正确的解决方案=对一个乔感兴趣。我将在我的脚本中使用这个。但是我只需要将它保存在一个文件中并读取相同的内容,而无需使用seek并通过切换模式重新打开文件。我只想知道这一点。如果我将大量数据存储到文件中,Tie::File是否会影响我的脚本。
Tie::File
只会为您搜索,这一切都是秘密的。由于它仍然是文件io,数据的大小不应该太重要,除非它必须搜索换行标记。除非你有比这更好的机制,否则它和你搜索某条线是一样的。
#!/usr/bin/env perl

# use some recommended safeguards
use strict;
use warnings;

my $filename = 'file.txt';
`touch $filename`;

# use indirect filehandle, and 3 argument form of open
open (my $handle, "+<", $filename) or die "Can't open file $filename : $!";
# btw good job on checking open sucess!

print $handle "Hello, welcome to File handling operations in perl\n";

# seek back to the top of the file
seek $handle, 0, 0;

my $line = <$handle>;

print "$line\n";
#!/usr/bin/env perl

# use some recommended safeguards
use strict;
use warnings;

use Tie::File;

my $filename = 'file.txt';
tie my @file, 'Tie::File', $filename
  or die "Can't open/tie file $filename : $!";

# note file not emptied if it already exists

push @file, "Hello, welcome to File handling operations in perl";
push @file, "Some more stuff";

print "$file[0]\n";
open my $fh, ">", "file.txt" or die $!;
print $fh "Hello\n";
open $fh, "<", "file.txt" or die $!;
print <$fh>;
print scalar <$fh>;  # put <$fh> in scalar context