fert has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm hoping somebody can shed some light on what is going on here. The quick and dirty reduced examples. Say we want to print a UTF8 character inside a function, to avoid warnings we must explicitly call binmode like this:

(in Perl v5.8.8)

my $char = "\x{30DE}\x{30A4}\x{30AF}"; binmode STDOUT, ":utf8"; out(); sub out { print "$char"; }

Works fine, prints out the character.

But as soon as we thread things up...

use threads; my $char = "\x{30DE}\x{30A4}\x{30AF}"; binmode STDOUT, ":utf8"; my $thr = threads->create(\&foo); $thr->join; sub foo { print "$char"; }

We get the "wide character print".

Is this known/expected behavior of a FileHandle? I can make the message go away with another binmode inside of the function, but since I'm getting this error out of Log4Perl changing the inards is something I am hoping to avoid. The bigger picture if you are interested. I am using Log4Perl with syswrite turned on to try and allow multiple threads to write to a single log file. Because of the threading(I think) anytime a utf8 character goes through the Appender (File), the utf8 bit is set to true, but only gets binmoded on creation of the Appender, not in what I am guessing is the copies that the thread has created. The file handle is no longer set to be utf8 so it dies, which causes the whole thread to die.

So the warning thrown to the Appender causes it to call a die, and subsequently my thread to die. I've put in a binmode to the source and it seems to work but am hesitant to assume I've fixed it completely.

I'm not sure if this is something the Log4Perl should be aware of or need to fix or if its something I am doing wrong, any suggestions?

Replies are listed 'Best First'.
Re: Filehandles, threads, utf8 reset?
by ikegami (Patriarch) on Apr 20, 2010 at 23:21 UTC

    Hi Mike, (or so I'm guessing)

    No warning here either.

    $ perl -v This is perl, v5.10.0 built for i486-linux-gnu-thread-multi [snip] $ perl -w a.pl マイク
      No, just a UTF-8 string I picked up from some other example :p Might be that 5.10 handles threads in this situation better. I have a fix in place at the moment but I will consider upgrading. Thanks for trying out the code.

        I get the warning with ActiveState 5.8.0, but I don't get the warning with ActivePerl 5.8.8 build 824. That said, AS includes patches, but it's a sign that upgrading 5.8.9 would be sufficient to fix the problem. That will save you from reinstalling all your modules.

Re: Filehandles, threads, utf8 reset?
by choroba (Cardinal) on Apr 20, 2010 at 22:26 UTC
    The threaded code works for me, prints マイク.