Saturday 5 April 2014

How to write to a file using PERL script

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

use Path::Class;
use autodie; # die if writing a file fails

my $dir = dir("/tmp"); # /tmp

my $file = $dir->file("file.txt"); # /tmp/file.txt

# Get a File Handle (IO::File object) you can write to
my $file_handle = $file->openw();

my @arr = ('This', 'is', 'my', 'first',"Perl","Program");

foreach my $line ( @arr ) {
    # Add the line to the file
    $file_handle->print($line . "\n");

No comments: