How to copy file content to another file in Perl

Perl comes with a core module called File::Copy that allows two essential functions, namely:

  • Copy

  • Move

These functions serve to transfer the contents of a file from one place to another. But what matters for us is the copy method which has the following syntax:

copy(source_file, destination_file)

This command accepts two parameters:

  • source_file: A FileHandle reference, a FileHandle glob, or a string representing the path to the source file is used in this argument.

  • destination_file: A FileHandle reference, a FileHandle glob, or a string representing the path to the destination file is used in this argument.

Note: The destination file will be created automatically if it does not exist but its parent directory exists. An error will occur when copying to a nonexistent directory.

Let's get into the coding and check the following examples:

Code example 1

This example shows how to copy file content while using the inbuilt copy function previously described:

main.perl
input.txt
use File::Copy;
$input_file = 'input.txt';
$output_file = '/usercode/output/output.txt';
copy($input_file, $output_file);

Let's go through the code widget, as follows:

  • Line 1: Import the standard File::Copy module.

  • Line 2: Define a variable, $input_file, pointing to the source file.

  • Line 3: Define a variable, $output_file, containing the destination file path.

  • Line 4: Invoke the function copy in order to get the source file content to the destination file.

Code example 2

The example below demonstrates how to perform the copy of the source file content while relying on low-level I/O routines:

main.perl
input.txt
$input_file = 'input.txt';
$output_file = '/usercode/output/output.txt';
open(input , "< $input_file") or die "Unable to open $input_file";
open(output, "> $output_file") or die "Unable to open $output_file";
$v_blk_size = (stat input)[11] || 16384;
while ($v_len = sysread input, $v_buf, $v_blk_size) {
if (!defined $v_len) {
next if $! =~ /^Interrupted/;
die "Error System Read: $!\n";
}
$v_offset = 0;
while ($v_len) { # Handle partial writes.
defined($written = syswrite output, $v_buf, $v_len, $v_offset)
or die "Error System Write: $!\n";
$v_len -= $written;
$v_offset += $written;
};
}
close(input);
close(output);

Let's go through the code widget, as follows:

  • Line 1: Define a variable, $input_file, pointing to the source file.

  • Line 2: Define a variable, $output_file, containing the destination file path.

  • Line 4: Open the source file using the function open, as follows:

    • The operand < indicates that this file is opened in read-only mode.

    • The function returns a FileHandle in associated with the source file.
      File-opening failures are handled using the die function.

  • Line 5: By the same token, open the destination file in write-only mode (operand >).

  • Line 7: Set the preferred block size for file system I/O.

  • Lines 8–20: Loop through the content blocks of the source file and write them to the destination file. We may exit the process by pressing CTRL-Z.

  • Line 22: Close the source FileHandle.

  • Line 23: Close the destination FileHandle.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved