Positioning inside a File in Perl


You can use to tell function in Perl to know the current position of a file and seek function to point a particular position inside the file.

Perl tell Function

The first requirement is to find your position within a file, which you do using the tell function −

tell FILEHANDLE
tell

This returns the position of the file pointer, in bytes, within FILEHANDLE if specified, or the current default selected filehandle if none is specified.

Perl seek Function

The seek function positions the file pointer to the specified number of bytes within a file −

seek FILEHANDLE, POSITION, WHENCE

The function uses the fseek system function, and you have the same ability to position relative to three different points: the start, the end, and the current position. You do this by specifying a value for WHENCE.

Zero sets the positioning relative to the start of the file. For example, the line sets the file pointer to the 256th byte in the file.

seek DATA, 256, 0;

Updated on: 29-Nov-2019

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements