Nano HE
Nano HE

Reputation: 1971

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 > work here.

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

Upvotes: 1

Views: 137

Answers (3)

FMc
FMc

Reputation: 42411

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 $!;

Upvotes: 7

Quick Joe Smith
Quick Joe Smith

Reputation: 8222

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.

Upvotes: 3

geekosaur
geekosaur

Reputation: 61369

> 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.)

Upvotes: 1

Related Questions