Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm not sure if this helps ...

Heh ... it helps ... but only in that it reaffirms what I already knew ;-)

I'll elaborate a little on how this all came about.
As I mentioned in the opening post, with perl-5.34.0, pack's 'D' templates are allowed on all builds irrespective of whether $Config{nvtype} is 'double' or 'long double' or '__float128'.
In earlier perl versions the 'D' templates were supported only if $Config{nvtype} was 'long double'.

With perl-5.34.0 and nvtype of 'long double' we get:
C:\>perl -wle "print unpack 'H*', pack 'D', 2.4;" 9a999999999999990040000000000000
whereas with perl-5.34.0 and nvtype of double, the same command yields a slightly different result:
C:\>perl -wle "print unpack 'H*', pack 'D', 2.4;" 00989999999999990040000000000000
That's pretty much as I expected because the string being unpacked in the first one-liner is different to the string being unpacked in the second one-liner.
But then I wanted to see what the result would be if the string created on the long double build (in the first one-liner) was fed to the 'double' build (in the second one-liner).
Would the result be 9a999999999999990040000000000000 or 00989999999999990040000000000000 ?
(I was quite sure it would be the former, but I knew that if I didn't check then I'd get it wrong.)

So I thought I'd do something cute:
I decided that, on my 'long double' build, I would run the following script:
use warnings; $template = 'D<'; $nv = 2.4; $p = pack $template, $nv; $s = "'$p'"; # Now pass $p to the 'double' build of perl. $double_perl = "C:/perl-5.34.0/bin/MSWin32-x64-multi-thread/perl.exe"; system $double_perl, '-wle', "print unpack('H*', $s);";
But, as already demonstrated, that doesn't DWIM.

In the end, I took the clunky approach of running 2 scripts.
Firstly, on the 'long double' build of perl-5.34.0 I ran the following script that would print the string returned by pack() to a file:
use warnings; $p = pack("D", 2.4); open WR, '>', 'packstr.txt' or die "Opening: $!"; binmode(WR); print WR $p; close WR or die "Closing: $!";
Then, on the 'double' build of perl-5.34.0, I ran a script that passed the string that was saved in packstr.txt to unpack():
use warnings; open RD, '<', 'packstr.txt' or die "Opening: $!"; binmode(RD); $p = <RD>; close RD or die "Closing: $!"; print unpack("H*", $p);
That script output 9a999999999999990040000000000000.
(Hmph ... as if it was ever going to do anything else ...)

It just would have been so much cleaner and simpler if I could have got the first approach to work.

Cheers,
Rob

In reply to Re^2: pack() returns an unusable string by syphilis
in thread pack() returns an unusable string by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found