On Thu, 21 Feb 2002 01:11:01 +1100
"Sisyphus" <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> Ok - so I'm running the code below and it's working as I want - unless
> either of the 2 values being written to the file is 10. (ie unless $num = 8
> or 10).
> 
> If the value is 10, then I get a couple of warnings about 'use of
> uninitialised value'.
> '10' is the only value I've found that exhibits this disgraceful behaviour.
> 
> Ummmm ...... I'm a little hard pressed to make sense of that ........
> something to do with the newline characters or the chomping perhaps ?
> 
> use warnings;
> my $file = "try";
> my $num = 111111112;
> open (WRITEB, ">$file")
>         or die "Can't open WRITEB: $!";
> binmode WRITEB;
> print WRITEB pack("I",$num,), "\n";

if $num==10 you are really printing "\n\0\0\0\n" to the file on a little
endian machine like a PC. pack( "I", 10 ) gives "\n\0\0\0". The least
significant byte is 10 which is the ASCII newline character.

> print WRITEB pack("I",$num + 2), "\n";
> close (WRITEB)
>          or die "Can't close WRITEB: $!";
> 
> open (READ, "$file")
>         or die "Can't open READ: $!";
> binmode READ;
> while (<READ>) {

Here you want to read one line at a time. If $num was 10 you will get 2
lines. The first one will be "\n" the 2nd "\0\0\0\n".

> chomp;

chomp makes "" from "\n".

> $ret = unpack("I", $_);

unpack( "I", "" ) yields undef.

> print $ret, "\n";

and here you get your warning

> }
> close (READ)
>          or die "Can't close READ: $!";
> 

In generally do not mix binary and line oriented IO. In your example you
can 
  print WRITEB pack("I",$num,);
without the newline and then use read or sysread:
  read READ, $_, length( pack( "I", 0 ) );

> Cheers,
> Rob
> 
> 
> _______________________________________________
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to