in reply to Re: Match full utf-8 characters
in thread Match full utf-8 characters

Thanks for shedding light on this. I'm actually planning to use this in a script, so I see now I need to have:
binmode(STDIN, ':utf8'); binmode(STDOUT, ':utf8');
at the top of my script.

Replies are listed 'Best First'.
Re^3: Match full utf-8 characters
by Allasso (Monk) on Apr 29, 2019 at 13:36 UTC
    ...Although, I find this works when I collect user input with the construct:
    my $user_input = <STDIN>;
    but does not work when I collect it with:
    my $stdin = new IO::Handle; $stdin->fdopen( fileno( STDIN ), "r" ) || die "Cannot open STDIN"; while ( my $char = $stdin->getc() ) { }
    and I need to use hippo's suggestion below. In each iteration, $char is a byte. Is there a way to coerce $char to be utf8?

      That's a very convoluted way of achieving something that's already done for you:

      $ perl -E'say STDIN->can("getc")' $ perl -MIO::Handle -E'say STDIN->can("getc")' CODE(0x55dddd02b150)
      I.e. if IO::Handle is loaded, STDIN is already an object resembling IO::Handle and you can call getc method on it.

      But for the sake of the exercise, you should be able to pass "<:utf8" instead of "r" to fdopen and have getc return Unicode characterscode points again. (untested)

        You're probably way above my head here, but when using STDIN->can("getc") it does not wait for user input, which is what I am doing with my construct.

        Also, attempting to use "<:utf8" instead of "r" gives IO::Handle: bad open mode... error.

        I also tried moving binmode(STDIN, ':utf8') call to after opening the filehandle as recommended here: https://perldoc.perl.org/perlfunc.html#binmode-FILEHANDLE%2c-LAYER -- but that did not help.