Search code examples
perlor-operator

perl logical OR operator


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


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"));