Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between '>>' and '>' in Perl

What is the difference between these two code snippets?

  1. open (MYFILE, '>>data.txt');

  2. open (MYFILE, '>data.txt');

like image 781
Karandeep Singh Avatar asked Sep 14 '26 10:09

Karandeep Singh


1 Answers

  1. open (MYFILE, '>>data.txt') — Open data.txt, keep the original data, append data from the end.
  2. open (MYFILE, '>data.txt') — Open data.txt, delete everything inside, and write data from the start.

From perldoc -f open:

If MODE is '<' or nothing, the file is opened for input. If MODE is '>', the file is truncated and opened for output, being created if necessary. If MODE is '>>', the file is opened for appending, again being created if necessary.

It stems from the shell usage that,

  • cmd < file.txt to copy file into stdin,
  • cmd > file.txt to write stdout into a file, and
  • cmd >> file.txt to append stdout to the end of the file.
like image 194
kennytm Avatar answered Sep 17 '26 15:09

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!