What is filehandles (lexical) in Perl?

In Perl, lexical filehandles provide a way to create filehandles that are scoped to a block or a file. This means they are not global and can help prevent name clashes in larger programs. Lexical filehandles are created using the `<` and `>` operators, which are scoped to the surrounding block in which they are declared.

Using lexical filehandles can improve the readability and maintainability of the code, as they limit the scope of the filehandles to where they are needed.

# Use of lexical filehandles in Perl { open my $fh, '<', 'example.txt' or die "Can't open file: $!"; while (my $line = <$fh>) { print $line; } close $fh; } # The filehandle $fh is no longer accessible here

lexical filehandles Perl file handling programming