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

What is the difference between these two pieces of code?

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

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

+3
source share
1 answer
  • open (MYFILE, '>>data.txt'). Open data.txt, save the source data and add data from the end.
  • open (MYFILE, '>data.txt'). Open data.txt, delete everything from the inside and write the data from the very beginning.

From perldoc -f open:

If MODE '<'or nothing, the file is opened for input. If MODE '>', the file is trimmed and opened for output, if necessary it is created. If MODE '>>', the file is opened for addition, and if necessary it is created.

This is due to the use of the shell,

  • cmd < file.txtto copy the file to stdin,
  • cmd > file.txt to write stdout to a file and
  • cmd >> file.txt, stdout .
+10

Source: https://habr.com/ru/post/1749169/


All Articles