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

Discussion in 'Perl Misc' started by Roland Reichenberg, Oct 14, 2003.

  1. 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
     
    Roland Reichenberg, Oct 14, 2003
    #1
    1. Advertising

  2. Roland Reichenberg

    ko Guest

    Roland Reichenberg wrote:
    > 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
     
    ko, Oct 14, 2003
    #2
    1. Advertising

  3. Roland Reichenberg

    Sisyphus Guest

    Roland Reichenberg wrote:
    > 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

    --
    To reply by email u have to take out the u in kalinaubears.
     
    Sisyphus, Oct 14, 2003
    #3
  4. Roland Reichenberg wrote:
    >
    > 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.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Möllers, Oct 14, 2003
    #4
  5. Roland Reichenberg

    Bart Lateur Guest

    ko wrote:

    >> #!/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,

    >> $var2 = 0xaffe;


    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;

    --
    Bart.
     
    Bart Lateur, Oct 14, 2003
    #5
  6. Roland Reichenberg

    ko Guest

    Bart Lateur wrote:
    > 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,
    >
    >
    >>>$var2 = 0xaffe;

    >
    >
    > 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.
     
    ko, Oct 14, 2003
    #6
  7. Josef Möllers <> wrote:
    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 14, 2003
    #7
  8. Tad McClellan wrote:
    >
    > Josef Möllers <> wrote:
    > > 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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Möllers, Oct 14, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,223
    Neredbojias
    Aug 19, 2005
  2. David
    Replies:
    10
    Views:
    5,967
    Richard Heathfield
    Sep 15, 2003
  3. Bengt Richter
    Replies:
    6
    Views:
    474
    Juha Autero
    Aug 19, 2003
  4. Manish_Ganvir
    Replies:
    13
    Views:
    1,585
    Keith Thompson
    Feb 14, 2005
  5. Ioannis Vranos
    Replies:
    8
    Views:
    341
    James Kanze
    Nov 14, 2008
Loading...

Share This Page