tags:

views:

83

answers:

3

Is it possible to do these things in Perl?

  • truncate a file if already exists, and
  • create,and
  • write, and
  • moving the cursor to end for append
+2  A: 

Use the ">" mode for open:

# the file is truncated and opened for output, being created if necessary.
open my $fh, '>', "file" or die $!;

# output to the file
print $fh "Line1\n";
print $fh "Line2\n";
eugene y
+2  A: 

yes see seek,

seek FILEHANDLE,POSITION,WHENCE

Sets FILEHANDLE's position, just like the fseek call of stdio . FILEHANDLE may be an expression whose value gives the name of the filehandle. The values for WHENCE are 0 to set the new position in bytes to POSITION, 1 to set it to the current position plus POSITION, and 2 to set it to EOF plus POSITION (typically negative). For WHENCE you may use the constants SEEK_SET , SEEK_CUR , and SEEK_END (start of the file, current position, end of the file) from the Fcntl module. Returns 1 on success, 0 otherwise.

Nikhil Jain
+2  A: 

It's all in perlfunc. Perl mostly uses the same stuff that C has.

  • open opens files in various ways.
  • truncate truncates files.
  • seek moves around in files.
brian d foy