MedicLdr has asked for the wisdom of the Perl Monks concerning the following question:

We're using warnings as recommended by pbp, reading a mainframe file and trying to test if a bit is set to do some additional logic, we get this warning:
Argument "a" isn't numeric in bitwise and (&) at ...
Is there a way to make "a" understood so the warning will not come out? Here is a one-line example to get the error:
perl -e 'use warnings; $a = "a"; if ($a & 1 << 7) {print "happy\n"}'

Replies are listed 'Best First'.
Re: Warnings and bitwise and
by FunkyMonk (Chancellor) on Oct 12, 2007 at 18:21 UTC
    Do you mean you want the ASCII code of $a? If so, use ord and some extra brackets:
    my $a = "a"; if ( (ord($a) & 1) << 7) {print "happy\n"}

    Update:

    if ( ord($a) & (1 << 7) ) {print "happy\n"}

    seems to make more sense

Re: Warnings and bitwise and
by toolic (Bishop) on Oct 12, 2007 at 18:27 UTC
    If "a" means the hexadecimal 0xA, then you must use hex:
    perl -e 'use warnings; $a = "a"; if (hex($a) & 1 << 7) {print "happy\n +"}'
Re: Warnings and bitwise and
by Aim9b (Monk) on Oct 12, 2007 at 18:59 UTC
    Thanks in no small part to several of the monks here, I have used the following routine to deal with some "flag bytes" in an IBM Mainframe file. A record is read into $inBuf, then fields are parsed out according to format.
    ... @Ind0 = BreakOut(substr($inBuf,6,1)); $Flag1= $Ind0[0]; ... $Flag7= $Ind0[7]; ... sub BreakOut { # Convert a single byte into an array of 8 1-bit flags # represented by a Y if ON, or an N if OFF. my $byte = shift; my @flag, $i; for ($i=0;$i<8;$i++) { if (vec($byte,$i,1)) { $flag[7-$i] = "Y"; } else { $flag[7-$i] = "N"; } # End If vec } # End For return @flag; } # End Sub BreakOut