title: Decoding 3-byte integer in Perl
body: <p>I'm reading a binary file format that starts out with 4 constant check bytes, followed by 3 octets that indicate how long the data portion of the record will be.  I can decode this as follows:</p>
  
  <pre><code>read($fh, $x, 7) or do {
    last if eof;
    die "Can't read: $!";
  };
  my ($type, $l1, $l2, $l3) = unpack("a4 C3", $x);
  my $length = $l1 &lt;&lt; 16 | $l2 &lt;&lt; 8 | $l3;
  </code></pre>
  
  <p>Is there a more direct way to read that 3-byte value, without intermediate variables?  Something I'm missing in the <code>pack</code> specifications maybe?  I haven't used <code>pack</code> very much except for hex encoding and other dilettantish pursuits.</p>