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.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Converting a source code
by oblate kramrdc (Acolyte) on Aug 21, 2006 at 19:57 UTC

    Thanks a lot monks! Been a big help! Combining all your comments...

    use strict; use warnings; 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; } elsif ($line =~ s/DISPLAY/PUT/) { print TARGETFILE $line; } elsif ($line =~ s/ACCEPT/GET/) { print TARGETFILE $line; } elsif ($line =~ s/PROGRAM -DONE\./END;/) { print TARGETFILE $line; } } close SOURCEFILE or die "Can't close: $!"; close TARGETFILE or die "Can't close: $!";
    Tried the array pairs GrandFather but i got a blank target file... I wonder why.... hmm.... <;

      You ought not have got a blank target file. However there was a bug in the code. $_-[0] in the regular expression should have been $_->[0].

      For test purposes it is conventient to use a __DATA__ section following the code and to print to the console. A test version of the code is:

      use strict; use warnings; my @pairs = ( ['WORKING -STORAGE SECTION.', 'declare'], ['PROGRAM -BEGIN.', 'begin;'], ['^DISPLAY', 'PUT'], ['^ACCEPT', 'GET'], ['PROGRAM -DONE.', 'END;'], ); while (defined (my $line = <DATA>)) { $line =~ s/$_->[0]/$_->[1]/ for @pairs; print $line; } __DATA__ WORKING -STORAGE SECTION. PROGRAM -BEGIN. DISPLAY ACCEPT PROGRAM -DONE. this DISPLAY not changed this ACCEPT not changed this PROGRAM -DONE. should get fixed

      Prints:

      declare begin; PUT GET END; this DISPLAY not changed this ACCEPT not changed this END; should get fixed

      DWIM is Perl's answer to Gödel

        Ah missed that!

      No, no not this

      open "SOURCEFILE", '<', "source.cbl" or die "Can't open: $!";

      but this:
      open SOURCEFILE, '<', "source.cbl" or die "Can't open: $!";

      I probably was less than clear (typical; my first language is Ambiguous English). When I said "uppercase strings", I just meant that file handles typically look like this FILEHANDLE not like filehandle.

      emc

      Experience is a hard teacher because she gives the test first, the lesson afterwards.

      Vernon Sanders Law
        Or the 'better practice' form:
        open my $sourcefh, '<', 'source.cbl' or die $!;

        ah ok tnx swampyankee!!!

      I hope the previous replies have solved the immediate problem for you, and you are getting stuff stored into your target file. Just a couple more comments about the code you posted:
      • If a line from the input file does not contain any of the patterns that you are altering via "s///", that line will not be printed to the output file. Is this what you want?
      • You are doing a lot of redundant typing. One of the nice things about Perl is that you can write scripts with an absolute minimum of repetitive lines of code.
      If you actually want the output file to have the same number of lines as the input file (with some lines being edited), it's pretty short and simple:
      use strict; use warnings; my $inp = "source.cbl"; my $out = "source.ada"; open INP, '<', $inp or die "Can't open $inp: $!"; open OUT, '>', $out or die "Can't open $out: $!"; while (<INP>) { s/WORKING -STORAGE SECTION\./declare/; s/PROGRAM -BEGIN\./begin/; s/DISPLAY/PUT/; s/ACCEPT/GET/; s/PROGRAM -DONE\./END;/; print OUT; } close INP; # no worries for closing input file close OUT or die "Can't close $out: $!";

        1.Yes
        2. Oh... that was short...

        Tnx that was simpler...