Perl | eof – End of File Function
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:
open (fh, "<Hello.txt" );
if ( eof (fh))
{
print ( "End Of File\n" );
}
close (fh);
if ( eof (fh))
{
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:
while (<>)
{
if ( eof ())
{
print "$_" ;
print ( "\nEnd Of File Reached" );
}
else
{
print "$_" ;
}
}
|
Output :
- eof : eof with no parentheses checks for the End Of File of the last file read.
Example:
if (! open (fh, "<Hello.txt" ))
{
print ( "File Not Found" );
exit ;
}
if ( eof fh)
{
print ( "Empty File" );
exit ;
}
if (not eof )
{
print ( "End Of File Not Reached" );
}
while (<fh>)
{ };
if ( eof )
{
print ( "\nEnd Of File Reached" );
}
|
Output :
Last Updated :
17 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...