in reply to Opening a file in Perl
Regardless of the difference in scoping between Python and Perl, the file handle $fh is closed whenever the variable goes out of scope. For more information on the subject, peruse open and perlopentut. Note the 3 parameter open idiom. open has default behavior, but there's been a move in recent years to be explicit and to also move way from bareword filehandles - i.e., my $fh versus FH.open my $fh, '<', 'welcome.txt';
Adding a die will allow you to invoke exception semantics. This can often be used as a way to avoid having to do a if file exists check with -e before attempting to open an existing file for reading, for example:
open my $fh, '<', 'welcome.txt' or die "couldn't open file for reading +!: $!";
* Scoping in Perl is generally easy to explain, but there's a lot to it. See documention on my, our, and perlsub as a good start.
|
|---|