tr translates characters. What you want to do is substitute strings. The strings you want to alter are stored in $line, but your current code, even if tr did as you expected, operates on $_ - the default variable.
The foreach loop "slurps" the file - reads the whole thing into memory. Your current code doesn't require that - use a while loop instead.
The following (untested) code should help:
use strict;
open sourcefile, '<', "source.cbl" or die "Can't open: $!";
open targetfile, '>', "source.ada" or die "Can't open: $!";
while (defined (my $line = <sourcefile>)) {
if ($line =~ s/WORKING -STORAGE SECTION\./declare;/) {
print targetfile $line;
} elsif ($line =~ s/PROGRAM -BEGIN\./begin/) {
print targetfile $line;
However there are better ways of doing this trick. For a start you may be better putting your translation pairs into an array then itterate over it for each input line (untested):
use strict;
use warnings;
my @pairs = (
['WORKING -STORAGE SECTION.', 'declare'],
['PROGRAM -BEGIN.', 'begin;'],
['^DISPLAY', 'PUT'],
['^ACCEPT', 'GET'],
['PROGRAM -DONE.', 'END;'],
);
open sourcefile, '<', "source.cbl" or die "Can't open: $!";
open targetfile, '>', "source.ada" or die "Can't open: $!";
while (defined (my $line = <sourcefile>)) {
$line =~ s/$_-[0]/$_->[1]/ for @pairs;
print targetfile $line;
}
However this sort of work is really crying out for Parse::RecDescent.
Update change to three parameter opens.
DWIM is Perl's answer to Gödel
|