How do I handle binary data correctly?

Perl is binary clean, so this shouldn't be a problem. For example, this works fine (assuming the files are found):

    if (`cat /vmunix` =~ /gzip/) {
	print "Your kernel is GNU-zip enabled!\n";
    }
On less elegant (read: Byzantine) systems, however, you have to play tedious games with "text" versus "binary" files. See perlfunc/"binmode" or perlopentut. Most of these ancient-thinking systems are curses out of Microsoft, who seem to be committed to putting the backward into backward compatibility.

If you're concerned about 8-bit ASCII data, then see perllocale.

If you want to deal with multibyte characters, however, there are some gotchas. See the section on Regular Expressions.


Back to perlfaq4