use strict; use warnings; my $input_layers = ':encoding(utf-8)'; if (!eof()) { binmode ARGV, $input_layers or die $!; } while (<>) { # ... process a chunk of input ... # ... print result ... if (eof && !eof()) { binmode ARGV, $input_layers or die $!; } }