Re: perl hex possible bug

2020-07-21 Thread Philip Guenther
On Tue, Jul 21, 2020 at 3:12 PM Edgar Pettijohn 
wrote:

> I was playing around with the hex function in perl. So naturally I
> started with:
>
> perldoc -f hex
>
> Which showed me a few examples namely the following:
>
> print hex '0xAf'; # prints '175'
> print hex 'aF';   # same
> $valid_input =~ /\A(?:0?[xX])?(?:_?[0-9a-fA-F])*\z/
>
> However, I get the following output: (newlines added for clarity)
>
> laptop$ perl -e 'print hex '0xAf';'
> 373
>

You used the same quotes on the inside and out, so the "inner"
quotes actually never get to the perl!  The shell parses the argument to
perl to
print hex 0xAf

0xAf is a numeric literal whose value is 175.  The hex() function then
takes its argument (175) converts it to a string ("175") and interpretats
that string per its rules...as if you passed it "0x175" which equals 373.

If you use distinct quotes, you get the value you expect:

$ perl -le 'print hex "0xAf";'
175
$


> laptop$ perl -e 'print hex 'aF';'
> 175
>

That relies on the so-called poetry extension, where a bare word like aF is
treated as a string.  Turn on strict...

$ perl -Mstrict -le 'print hex Af;'
Bareword "Af" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.
$



> I'm guessing there is a bug here but not sure if its software or
> documentation.
>

No bug, just shell quoting traps.


Philip Guenther


Re: perl hex possible bug

2020-07-21 Thread Andrew Hewus Fresh
I realized after the fact, that looking at some of the different ways
you can write numbers in perl.

$n = 1234;  # decimal integer
$n = 0b1110011; # binary integer
$n = 01234; # octal integer
$n = 0x1234;# hexadecimal integer
$n = 12.34e-56; # exponential notation
$n = "-12.34e56";   # number specified as a string
$n = "1234";# number specified as a string

http://man.openbsd.org/perlnumber

On Tue, Jul 21, 2020 at 05:24:34PM -0700, Andrew Hewus Fresh wrote:
> On Tue, Jul 21, 2020 at 07:10:34PM -0500, Edgar Pettijohn wrote:
> > I was playing around with the hex function in perl. So naturally I
> > started with:
> > 
> > perldoc -f hex
> > 
> > Which showed me a few examples namely the following:
> > 
> > print hex '0xAf'; # prints '175'
> > print hex 'aF';   # same
> > $valid_input =~ /\A(?:0?[xX])?(?:_?[0-9a-fA-F])*\z/
> > 
> > However, I get the following output: (newlines added for clarity)
> > 
> > laptop$ perl -e 'print hex '0xAf';'
> > 373
> 
> so, you're double-use of single quotes here causes some fun shell
> processing.  This is the same as:
> 
> perl -e 'print hex 0xAf'
> 
> (although let me re-do that with -E and say)
> 
> $ perl -E 'say hex 0xAf'
> 373
> 
> Well, as you say, that's not what you expect.
> 
> But, perhaps there is an explanation.  Lets try without hex.
> 
> $ perl -E 'say 0xAf' 
> 175
> 
> interesting, but where's the hex?
> 
> $ perl -E 'say hex 175'
> 373
> 
> ahh, there it is.
> 
> Just to get back on the original page though and avoid the shell
> confusion, lets try one last thing.
> 
> $ perl -E 'say hex "0xAf"'
> 175
> 
> And that work.  Although I guess we can also
> 
> $ perl -e 'print hex "0xAf"'
> 175
> 
> if you'd like.
> 
> 
> > laptop$ perl -e 'print hex 'aF';'
> > 175
> > 
> > I'm guessing there is a bug here but not sure if its software or
> > documentation.
> > 
> > Thanks,
> > 
> > Edgar
> > 
> 
> -- 
> andrew - http://afresh1.com
> 
> Hey! It compiles! Ship it!
> 

-- 
andrew - http://afresh1.com

Hey, I think I see a barn up ahead.
  -- The American Astronaut



Re: perl hex possible bug

2020-07-21 Thread Andrew Hewus Fresh
On Tue, Jul 21, 2020 at 07:10:34PM -0500, Edgar Pettijohn wrote:
> I was playing around with the hex function in perl. So naturally I
> started with:
> 
> perldoc -f hex
> 
> Which showed me a few examples namely the following:
> 
>   print hex '0xAf'; # prints '175'
>   print hex 'aF';   # same
>   $valid_input =~ /\A(?:0?[xX])?(?:_?[0-9a-fA-F])*\z/
> 
> However, I get the following output: (newlines added for clarity)
> 
> laptop$ perl -e 'print hex '0xAf';'
> 373

so, you're double-use of single quotes here causes some fun shell
processing.  This is the same as:

perl -e 'print hex 0xAf'

(although let me re-do that with -E and say)

$ perl -E 'say hex 0xAf'
373

Well, as you say, that's not what you expect.

But, perhaps there is an explanation.  Lets try without hex.

$ perl -E 'say 0xAf' 
175

interesting, but where's the hex?

$ perl -E 'say hex 175'
373

ahh, there it is.

Just to get back on the original page though and avoid the shell
confusion, lets try one last thing.

$ perl -E 'say hex "0xAf"'
175

And that work.  Although I guess we can also

$ perl -e 'print hex "0xAf"'
175

if you'd like.


> laptop$ perl -e 'print hex 'aF';'
> 175
> 
> I'm guessing there is a bug here but not sure if its software or
> documentation.
> 
> Thanks,
> 
> Edgar
> 

-- 
andrew - http://afresh1.com

Hey! It compiles! Ship it!



perl hex possible bug

2020-07-21 Thread Edgar Pettijohn
I was playing around with the hex function in perl. So naturally I
started with:

perldoc -f hex

Which showed me a few examples namely the following:

print hex '0xAf'; # prints '175'
print hex 'aF';   # same
$valid_input =~ /\A(?:0?[xX])?(?:_?[0-9a-fA-F])*\z/

However, I get the following output: (newlines added for clarity)

laptop$ perl -e 'print hex '0xAf';'
373
laptop$ perl -e 'print hex 'aF';'
175

I'm guessing there is a bug here but not sure if its software or
documentation.

Thanks,

Edgar