while (<>) { # ... process a chunk of input ... # ... print result ... } #### 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 $!; } } #### use strict; use warnings; use Iterator::Diamond; my $iterator = Iterator::Diamond->new(magic => 'stdin'); while ($iterator->has_next) { my $line = $iterator->next; # ... process a chunk of input ... # ... print result ... }