Oh Dispensers of Wisdom, please come to my aid

Using the neat Win32::TieRegistry module I'm reading stuff from the Windows registry. The data gets written to a file, which is later read and processed. I'm having trouble with bit-twiddling after reading the data file, because of my limited understanding of pack, unpack and perl's internal data representation. I'm just missing something here -- possibly a piece of grey matter.

We're using ActiveState perl: "This is perl, version 5.005_03 built for MSWin32-x86-object" (but will probably be moving to a newer version soon.)

For REG_DWORD items read by Win32::TieRegistry's GetValue, the value is read thus:

my ( $value, $type ) = $key->GetValue( "$_" ); # ---- format the value as text if ( $type == 4 ) # reg_dword { $value = hex( $value ) }
So for a reg_dword value 0x00500000 in the registry, the string 5242880 is written to file.

Later I want to test and set bits in the value against a mask, so presumably it needs to be packed back into four bytes. Unfortunately none of my attempts to do this work, and I can't figure out how to "see" how perl is storing the data. Please can someone who has seen the light tell me how?

maybe something like this:

$dword = pack <somehow> ( $value ); # eg $value = 5242880 dec $mask = pack <somehow> ( 4194304 ); # 0x00400000 $flags = pack <somehow> ( 1048576 ); # 0x00100000 $testdw = $dword & $mask; $newdw = $dword | $flags; $newval = hex ( $newdw ); # gets written to file
Many thanks

In reply to how to pack bits, flags and masks by anadem

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.