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

Hi I have been trying to performing some math op after converting the number to hex(since I need to print it in hex) before doing some math on it, but to no avail. Here's the snippet.
$b_ecc = $b_ecc->as_hex(); $bmask = $bmask->as_hex(); $bigint_data = $bigint_data->as_hex(); print "$bigint_data, $bmask, $b_ecc\n"; $count_xor+=1 if($b_ecc->is_odd());
And I get this error instead:

Can't call method "is_odd" without a package or object reference at blah in line 21

Something I am missing here. Works fine, if I don't covert it to int. I have alternative methods to get around this, but would like to understand the problem though.

Replies are listed 'Best First'.
Re: Math after hex conversion
by Corion (Patriarch) on Nov 09, 2016 at 11:36 UTC

    Why do you convert your number to a string in hexadecimal representation when you still want to do arithmetic with it?

    I think that removing the line will do what you want:

    # $b_ecc = $b_ecc->as_hex(); # remove this
Re: Math after hex conversion
by ww (Archbishop) on Nov 09, 2016 at 11:47 UTC

    Convert the result of your math ops; not the input.

    If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: Math after hex conversion
by Monk::Thomas (Friar) on Nov 09, 2016 at 14:42 UTC
    After this statement
    $b_ecc = $b_ecc->as_hex();

    $b_ecc is no longer an object, you replaced it with a simple scalar value ("0xdeadbeef" or whatever ->as_hex() returns). But then you try to execute '$b_ecc->is_odd()' on something that's no longer an object and Perl complains about it.

    Solution: Don't clobber your variables, pick some other names:

    my $val1 = $b_ecc->as_hex(); my $val2 = $bmask->as_hex(); my $val3 = $bigint_data->as_hex(); print "$val3, $val2, $val1\n"; $count_xor+=1 if($b_ecc->is_odd());
Re: Math after hex conversion
by GotToBTru (Prior) on Nov 09, 2016 at 13:10 UTC

    The only math I see is $count_xor += 1. But the error message has nothing to do with math. You don't show which modules you're using, but you've forgotten the one that defines is_odd().

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)