Updated July 3, 2023
Introduction to Perl copy file
The Perl copy file functionality is implemented using the copy() function, which is part of the Perl language. This function allows the user to copy files and their contents from one location to another. Typically, the copy function in Perl is employed as an intermediate step in the process of storing and retrieving data from a server.
Syntax:
By using these functions, time will be saved if the requirement is to be the same as the application also during the backup, it will be a major role of the end-user.
use File::Copy "reference string";
copy("user input datas sourcefile", "same userinputs which is copied from the source it's a destination datas");
$variable = FileHandle or any other type of Exception -> new("path", "r or w");
cp($variable,"");
How to copy file in Perl?
In Perl, the copy function is a versatile feature that can be utilized in various modules. And these two sets of arguments related to the string data type will throw some exceptions like file handle with reference, etc. Because this exception mainly occurs in the source path when the user gives the wrong path or there is no data related to the path, like a file name invalid file has no contents or data. We used “r ->read,” “w>write” for these characters, which is pointed out on the copy function whether the file is to be readable, writeable, or not. Generally, the copy function will not overwrite the read-only data on the files if suppose the destination path of the file already exists and also the name it’s a directory.
Examples
Let us discuss examples of Perl copy file.
Example #1
Code:
use File::Copy;
$varsourcefile = 'first.txt';
$vardestinationfile = 'second.txt';
copy($varsourcefile, $vardestinationfile) or die "Sorry User your file datas are not copied kindly check once with your end the file is in the correct source path: $!";
Output:
In the above example, we used copy function for the file module, which is on the basic level. In this scenario, we utilize two variables, $varsourcefile, and $vardestinationfile, to handle the file paths for the source and destination files. Here we have not received any exceptions or errors during the run time.
Example #2
Code:
#!/usr/bin/env perl
use strict;
use warnings;
use File::Copy;
my $varsourcefile = "E:/first";
my $vardestinationfile = "F:/second";
opendir(my $vars2, $varsourcefile) || die "User kindly check once with your end whether the files are there in the source also the files are not exists on the destination $!";
my @vars3 = readdir($vars2);
foreach my $vars4 (@vars3)
{
if(-f "$varsourcefile/$vars4" ) {
copy "$varsourcefile/$vars4", "$vardestinationfile/$vars4";
}
}
closedir($vars2);
Output:
In the second example, we continue to use the same copy function from the file module. However, there are some modifications in this case. We have a folder named “first” containing three files, each with its own data or contents. Using the copy function mentioned earlier, we copy the entire folder and its contents from the source drive to the destination drive while maintaining the same file names and data without any interruptions. To accomplish this, we create a folder named “second” on the destination drive and then proceed to copy and paste the contents of the source folder “first” into the destination folder “second.”
Example #3
Code:
use File::Copy;
copy("first.jsp", "second.txt")
or die "Sorry user your jsp file datas are copied to the destination please check the file formats: $!";
print "Thank you user your jsp file datas are copied to the text file format successfully \n";
Output:
The source file, “first.jsp,” and the destination file, “second.txt,” have distinct file extensions. It has one of the advantages when we use copy() function in the Perl script.
Conclusion
In Perl script, copy function is one of the main concepts for copying the source files to the destination files without interruptions and also the data losses. When we use copy(), it has some other types also, like rmscopy, syscopy, and move these functions are also included. It depends on the user’s requirements.
Recommended Articles
This is a guide to Perl copy file. Here we also discuss the Definition and How to copy file in Perl? along with different examples and code implementation. You may also have a look at the following articles to learn more –