4

I Have The following block in the beginning of my script:

#!/usr/bin/perl5 -w
use strict;
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");

In some subroutines when there is other encoding(from a distant subroutine), the data will not display correctly, when receiving cyrillic or other characters. It is the "binmode", that causes the problem.

Can I "turn off" the binmode utf8 locally, for the subroutine only?

I can't remove the global binmode setting and I can't change the distant encoding.

4
  • 2
    Backwards. In cases where you output a mix of text and bytes, don't add :utf8 in the first place. Encode text into bytes manually so that you only output bytes. Much simpler.
    – ikegami
    Jan 6, 2015 at 16:30
  • The problem is that I can't remove the ":utf8", Otherwise that would've solved my problem. Jan 7, 2015 at 8:29
  • Sure you can. Use the delete key to remove the binmode statements
    – ikegami
    Jan 7, 2015 at 11:25
  • I meant I am not allowed to do it, since I am only working on a small part of the module. Jan 7, 2015 at 17:01

3 Answers 3

7

One way to achieve this is to "dup" the STD handle, set the duplicated filehandle to use the :raw layer, and assign it to a local version of the STD handle. For example, the following code

binmode(STDOUT, ':utf8');
print(join(', ', PerlIO::get_layers(STDOUT)), "\n");

{
    open(my $duped, '>&', STDOUT);
    # The ':raw' argument could also be omitted.
    binmode($duped, ':raw');
    local *STDOUT = $duped;
    print(join(', ', PerlIO::get_layers(STDOUT)), "\n");
    close($duped);
}

print(join(', ', PerlIO::get_layers(STDOUT)), "\n");

prints

unix, perlio, utf8
unix, perlio
unix, perlio, utf8

on my system.

1
  • This didn't work for me using ActiveState Perl 5.20 on Windows. Nothing I tried would remove the utf8 layer from STDOUT or a dupe of it once utf8 had been added by 'use open' or a previous binmode call. Feb 18, 2022 at 3:27
3

I like @nwellnhof's approach. Dealing only with Unicode and ASCII - a luxury few enjoy - my instinct would be to leave the bytes as is and selectively make use of Encode to decode()/encode() when needed. If you are able to determine which of your data sources are problematic you could filter/insert decode when dealing with them.

% file koi8r.txt 
koi8r.txt: ISO-8859 text
% cat koi8r.txt 
������ �� ����� � ������� ���. ���
���� ����� ������ ����� �����.
% perl -CO -MEncode="encode,decode" -E 'say decode("koi8-r", <>) ;' koi8r.txt
Американские суда находятся в международных водах. Япония
2
  • Shouldn't that last line be: perl -CO -MEncode="encode,decode" -E 'print decode("koi8-r", <>) ;' koi8r.txt ? (print and koi8r.txt) Aug 8, 2019 at 9:05
  • @peter-v-mørch - yes it should :-)
    – G. Cito
    Nov 28, 2019 at 6:03
0

You could use something like Scope::Guard - lexically-scoped resource management to ensure it gets set back to :utf8 when you leave the scope, regardless of how (return, die, whatever):

#!/usr/bin/perl -w
use strict;

use Scope::Guard qw(guard);

binmode(STDOUT, ':utf8');
print(join(', ', PerlIO::get_layers(STDOUT)), "\n");

{
    # When guard goes out of scope, this sub is guaranteed to be called:
    my $guard = guard {
        binmode(STDOUT, ':utf8');
    };
    binmode(STDOUT, ':raw');
    print(join(', ', PerlIO::get_layers(STDOUT)), "\n");
}

print(join(', ', PerlIO::get_layers(STDOUT)), "\n");

Or, if you don't want to include a new dependency like Scope::Guard (Scope::Guard is awesome for this kind of localizing...):

#!/usr/bin/perl -w
use strict;

binmode(STDOUT, ':utf8');
print(join(', ', PerlIO::get_layers(STDOUT)), "\n");

{
    my $guard = PoorMansGuard->new(sub {
        binmode(STDOUT, ':utf8');
    });
    binmode(STDOUT, ':raw');
    print(join(', ', PerlIO::get_layers(STDOUT)), "\n");
}

print(join(', ', PerlIO::get_layers(STDOUT)), "\n");

package PoorMansGuard;

sub new {
    my ($class, $sub) = @_;
    bless { sub => $sub }, $class;
}

sub DESTROY {
    my ($self) = @_;
    $self->{sub}->();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.