LoginSignup
6
5

More than 5 years have passed since last update.

Perlでファイルをバイナリで読む

Last updated at Posted at 2017-05-31

2017/07/03 binmodeを追加。
open my $fh, <:rawも同等。binmode($fh)なら過去のPerlとも互換がある。

テキストファイルを特に意味はないけどバイナリで読む方法。
画像ファイルとかの解析なら役に立つ。

まずは普通にテキストファイルを読む

$ cat test1.pl
#!/usr/bin/perl
use strict;
use warnings;

my $file = $0; # 自分自身を読む
open my $fh, "<", $file or die $!;
binmode($fh);
while (my $row = <$fh>) {
        print "read:" . $row;
}
close $fh;

$ perl test1.pl
read:#!/usr/bin/perl
read:use strict;
read:use warnings;
read:
read:my $file = $0; # 自分自身を読む
read:open my $fh, "<", $file or die $!;
read:binmode($fh);
read:while (my $row = <$fh>) {
read:   print "read:" . $row;
read:}
read:close $fh;

テキストファイルをバイナリで読む

$ cat test.pl
#!/usr/bin/perl
use strict;
use warnings;

my $file = $0;
open my $fh, "<", $file or die $!;
binmode($fh);
while (sysread $fh, my $buf, 16) {
        my @data = unpack "C*", $buf;
        my @s = map { sprintf "%02x", $_ } @data;
        print "@s\n";
}
close $fh;

$  hexdump -C test.pl
00000000  23 21 2f 75 73 72 2f 62  69 6e 2f 70 65 72 6c 0a  |#!/usr/bin/perl.|
00000010  75 73 65 20 73 74 72 69  63 74 3b 0a 75 73 65 20  |use strict;.use |
00000020  77 61 72 6e 69 6e 67 73  3b 0a 0a 6d 79 20 24 66  |warnings;..my $f|
00000030  69 6c 65 20 3d 20 24 30  3b 0a 6f 70 65 6e 20 6d  |ile = $0;.open m|
00000040  79 20 24 66 68 2c 20 22  3c 22 2c 20 24 66 69 6c  |y $fh, "<", $fil|
00000050  65 20 6f 72 20 64 69 65  20 24 21 3b 0a 77 68 69  |e or die $!;.whi|
00000060  6c 65 20 28 73 79 73 72  65 61 64 20 24 66 68 2c  |le (sysread $fh,|
00000070  20 6d 79 20 24 62 75 66  2c 20 31 36 29 20 7b 0a  | my $buf, 16) {.|
00000080  09 6d 79 20 40 64 61 74  61 20 3d 20 75 6e 70 61  |.my @data = unpa|
00000090  63 6b 20 22 43 2a 22 2c  20 24 62 75 66 3b 0a 09  |ck "C*", $buf;..|
000000a0  6d 79 20 40 73 20 3d 20  6d 61 70 20 7b 20 73 70  |my @s = map { sp|
000000b0  72 69 6e 74 66 20 22 25  30 32 78 22 2c 20 24 5f  |rintf "%02x", $_|
000000c0  20 7d 20 40 64 61 74 61  3b 0a 09 70 72 69 6e 74  | } @data;..print|
000000d0  20 22 40 73 5c 6e 22 3b  0a 7d 0a 63 6c 6f 73 65  | "@s\n";.}.close|
000000e0  20 24 66 68 3b 0a 0a 0a  0a                       | $fh;....|
000000e6

$ perl test.pl
23 21 2f 75 73 72 2f 62 69 6e 2f 70 65 72 6c 0a
75 73 65 20 73 74 72 69 63 74 3b 0a 75 73 65 20
77 61 72 6e 69 6e 67 73 3b 0a 0a 6d 79 20 24 66
69 6c 65 20 3d 20 24 30 3b 0a 6f 70 65 6e 20 6d
79 20 24 66 68 2c 20 22 3c 22 2c 20 24 66 69 6c
65 20 6f 72 20 64 69 65 20 24 21 3b 0a 77 68 69
6c 65 20 28 73 79 73 72 65 61 64 20 24 66 68 2c
20 6d 79 20 24 62 75 66 2c 20 31 36 29 20 7b 0a
09 6d 79 20 40 64 61 74 61 20 3d 20 75 6e 70 61
63 6b 20 22 43 2a 22 2c 20 24 62 75 66 3b 0a 09
6d 79 20 40 73 20 3d 20 6d 61 70 20 7b 20 73 70
72 69 6e 74 66 20 22 25 30 32 78 22 2c 20 24 5f
20 7d 20 40 64 61 74 61 3b 0a 09 70 72 69 6e 74
20 22 40 73 5c 6e 22 3b 0a 7d 0a 63 6c 6f 73 65
20 24 66 68 3b 0a 0a 0a 0a
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5