in reply to Re: Converting a source code
in thread Converting a source code

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.... <;

Replies are listed 'Best First'.
Re^3: Converting a source code
by GrandFather (Saint) on Aug 21, 2006 at 20:48 UTC

    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!

Re^3: Converting a source code
by swampyankee (Parson) on Aug 21, 2006 at 22:04 UTC

    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!!!

Re^3: Converting a source code
by graff (Chancellor) on Aug 22, 2006 at 03:33 UTC
    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...