cannot use bitwise AND-Operator on hex-scalars from ARGV[0]

R

Roland Reichenberg

Hi NG,

here is my littel perl scirpt:

===========================
#!/usr/bin/perl

$var1 = $ARGV[0];

$var2 = 0xaffe;

$res = $var1 & $var2;

print $res;
===========================

if I start this script from command line like this:

user@host > ./my.perl.test.pl 0xcafe

I get an error message that says: "Argument "0xcafe" isn't numeric in
bitwise and (&) in line...

What's going wrong? We have perl V5.8.0 installed on a linux box.

Thank you for your answers!

Regards,

Roland
 
K

ko

Roland said:
Hi NG,

here is my littel perl scirpt:

===========================
#!/usr/bin/perl

$var1 = $ARGV[0];

$var2 = 0xaffe;

$res = $var1 & $var2;

print $res;
===========================

if I start this script from command line like this:

user@host > ./my.perl.test.pl 0xcafe

I get an error message that says: "Argument "0xcafe" isn't numeric in
bitwise and (&) in line...

What's going wrong? We have perl V5.8.0 installed on a linux box.

Thank you for your answers!

Regards,

Roland

Try using oct():

$var1 = oct($var1) if $var1 =~ /^0/;

HTH - keith
 
S

Sisyphus

Roland said:
Hi NG,

here is my littel perl scirpt:

===========================
#!/usr/bin/perl

$var1 = $ARGV[0];

That's effectively the same as:
$var1 = '0xcafe';
(given that the command line arg is, in fact, 0xcafe), which produces
the same error.

Try, instead:
$var1 = hex($ARGV[0]);

It should then work, irrespective of whether the command line arg is
given as 0xcafe or simply cafe.

Hth.

Cheers,
Rob
 
J

Josef Möllers

Roland said:
Hi NG,

here is my littel perl scirpt:

===========================
#!/usr/bin/perl

$var1 = $ARGV[0];

$var2 = 0xaffe;

$res = $var1 & $var2;

print $res;
===========================

if I start this script from command line like this:

user@host > ./my.perl.test.pl 0xcafe

I get an error message that says: "Argument "0xcafe" isn't numeric in
bitwise and (&) in line...

What's going wrong? We have perl V5.8.0 installed on a linux box.

You have a string, not an integer.

Use eval($ARGV[0]);

You can use eval to check for invalid numbers, too.
 
B

Bart Lateur

ko said:
#!/usr/bin/perl
$var1 = $ARGV[0];
$var2 = 0xaffe;
$res = $var1 & $var2;
print $res;
if I start this script from command line like this:

user@host > ./my.perl.test.pl 0xcafe

I get an error message that says: "Argument "0xcafe" isn't numeric in
bitwise and (&) in line...
Try using oct():

$var1 = oct($var1) if $var1 =~ /^0/;

That's the best generic solution. oct() is smart about other prefixes,
so it'll do the right thing with strings that start with "0x" (hex) and
"0b" (binary).

The test for a leading "0" prevents treating strings like "123" as
octal.

There's only one small problem with it, which is most obvious with
bitwise operators. It doesn't convert a string with a decimal number to
a number. If both operands are strings, the bitwise operators work
differently. Now, with the second argument explicitely entered as a
number,

this won't give a problem. If both come from user input, it will.

Just to be on the safe side, you can do

$var1 = $var1 =~ /^0/ ? oct($var1) : 0+$var1;
 
K

ko

Bart said:
ko wrote:
[snip]
Try using oct():

$var1 = oct($var1) if $var1 =~ /^0/;


That's the best generic solution. oct() is smart about other prefixes,
so it'll do the right thing with strings that start with "0x" (hex) and
"0b" (binary).

The test for a leading "0" prevents treating strings like "123" as
octal.

There's only one small problem with it, which is most obvious with
bitwise operators. It doesn't convert a string with a decimal number to
a number. If both operands are strings, the bitwise operators work
differently. Now, with the second argument explicitely entered as a
number,



this won't give a problem. If both come from user input, it will.

Just to be on the safe side, you can do

$var1 = $var1 =~ /^0/ ? oct($var1) : 0+$var1;

Thanks for the pointer :) Didn't think about the numeric vs. string
difference, and should have also pointed out that oct() is flexible when
converting numbers.
 
T

Tad McClellan

Josef Möllers said:
Roland Reichenberg wrote:
$var1 = $ARGV[0];
user@host > ./my.perl.test.pl 0xcafe

You have a string, not an integer.

Use eval($ARGV[0]);


String eval() should be the *last* choice. Use it only when you must,
and we don't need it for this problem as hex() or oct() will do
it without resorting to the evil eval.
 
J

Josef Möllers

Tad said:
Josef Möllers said:
Roland Reichenberg wrote:
$var1 = $ARGV[0];
user@host > ./my.perl.test.pl 0xcafe
You have a string, not an integer.

Use eval($ARGV[0]);

String eval() should be the *last* choice. Use it only when you must,
and we don't need it for this problem as hex() or oct() will do
it without resorting to the evil eval.

hex() or oct() will do only if it is indeed a number.
I added that eval will allow to catch errors.
As an added bonus, eval will also allow expressions ...

But I see your point:
$arg = 'open(X,"> XXX"); close(X);';
eval ($arg);

One never ceases to learn,

Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top