On Tue, Jul 21, 2020 at 3:12 PM Edgar Pettijohn <[email protected]>
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

Reply via email to