Reputation: 13
How to convert image, text, PDF or for that instance any file to byte array in Perl, without using any external library?
Upvotes: 1
Views: 433
Reputation: 241828
Use open to open the file, specify the :raw
to read it in binary. read (or any other means) reads into a buffer, so you need to unpack it into bytes.
#! /usr/bin/perl
use strict;
use warnings;
my $file_name = shift;
my @byte_array;
open my $fh, '<:raw', $file_name or die $!;
while (read $fh, my $buffer, 16384) {
push @byte_array, unpack 'c*', $buffer;
}
print 'Size: ', scalar @byte_array, "\n";
Upvotes: 3