Thank you for your comment, Shlomi.
> 1. There's no such thing as IOScalar. Maybe you mean writing to an in-memory
> buffer.
Yes, in-memory buffer. That's what I meant.
> 2. Don't use threads in Perl. They cause too many problems.
Is there any workaround? to make multi-thread script without 'use threads'?
> 4. Please add "use warning;" to your code and correct all warnings.
There were warnings with the sample script I attached but I couldn't
correct it :-(
So I changed the idea, using in-memory buffer inside child sub-routine and take
result as return value.
This worked.
<<<
#!perl
use strict;
use warnings;
use threads ('yield',
'stack_size' => 64*4096,
'exit' => 'threads_only',
'stringify');
my @result;
my @thr;
for (my $i = 0; $i < 10; $i++) {
$thr[$i] = threads->create(
sub{my $i = shift; my $buf;
open my $fh, ">:scalar", \$buf;
print $fh "test$i";
close $fh;
return \$buf;
},
$i);
}
for (@thr) {push @result, $_->join();}
for (@result) {
print $$_, "\n";
}
Thanks again.
On Tue, 23 Mar 2010 15:12:02 +0200
Shlomi Fish <[email protected]> wrote:
> Hi iizuka!
>
> On Tuesday 23 Mar 2010 09:53:54 [email protected] wrote:
> > I'm trying to make threaded Net::Telnet program.
> > I want all "input_log" to be written to one file.
> > So I tried to give an IOScalar FileHandle to "input_log", then output real
> > file later. IOScalar works fine, threads works fine.
> > But together dosen't work.
> > Here is sample script.
> > Any suggestions appreciated.
> >
>
> A few notes:
>
> 1. There's no such thing as IOScalar. Maybe you mean writing to an in-memory
> buffer.
>
> 2. Don't use threads in Perl. They cause too many problems.
>
> 3. Don't use the FileHandle module. It is deprecated. Look at lexical
> filehandles and IO::Handle instead.
>
> 4. Please add "use warning;" to your code and correct all warnings.
>
> Regards,
>
> Shlomi Fish
>
> > # works fine without threads
> > use strict;
> > use FileHandle;
> >
> > my @result;
> > for (my $i = 0; $i < 10; $i++) {
> > my $fh = new FileHandle \($result[$i]), '>:scalar';
> > $fh->print("test$i");
> > $fh->close();
> > }
> > for (@result) {
> > print $_, "\n";
> > }
> > -----
> > # no output with threads
> > use strict;
> > use FileHandle;
> > use threads ('yield',
> > 'stack_size' => 64*4096,
> > 'exit' => 'threads_only',
> > 'stringify');
> >
> > my @result;
> > my @thr;
> > for (my $i = 0; $i < 10; $i++) {
> > my $fh = new FileHandle \($result[$i]), '>:scalar';
> > $thr[$i] = threads->create(sub{$_[0]->print($_[1]);$_[0]->close()},
> > $fh,
> > "test$i");
> > }
> > for (@thr) {my $dummy = $_->join();}
> > for (@result) {
> > print $_, "\n";
> > }
>
> --
> -----------------------------------------------------------------
> Shlomi Fish http://www.shlomifish.org/
> Why I Love Perl - http://shlom.in/joy-of-perl
>
> Deletionists delete Wikipedia articles that they consider lame.
> Chuck Norris deletes deletionists whom he considers lame.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/