http://www.perlmonks.org?node_id=412282

sweetblood has asked for the wisdom of the Perl Monks concerning the following question:

I have a bunch of data files along with COBOL copybooks. I need to convert these COBOL data files to ascii. I suspect I'll need to use pack/unpack to get at these data, but I don't have the foggiest notion where to begin. I do have perlpacktut and Pack/Unpack Tutorial, I have not read them as yet but will start as soon as I finish this post.

The copybooks I have look like this:

01 CLASS-OUT. 05 CCNUM-OUT PIC X(14). 05 CCDESC-OUT PIC X(40). 05 CL-OUT PIC X(14). 05 CLDESC-OUT PIC X(38).
or
01 MASTER-OUT REDEFINES HEADER-RECORD. 05 CC-NUMBER PIC 9(04). 05 MTD-UNITS PIC S9(07). 05 MTD-DOLLARS PIC S9(09). 05 INV-ON-HAND PIC S9(07).
I also have a COBOL reference that tells me that 'PIC X(14).' tells me that the data field is a 14-byte alpha-numeric field and that PIC S9(07) is a 7-byte signed integer. I'm just not sure how to get Perl to pass this as ascii.

I would really appreciate any suggetions on how to start.

Thanks!!!

Update: This is on an HP-UX with Perl 5.6.1

Sweetblood

Replies are listed 'Best First'.
Re: COBOL to ascii conversions
by VSarkiss (Monsignor) on Dec 03, 2004 at 21:07 UTC

    You probably mean EBCDIC, which is an encoding, not COBOL, which is a language. You can use EBCDIC-to-ASCII converters (most often in mainframe file transfer programs). After that, you have a plain, fixed-width, ascii file to work with. It's usually a lot simpler than trying to do the EBCDIC conversion yourself.

    As an aside, there are COBOL fields that are a pain to parse (COMP fields, specifically), but if you have plain X and 9 fields, you're OK.

    Update
    For example, once you have it in ascii, you can handle your first record with something as simple as:

    ($ccnum_out, $ccdesc_out, $cl_out, $cldesc_out) = unpack('A14 A40 A14 A38', $class_out);
    Assuming $class_out contains the whole line. For more details on unpack, take a look at perlfunc:pack.

Re: COBOL to ascii conversions
by fglock (Vicar) on Dec 03, 2004 at 21:27 UTC

    Check the Encode module:

    use Encode; ... from_to($data, "EBCDIC", "ASCII");