open a file in Perl
Open for reading:
open my $fh, '<:encoding(utf8)', $filename or die "Could not open '$filename' $!"
Open for writing:
open my $fh, '>:encoding(utf8)', $filename or die "Could not open '$filename' $!"
Open to append:
open my $fh, '>>:encoding(utf8)', $filename or die "Could not open '$filename' $!"
- Open and read from file
- Always use 3-argument open
- Never the old way

Published on 2021-04-07
Comments
In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation. comments powered by Disqus
If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.
Comment on this post