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

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)

Replies are listed 'Best First'.
Re^5: Match full utf-8 characters
by Allasso (Monk) on Apr 29, 2019 at 15:14 UTC

    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.

      STDIN->can("getc") being defined means that you can call the method: STDIN->getc().

      Sorry about giving you misleading information on the "mode" parameter of IO::Handle::fdopen. From my understanding of the documentation (For the documentation of the "open" method, see IO::File. ... If "IO::File::open" is given a mode that includes the ":" character, it passes all the three arguments to the three-argument "open" operator.), it should have worked.

      I also tried moving binmode(STDIN, ':utf8') ... but that did not help.

      That is because you don't use the STDIN file handle, but create another one with the UTF-8 decoding layer stripped. Try setting ":utf8" binmode on STDIN, then using calling STDIN->getc (or just getc, since STDIN is chosen by default) in a loop.

        Okay, I'm starting to see what you're talking about now. For example, if I set binmode($stdin, ':utf8'); to correspond with my construct of creating a new IO::Handle, it works.

        I'm also understanding STDIN->getc(), I think I was trying to use STDIN->can("getc") in its place before.

        It's all starting to make a little bit of sense to me now. They are doing the same thing, with almost the same code, only I can use the global(?) STDIN filehandle instead of creating a new one.

        I've removed the unnecessary creation of a new IO::Handle and fdopen, and am just using STDIN like you suggested. Everything is working like I wanted and is much cleaner now.

        Thanks!