문제

#!/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 ) ?

도움이 되었습니까?

해결책

| 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"));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top