in reply to perlpacktut "packing text" example and Unicode
The entire line is being from a UTF-8 file that was correctly read by using open(my $in, '<:utf8', $file)
You've already "unpacked" the bytes (= 8 bit chunks), which contain integers, into UTF-8 characters after doing that.
unpack() is for reading "raw" bytes when you know ahead of time how those raw bytes are laid out in the file. A raw byte is one that has not undergone a translation (:utf8 applies a translation). First, you need to realize that a file contains only integers. A computer can only store numbers--not characters. So characters are represented by integer codes. But if you encounter the integer 120 in a file, how do you know whether that should be the id of a customer(i.e. the actual integer) or the ascii code for the letter 'x'?
A byte is an 8 bit chunk of memory that is used to store an integer. unpack() allows you to tell perl exactly what each byte in a file should represent. For instance, you can tell perl that the first 4 bytes represent one integer, the next byte is an integer which represents the ascii code for a character, followed by an undetermined number of bytes which is the UTF-8 integer code for a character, the next 8 bytes after that represent one integer, etc.
A file contains only integers, and each integer occupies 1 byte(=8 bits). Furthermore, you can tell perl how to interpret the integers it encounters. If you want to read raw bytes from a file so that you can tell perl exactly how to interpret each byte, you can do that.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: perlpacktut "packing text" example and Unicode
by glasswalk3r (Friar) on Jul 14, 2011 at 17:03 UTC |