binmode関数 - バイナリモード



  1. Perl




  2. 関数



  3. here

 ファイルハンドルをバイナリモードに変更するにはbinmode関数を使用します。バイナリモードとはファイル入出力を行うときに改行の自動変換を行わないモードのことです。画像や動画などのバイナリファイルの入出力はバイナリモードで行う必要があります。

binmode $fh;

 画像ファイルを読み込むサンプルです。

# 画像ファイルの読込
my $file = 'picture.png';

open my $fh, '<', $file
  or die "Cannot open '$file': $!";

binmode $fh;

my $size = -s $file;

my $read = read($fh, my $buffer, $size, 0);

die "Cannot read '$file' : $!" unless defined $read;

close $fh;



  1. Perl




  2. 関数



  3. here