开发者

Open File method in Perl

I tested >> and > for Open destination file in my code below, it work well. What's the different for them?

my $sourfile = "ch1.txt";
my $destfile = "chapter1.txt";

open (SOURFILE, $sourfile);
open (DESTFILE, ">>$destfile"); #both >> and > wo开发者_开发百科rk here.

#my $fh = \*DATA;  
my $fh = \*SOURFILE;


The difference:

>    Open file for writing.
>>   Open file for appending.

You might want to switch to using the 3-argument form of open, and to using lexical variables as file handles:

open(my $handle, '>', "some_file") or die $!;


Apologies in advance for being terse, but open - perldoc. In fact, I would generalise my answer to: always try http://perldoc.perl.org first. Forums/Q&A sites are your last resort, not your first.


> creates, or truncates if it already exists. >> creates, or appends to an existing file. (And it's not a method; Perl 5 isn't really all that OO unless you squint.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜