mkHun mkHun - 2 months ago 6x
Perl Question

What is the meaning of \0 in perl?

$a = "aaata";
$b = "aataa";
$count = ($a ^ $b) =~tr/\0//c;

output 2 (because of two miss matches perform by the
without using
flag output is

Here what is the use of
. Without using
, script gives the some gibberish character. I don't know what is this and use of
in here and use of the
. Apart from this where we use the
in perl.


In general, an escape sequence consisting of up to three octal digits will insert a character with that code point, so \40 or \040 produce a space character, and \0 produces an ASCII NUL

The code is counting the number of characters that are different between $a and $b

It does a bitwise XOR on the two strings. Any characters that are identical will XOR together to zero, producing a NUL character. The tr/\0//c counts the number of characters in the resulting string that other than NULs (because of the /c modifier) so it will return 2 in this case because the two strings are different at the third and fourth character positions

Dumping the value of the expression $a ^ $b shows this clearly


The tr/// counts the two \25 characters, ignoring all NULs