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

In reply to Re: Converting a source code by GrandFather
in thread Converting a source code by oblate kramrdc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.