Chris
Chris

Reputation: 729

Cannot write when specifying encoding on open

Perhaps I am misunderstanding encodings etc, but whenever I specify an encoding on opening an expression under a writeable mode, printing to the handle appears to have no effect.

Code:

my $string = "one\n";

#open my $handle, '>>', \$string or die "cannot open: $!";
open my $handle, '>>:encoding(UTF-8)', \$string or die "cannot open: $!";

print $handle "two\n";

print $string;

Output:

one

Expected Output:

one
two

Could somebody please explain why this is so.

Thanks,

Chris

Upvotes: 2

Views: 75

Answers (1)

ikegami
ikegami

Reputation: 385789

You are suffering from buffering. Add

use IO::Handle qw( );
$handle->autoflush(1);

or close the handle before reading from the buffer.

Upvotes: 5

Related Questions