Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Perl's IO::File equivalent to open($fh, ">:utf8",$path)?

Tags:

utf-8

perl

It's possible to white a file utf-8 encoded as follows:

open my $fh,">:utf8","/some/path" or die $!;

How do I get the same result with IO::File, preferably in 1 line? I got this one, but does it do the same and can it be done in just 1 line?

my $fh_out = IO::File->new($target_file, 'w');
$fh_out->binmode(':utf8');

For reference, the script starts as follows:

use 5.020;
use strict;
use warnings;
use utf8;
# code here
like image 372
capfan Avatar asked Feb 10 '17 17:02

capfan


1 Answers

Yes, you can do it in one line.

open accepts one, two or three parameters. With one parameter, it is just a front end for the built-in open function. With two or three parameters, the first parameter is a filename that may include whitespace or other special characters, and the second parameter is the open mode, optionally followed by a file permission value.

[...]

If IO::File::open is given a mode that includes the : character, it passes all the three arguments to the three-argument open operator.

So you just do this.

my $fh_out = IO::File->new('/some/path', '>:utf8');

It is the same as your first open line because it gets passed through.

like image 152
simbabque Avatar answered Oct 11 '22 10:10

simbabque