Reputation: 706
I am very new to Perl and its syntax. I've done a bit of research about reading from one file and writing to another. I've written a short piece of code that doesnt seem to be giving me any error but it also doesn't write to the file. Some help would be greatly appreciated.
#!/usr/bin/perl
use strict;
use warnings;
my $defaultfile = 'C:\\Glenn Scott C\\AUTO IOX\\IOMETER FILES\\test.txt';
my $mainfile = 'C:\\Glenn Scott C\\AUTO IOX\\IOMETER FILES\\IOMETERFILECREATOR.txt';
open FILE, $defaultfile;
open FILE2, $mainfile;
while (my $line = <FILE>)
{
print FILE2($line);
}
close FILE;
close FILE2;
Upvotes: 1
Views: 7736
Reputation: 53508
Close, but not quite.
open is best done with 3 arguments. open ( my $default_fh, '<', $defaultfile ) or die $!;
print
to a file handle doesn't work like that. It's print {$main_fh} $line;
you should test open
for success. An or die $!
is sufficient.
So this would be what you need:
#!/usr/bin/perl
use strict;
use warnings;
my $defaultfile = 'C:\\Glenn Scott C\\AUTO IOX\\IOMETER FILES\\test.txt';
my $mainfile =
'C:\\Glenn Scott C\\AUTO IOX\\IOMETER FILES\\IOMETERFILECREATOR.txt';
open( my $default_fh, "<", $defaultfile ) or die $!;
open( my $main_fh, ">", $mainfile ) or die $!;
while ( my $line = <$default_fh> ) {
print {$main_fh} $line;
}
close $default_fh;
close $main_fh;
Upvotes: 3