On Mon, Nov 24, 2008 at 07:24, loody <[EMAIL PROTECTED]> wrote: snip > My question are: > Q1: > what is the differences of using > "use bytes; use open IN => ":bytes";" > and "binmode "? snip
The bytes pragma changes how string functions work (char vs byte, some chars take up more than one byte). #!/usr/bin/perl use strict; use warnings; my $c = "\x{2603}"; #snowman print "without the byte pragma: ", length $c, "\n"; { use bytes; print "with the byte pragma: ", length $c, "\n"; } The open pragma affects all calls to open, binmode affects a specific filehandle. This means you can say use open IN => ":bytes", OUT => ":bytes"; and avoid having to call binmode for each filehandle. The open pragma is newer than the binmode function (which goes a long ways back) and it explicitly uses the new PerlIO layer. I can only assume binmode was rewritten to use it when the PerlIO layer got added, but I don't know for sure. snip > Q2: > Pack seems the reverse of unpack, but the value I write into file at > Q2 and Q4 are different. > Is the template I give wrong? snip You seem very confused about pack and unpack. The unpack function takes a format and string and returns a list: #each entry will be a number between 1 and 256 my @bytes = unpack "C*", $some_string; The pack function takes a format and list and turns the list into a string of bytes: my $same_as_some_string = pack "C*", @bytes; #!/usr/bin/perl use strict; use warnings; my $s = "abc"; printf "%02x\n", $_ for unpack "C*", $s; print pack("C*", unpack "C*", $s), "\n"; In Q4, you are saying foreach my $byte (pack "H*", $tmp_gold) { syswrite EXCERPT_GOLD, $byte; #Q4 } Which means take the string of bytes in $tmp_gold and treat it like a string of hex characters to pack into a string of bytes. So if $tmp_gold contained a string that had the bytes 0x36 and 0x31 (i.e. the string "61") the output would be the string "a". Also, the for loop is pointless since pack can only return one value. snip > Q3: > I found the foreach seems has no effect. > $byte will contain all the data in $tmp_gold which is unpack as hex string. snip No effect or the lack of the effect you desired? It should unpack the data as hex representations (i.e. two hex characters per byte) of the bytes. If you wanted the actual bytes you need to say "C*" not "H*". snip > Q5: "local $/ = \1; #read one byte at a time" > I have search perldoc and $/ means $INPUT_RECORD_SEPARATOR, which > default is newline. > what it will be if we set it as \1? snip If you hand $/ a reference to a number then it switches from being a delimiter to a number of bytes to read. In this case 1 byte. from perldoc perlvar Setting $/ to a reference to an integer, scalar containing an integer, or scalar that's convertible to an integer will attempt to read records instead of lines, with the maximum record size being the referenced integer. So this: local $/ = \32768; # or \"32768", or \$var_containing_32768 open my $fh, $myfile or die $!; local $_ = <$fh>; will read a record of no more than 32768 bytes from FILE. If you're not reading from a recordāoriented file (or your OS doesn't have recordāoriented files), then you'll likely get a full chunk of data with every read. If a record is larger than the record size you've set, you'll get the record back in pieces. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read.