title: How to read binary file in Perl
body: <p>I'm having an issue with writing a Perl script to read a binary file.</p>
  
  <p>My code is as the following whereby the <code>$file</code> are files in binary format. I tried to search through the web and apply in my code, tried to print it out, but it seems it doesn't work well.</p>
  
  <p>Currently it only prints the '&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;" and ""ppppppppppp", but what I really want is it can print out each of the <code>$line</code>, so that I can do some other post processing later. Also, I'm not quite sure what the <code>$data</code> is as I see it is part of the code from sample in article, stating suppose to be a scalar. I need somebody who can pin point me where the error goes wrong in my code. Below is what I did.</p>
  
  <pre><code>my $tmp = "$basedir/$key";
  opendir (TEMP1, "$tmp");
  my @dirs = readdir(TEMP1);
  closedir(TEMP1);
  
  foreach my $dirs (@dirs) {
      next if ($dirs eq "." || $dirs eq "..");
      print "----&gt;$dirs\n";
      my $d = "$basedir/$key/$dirs";
      if (-d "$d") {
          opendir (TEMP2, $d) || die $!;
          my @files = readdir (TEMP2); # This should read binary files
          closedir (TEMP2);
  
          #my $buffer = "";
          #opendir (FILE, $d) || die $!;
          #binmode (FILE);
          #my @files =  readdir (FILE, $buffer, 169108570);
          #closedir (FILE);
  
          foreach my $file (@files) {
              next if ($file eq "." || $file eq "..");
              my $f = "$d/$file";
              print "==&gt;$file\n";
              open FILE, $file || die $!;
              binmode FILE;
              foreach ($line = read (FILE, $data, 169108570)) {
                  print "&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;$line\n";
                  print "ppppppppppp$data\n";
              }
              close FILE;
          }
      }
  }
  </code></pre>
  
  <p>I have altered my code so that it goes like as below.  Now I can read the $data.  Thanks J-16 SDiZ for pointing out that.  I'm trying to push the info I got from the binary file to an array called "@array", thinkking to grep data from the array for string whichever match "p04" but fail.  Can someone point out where is the error?</p>
  
  <pre><code>my $tmp = "$basedir/$key";
  opendir (TEMP1, "$tmp");
  my @dirs = readdir (TEMP1);
  closedir (TEMP1);
  
  foreach my $dirs (@dirs) {
      next if ($dirs eq "." || $dirs eq "..");
      print "----&gt;$dirs\n";
      my $d = "$basedir/$key/$dirs";
      if (-d "$d") {
          opendir (TEMP2, $d) || die $!;
          my @files = readdir (TEMP2); #This should read binary files
          closedir (TEMP2);
  
          foreach my $file (@files) {
              next if ($file eq "." || $file eq "..");
              my $f = "$d/$file";
              print "==&gt;$file\n";
              open FILE, $file || die $!;
              binmode FILE;
              foreach ($line = read (FILE, $data, 169108570)) {
                  print "&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;$line\n";
                  print "ppppppppppp$data\n";
                  push @array, $data;
              }
              close FILE;
          }
      }
  }
  
  foreach $item (@array) {
      #print "==&gt;$item&lt;==\n"; # It prints out content of binary file without the ==&gt; and &lt;== if I uncomment this.. weird!
      if ($item =~ /p04(.*)/) {
          print "=&gt;$item&lt;===============\n"; # It prints "=&gt;&lt;===============" according to the number of binary file I have.  This is wrong that I aspect it to print the content of each binary file instead :(
          next if ($item !~ /^w+/);
          open (LOG, "&gt;log") or die $!;
          #print LOG $item;
          close LOG;
      }
  }
  </code></pre>
  
  <p>Again, I changed my code as following, but it still doesn't work as it do not able to grep the "p04" correctly by checking on the "log" file.  It did grep the whole file including binary like this "@^@^@^@^G^D^@^@^@^^@p04bbhi06^@^^@^@^@^@^@^@^@^@hh^R^@^@^@^^@^@^@p04lohhj09^@^@^@^^@@" .  What I'm aspecting is it do grep the anything with p04 only such as grepping p04bbhi06 and p04lohhj09. Here is how my code goes:-</p>
  
  <pre><code>foreach my $file (@files) {
      next if ($file eq "." || $file eq "..");
      my $f = "$d/$file";
      print "==&gt;$file\n";
      open FILE, $f || die $!;
      binmode FILE;
      my @lines = &lt;FILE&gt;;
      close FILE;
      foreach $cell (@lines) {
          if ($cell =~ /b12/) {
              push @array, $cell;
          }
      }
  }
  
  #my @matches = grep /p04/, @lines;
  #foreach $item (@matches) {
  foreach $item (@array) {
      #print "--&gt;$item&lt;--";
      open (LOG, "&gt;log") or die $!;
      print LOG $item;
      close LOG;
  }
  </code></pre>