views:

138

answers:

2

Hi Folks,

this call

my $th = threads->create(\&print, "Hello thread World!\n");
$th->join();

works fine. But as soon as I add

binmode(STDOUT, ":encoding(ISO-8859-1)");

to my script file, I get an error like "segmentation fault", "access denied".

What is wrong to define an encoding type when trying to call a perl thread?

Example:

use strict; use warnings;
use threads;

binmode(STDOUT, ":encoding(ISO-8859-1)");

my $th = threads->create(\&print, "Hello thread World!\n");
$th->join();

sub print {
    print @_;
}

This code does not work for me.

Kind Regards

--Andy

+2  A: 

First, note that having a subroutine print with the same name as a built-in function is likely to cause a lot of confusion (if not to perl definitely to you or anyone who needs to read your code).

Second, I do not observe the problem with:

#!/usr/bin/perl

use strict; use warnings;
use threads;

my $th = threads->create(\&print, "Hello thread World!\n");
$th->join();

sub print {
    binmode(STDOUT, ":encoding(ISO-8859-1)");
    print @_;
}

Output:

C:\Temp> t
Hello thread World!

However, since STDOUT is a package variable, I would not recommend doing anything like this.

You should post a short but complete script that exhibits the problem rather than bits and pieces and also describe what you are trying to do rather than just the mechanical steps.

Sinan Ünür
My bad, the example with print was confusing. calling binmode within a sub does actually work. It doesn't work if I call binmode globally and then try to create a thread. I updated my post with an example.
jAndy
@jAndy I get a segmentation fault with `binmode` moved to top level as well. I am not sure I have a quick solution right now and I have to do some other things. I am sure someone else will provide further insight while I am away.
Sinan Ünür
+3  A: 

This was reported as bug in Perl's bug tracker. I have same failure on 5.12 RC0 on Windows.

Alexandr Ciornii