Question

#!/usr/bin/perl

$v = "test";
$v |= "best";
print $v;
$v =  "test" | "best";
print $v;

How the OR-ing is coming out here is not clear in second case(first case is oring with null seems to be clear ) ?

Was it helpful?

Solution

| is bitwise operator and you wan't to short-circuit string to variable, thus use logical OR ||

$v ||= "best";

Bitwise calculation for first chars "t" | "b" is same as

#             116  | 98        = 118 ("v")
print chr(ord("t") | ord("b"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top