Free Perl Tutorials

How To Copy A File In PERL

HOW TO COPY A FILE IN PERL

Copying a file using the PERL programming language is a bit more complicated than the renaming function. It's got a small difference, but nothing even a newb like you can't over come :)

Here is the code for how to copy a file in PERL:


use File::Copy
copy ("test.txt","copy-of-test.txt");


*GASP*
What in the world is that crazy "use" function, and what the heck is "File::Copy"??!
Relax. PERL has a library of modules that you can use in your programming endeavors to make your life 100 times easier. "use" tells PERL to 'use' a module called File::Copy (which most likely came with your original download of PERL). Since PERL in its most naked form doesn't have an actual copy function, it allows you to use the "copy" function we see in our code!

Now to explain the code: The first .txt file in the quotes in the parenthases (test.txt) is the file we want to copy and the second in quotes in the parenthases is the name of the copied file we wish to create (copy-of-test.txt).

Run the program, and you will see your original file and the copied file.

Don't see a copied file? Probably because your copy .pl program is not located in the same directory as the .txt file you wish to copy!

Don't fear the modules! Modules are our friend!