Perl Write to File

Summary: in this tutorial, you will learn how to write text to file using the print() function. We will give you several examples of writing to files.

Before going forward with this tutorial, you need to know how to open a file in Perl. If you want to read from a file, follow the reading from a file tutorial.

In order to write to a file, first you need to open the file for writing as follows:

open(FH, '>', $filename) or die $!;Code language: Perl (perl)

If the file with filename $filename does not exist, the new file will be created.

Next, you use the print() function to write data into file as follows:

print FH $str;Code language: Perl (perl)

You must put space between print(), filehandle FH and $str variable. The $str variable holds data that is written to the file. Notice that if you write to a file that contains content, Perl will truncate its content.

As always, you should close the filehandle when you are no longer use it.

close(FH);Code language: Perl (perl)

Putting it all together.

#!/usr/bin/perl
use warnings;
use strict;

my $str = <<END;
This is the sample text
that is used to write to file
END

my $filename = 'c:\temp\test3.txt';

open(FH, '>', $filename) or die $!;

print FH $str;

close(FH);

print "Writing to file successfully!\n";Code language: Perl (perl)

The following example demonstrates how to read the content of one file and write it another file.

#!/usr/bin/perl
use warnings;
use strict;

my $src = 'C:\temp\test3.txt';
my $des = 'C:\temp\test4.txt';

# open source file for reading
open(SRC,'<',$src) or die $!;

# open destination file for writing
open(DES,'>',$des) or die $!;

print("copying content from $src to $des\n");

while(<SRC>){
   print DES $_;	
}

# always close the filehandles
close(SRC);
close(DES);

print "File content copied successfully!\n";Code language: Perl (perl)

How it works.

  • We copied content from the file source file c:\temp\test3.txt to destination file c:\temp\test4.txt.
  • First, we opened source file c:\temp\test3.txt for reading and destination file c:\temp\test4.txt for writing using the open() function.
  • Second, we read the source file line by line. For each line we read from the source file, we used the print() function to write it to the destination file.
  • Third, we closed the filehandles using the close() function.

Here is the output of the program.

Perl Write to File

If you want to pass arguments to the program via command-line, just replace the code at line 6 and 7 by the following code:

my $src = shift @ARGV;
my $des = shift @ARGV;Code language: Perl (perl)

When the program is invoked, the command-line arguments are stored in a special array  @ARGV.  We used the shift() function to get the source and destination files from the array @ARGV.

Now, you can call the program and pass the command-line arguments as follows:

C:\>perl c:\perlws\perl-write-file2.pl c:\temp\test3.txt c:\temp\test4.txt
copying content from c:\temp\test3.txt to c:\temp\test4.txt
File content copied successfully!

C:\>Code language: Perl (perl)

The perl-write-file2.pl is the name of the Perl program.

In this tutorial, we have shown you step by step how to write data to file by using the  print() function.

Was this tutorial helpful ?