Re: Catching binary ouput

2002-06-22 Thread Carl Jolley

On Mon, 17 Jun 2002, Sisyphus wrote:

 Hi,

 I'm running:
 my $out = `perl try.pl`;
 print length($out), \n;

 'try.pl'  is outputting a random 256 bit binary string to stdout. It
 contains the following code:


 my @try = ();

 while (scalar(@try)  256)
   {push @try, int(rand(2))}

 my $vector = join '', @try;

 my $pack = pack(b*, $vector);

 if (length($pack) != 32) {print ERROR}#never happens

 else {print $pack}

 Why would '$out' not always contain the entire 256 bits ? The length of
 '$out' is mostly, but not always, reported as 32.
 (I expect that it has something to do with the affect of printing backspaces
 etc.)
 What measure could one take to ensure that it does always capture the full
 256 bits (without altering the code in 'try.pl') ?

 I tried 'binmode STDOUT;' before running the system command, but that didn't
 work.

 Snookered ?  or is there something I can do ?


Did you try doing binmode STDOUT in try.pl?

 [EMAIL PROTECTED] Carl Jolley
 All opinions are my own and not necessarily those of my employer 

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



Re: Catching binary ouput

2002-06-18 Thread Sisyphus


- Original Message -
From: $Bill Luebkert [EMAIL PROTECTED]

 
  I *have* had runs of 50 32's, but often there's a sprinkling of 33's
  throughout. (Try about 500 iterations and there should be at least one
33 ).

 I'm not seeing that.

Wonder why that is ? Are you running the code for 'try.pl' as I originally
posted ? (From what you posted it looks like you are.)


 Here is one that outputs 1000 lines to a file
 and then when you give it an arg, it checks the lines in the file.

 I get no lines with other than 33 chars.  I added a newline and
 prevented embedded newlines just for the test - making each line 33.

 use strict;

 if (@ARGV) {

 open IN, 'out.txt';
 binmode IN;
 while (IN) {
 my $len = length $_;
 print $len, \n if $len != 33;
 }
 close IN;
 exit;
 }

 open OUT, out.txt or die;
 binmode OUT;
 for (1 .. 1000) {

 my @try = ();

 while (scalar (@try)  256) {
 push @try, int rand (2);
 }
 my $vector = join '', @try;
 my $pack = pack 'b*', $vector;

 if (length ($pack) != 32) {
 print ERROR\n;
 } else {
 $pack =~ s/\x0A/\*/g; # \n = '*' to prevent short lines
 print OUT $pack, \n;
 }
 }
 close OUT;

 __END__


I'll try that out - though implementation of the idea relies on an
alteration to 'try.pl' (which means it's outside the terms of reference :-)

Thanks for the 'O_BINARY' reference. I did a google search for it and turned
up some code that looks like it should be binmoding stdout - judging by the
comments attached to the code and the fact that it compiles ok. I'm still
not always catching the expected number of bits - but I really don't know if
that's because stdout is *not* binmoded, or because of some bug in the code.
The executable is not behaving as it should - which is what got me curious
in the first place.

The lesson is:
*never* try to probe C code when the tailor-made print function that comes
with its library won't even work. (It gave Dr.Watson quite a workout.)

Cheers,
Rob

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs