in reply to Re: Strange substr Problem
in thread Strange substr Problem
Glad you found your problem, but a note of caution. If your ever going to upgrade from 5.005 to a newer version of Perl, and if there is any chance that any of your data will contain utf-8 characters, substr might cause you problems in that it deals in characters not bytes.
You might consider using unpack 'a100' or use bytes to avoid problems as and when you upgrade. A snippet to demonstrate the potential problem and both solutions.
#! perl -slw use strict; use 5.008; my $utfdata = join'',map{ chr(0xffff + $_) } 1 .. 100; print length $utfdata; #! Gives 100 my @bytes = unpack 'C*', $utfdata; print scalar @bytes; #! Gives 400 my $chunk = substr $utfdata, 0, 100; print length $chunk; #! Gives 100 @bytes = unpack 'C*', $chunk; print scalar @bytes; #! Gives 400 { use bytes; print length $utfdata; #! Gives 400 print length $chunk; #! Gives 400 } my @chunks = unpack '(a100)*', $utfdata; print scalar @chunks; #! Gives 4 print "@{[ map{length} @chunks ]}"; #! Gives 100 100 100 100
|
|---|