##
my %bitNames = (
BIT0 => 0,
BIT1 => 1,
BIT2 => 2,
BIT3 => 4,
BIT4 => 8,
#...
);
####
my $flag=0; # or value from your api
vec($flag, $bitNames{BIT0}, 1)=$newBit;
# you can also use vec to retrieve the old bit values:
print "Value at position BIT3 is:",vec($flag, $bitNames{BIT3},1),"\n";