Perl | eof - End of File Function Last Updated : 17 Dec, 2019 Comments Improve Suggest changes Like Article Like Report The eof() function is used to check if the End Of File (EOF) is reached. It returns 1 if EOF is reached or if the FileHandle is not open and undef in all other cases. Syntax: eof(FileHandle) Parameter: FileHandle: used to open the file Returns: 1 if EOF is reached Cases: eof(FileHandle) : Passing FileHandle to eof() function. If File is empty then it returns 1 otherwise undef. Example: Perl #!/usr/bin/perl # Opening Hello.txt file open(fh,"<Hello.txt"); # Checking if File is Empty or not if(eof(fh)) # Returns 1 if file is empty # i.e. EOF encountered at the beginning { print("End Of File\n"); } # Closing the File close(fh); # Checking if File is closed or not # using eof() function if(eof(fh)) # fh is a closed file # and hence, eof returns 1 { print("File is closed"); } Output : If Hello.txt is empty: If ex1.txt is not empty: eof() : The eof with empty parentheses refers to pseudo file formed from the files passed as command line arguments and is accessed via the '<>' operator. eof() checks for the end of the last file of all the files passed as arguments in the command line. Example: Perl #!/usr/bin/perl # opens filehandle for files passed as arguments while(<>) { # checks for eof of the last file passed as argument if(eof()) # It returns 1 if End Of the File is reached. { print "$_"; print("\nEnd Of File Reached"); } else # prints each fileread of the File { print "$_"; } } Output : eof : eof with no parentheses checks for the End Of File of the last file read. Example: Perl #!/usr/bin/perl # opening Hello.plx if(!open(fh, "<Hello.txt")) { print("File Not Found"); exit; } if(eof fh) { print("Empty File"); exit; } # check for End Of File of last file read i.e. fh if(not eof) # Returns 1 since eof is not reached { print("End Of File Not Reached"); } # Empty while loop to reach to the End Of File while(<fh>) { }; # check for End Of File of last file read i.e. fh if(eof) # Returns 1 since eof is reached { print("\nEnd Of File Reached"); } Output : Comment More infoAdvertise with us Next Article Perl | eof - End of File Function K KoushikMasavarapu Follow Improve Article Tags : Perl Similar Reads Perl | File I/O Functions File handling in Perl is used to read data from an external file or to write data into an external file. This is very useful as it provides a platform to permanently store and retrieve data from files. File Handle A FileHandle associates a name to an external file, that can be used until the end of 4 min read Perl | getc Function getc() function in Perl is used to read the next character from the file whose File Handle is passed to it as argument. If no FileHandle is passed then it takes one character as input from the user. Syntax: getc(FileHandle) Parameter: FileHandle: of the file to be read Returns: the character read fr 1 min read Perl | Useful File-handling functions Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. These operations can be performed by the use of various inbuilt file functions. Example: Perl #!/usr/bin/perl # Opening a 2 min read Vim - Go to End of File In this article you will get to know many ways to go to the end of any file opened using Vim (Vi Improved) which is a open source text editor. You will get to know what vim is and many shortcuts and key combinations in the entire article to navigate to the end of the file and after that, you can dec 5 min read Perl | tell() Function tell() function in Perl is used to get the position of the read pointer in a File with the use of its FileHandle. If no FileHandle is passed then it returns the position within the most recent accessed file. Syntax: tell(FileHandle) Parameter: FileHandle: Filehandle of the file to be accessed. Retur 1 min read Like