in reply to Converting a source code
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Converting a source code
by oblate kramrdc (Acolyte) on Aug 21, 2006 at 19:57 UTC | |
by GrandFather (Saint) on Aug 21, 2006 at 20:48 UTC | |
by oblate kramrdc (Acolyte) on Aug 24, 2006 at 22:00 UTC | |
by swampyankee (Parson) on Aug 21, 2006 at 22:04 UTC | |
by rvosa (Curate) on Aug 22, 2006 at 01:10 UTC | |
by oblate kramrdc (Acolyte) on Aug 24, 2006 at 21:57 UTC | |
by graff (Chancellor) on Aug 22, 2006 at 03:33 UTC | |
by oblate kramrdc (Acolyte) on Aug 24, 2006 at 21:56 UTC |