Handling Binary Files in Perl

Handling Binary Files in Perl

For some reason, there exists a common misconception that there is no cross-platform, built-in way in Perl to handle binary files. The copy_file code snippet below illustrates that Perl handles such tasks quite well. The trick is to use “binmode” on both the input and output files after opening them. “Binmode” switches files to binary mode, which for the input file means it won’t stop reading at the first “end of text file” character (^Z in win/dos); for the output file binmode means it won’t translate ‘
‘ (LF) into ‘
‘ (CRLF) when printing. In this way the files get copied byte for byte.

sub copy_file {  my ($srcfile, $destfile) = @_;  my $buffer;  open INF, $srcfile    or die "
Can't open $srcfile for reading: $!
";  open OUTF, ">$destfile"    or die "
Can't open $destfile for writing: $!
";  binmode INF;  binmode OUTF;  while (    read (INF, $buffer, 65536)	# read in (up to) 64k chunks, write    and print OUTF $buffer	# exit if read or write fails  ) {};  die "Problem copying: $!
" if $!;  close OUTF    or die "Can't close $destfile: $!
";  close INF    or die "Can't close $srcfile: $!
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes