[Israel.pm] print to 2 places at the same time

Offer Kaye oferk at oren.co.il
Wed Jun 2 01:43:11 PDT 2004


>
> Well after the open line you have 2 concurrent processes writting

concurrent? How? First I print to both STDOUT and OUT, later I print to
STDOUT alone, finally I print to both STDOUT and OUT. Nothing concurrent
here...

>
> One trick is to add this line after your open line:
>
>   open STDOUT, ">&", \*OUT;
>   select OUT; $| = 1; select STDOUT; $| = 1;
>
> and this line before your close line:
>
>   close(STDOUT);
>
> There are other solutions too, but maybe this one is good enough.
>
> Regards,
> Mikhael.
>

Okay, now I have the code:

open(OUT,"| tee foo.txt") or die "tee foo no good: $!\n";
open STDOUT, ">&", \*OUT;
select OUT; $| = 1; select STDOUT; $| = 1;
print OUT "foobar\n";
print "AAAAAAAAARRRRRRRRRGGGGGGGGG\n";
print OUT "barfoo\n";
close(OUT) or die "wtf?\n";
close(STDOUT) or die "wtf2?\n";

But while it does the job, it hangs at the end, so that I have to do "^c" to
stop it. This is not good.
BTW, I would appreciate an explanation of what the second open does...

Thanks,
----------------------------------
Offer Kaye




More information about the Perl mailing list