Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: pack() returns an unusable string

by syphilis (Archbishop)
on May 29, 2021 at 08:53 UTC ( [id://11133250]=note: print w/replies, xml ) Need Help??


in reply to Re: pack() returns an unusable string
in thread pack() returns an unusable string

I recommend that you pass the value as an argument

What I was wanting to do was to pass the actual string as the argument.
It didn't really occur to me that doing that would be such a problem. I thought that people would be able to show me that this could be achieved with little fuss. (I was wrong ;-)
What I was interested in doing is easily demonstrated in the following script, where I've used the code that you supplied, but have replaced the last line with 2 lines:
use strict; use warnings; my $template = 'D<'; my $nv = 2.4; my $p = pack $template, $nv; # I'm running this script on perl-5.34.0 # with nvtype of 'long double'. # I want to see the result of perl-5.34.0 # with nvtype of 'double' unpacking $p : my $perl = "C:/perl-5.34.0/bin/MSWin32-x64-multi-thread/perl.exe"; system $perl, '-wle', 'print unpack("H*", $p)';
If that DWIMmed, it would output :
9a999999999999990040000000000000
but, of course, all that it emits is the warning:
Use of uninitialized value $_ in print at -e line 1.
Sure, I could take a leaf out of your book and alter my last line to:
system $perl, '-wle', 'print $ARGV[0]', unpack('H*', $p);
But that unpack('H*', $p) is being evaluated by the 'long double' build of perl.
I wanted to see what happened when it was evaluated by my 'double' build of perl-5.34.0.
I've since accomplished this by other means.

Cheers,
Rob

Replies are listed 'Best First'.
Re^3: pack() returns an unusable string
by ikegami (Patriarch) on May 29, 2021 at 23:19 UTC

    unpack "H*" hasn't changed, so what you're saying makes no sense. Encode the string in the parent (say using unpack "H*"), then recreate the original in the child if so you desire (using pack "H*").

    Seeking work! You can reach me at ikegami@adaelis.com

      unpack "H*" hasn't changed, so what you're saying makes no sense.

      Well - you apparently knew that, but I didn't.
      I was fairly certain it would be the case and that it should be the case, but I wanted to verify it with an actual example, and your approach does not provide that verification.
      (Admittedly, it is pretty hard to see how anything else *could* be the case ;-)

      I was also wanting to do the same thing, but replacing "H*" with "D" - in order to verify that rounding would be performed as expected. (It is ... so long as one's expectation is correct.)
      Wrt perl-5.34.0, unlike unpack("H*", $string) we see that the value returned by unpack("D", $string) will often vary between 'double' and 'long double' builds, for the very same $string.
      I don't doubt that you regard running those checks as also being something that "makes no sense".
      But having a "D" template on a perl whose nvtype is 'double' is a little disconcerting to me, and I just want to make sure that its behaviour is clear in my mind.

      Cheers,
      Rob

        I was also wanting to do the same thing, but replacing "H*" with "D"

        That totally misses the point. I suggested using unpack "H*" as a form of serialization. You'd use in the parent and pack "H*" in the child in addition to pack "d" or pack "D" or whatever you want to test.

        Seeking work! You can reach me at ikegami@adaelis.com

        Why would you think that any or all of pack "H*" AND JSON encoders AND base64 encoders AND all the other alternatives I mentioned stopped working?

        Seeking work! You can reach me at ikegami@adaelis.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11133250]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2024-04-18 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found