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

Hi Monks,

Looking on comments to improve my Perl skills. I am revising a script that we use to validate Autosys JIL (Job Insertion Language) files before they are promoted to production. One of the rules is that if insert_job is in the record set then owner must be in the same record. insert_job and owner are on separate lines of the record.

What I have done in the past was if I found insert_job set a flag varable to 1 and if the owner value was found in the record, set the varable to 0. Then if flag eq 1 at the end of the record, raise a error.

Thinking now to put the record into a hash and if $hash{insert_job} exists, then $hash{owner} exists or raise error.

Any other sugesstions? Not looking for full blown solutions, just ideas. We keep adding tests to this script, so I want to make sure I do something that allows for flexability

  • Comment on Testing if A is in a record, The B must be in the Record

Replies are listed 'Best First'.
Re: Testing if A is in a record, The B must be in the Record
by Limbic~Region (Chancellor) on Aug 03, 2006 at 17:01 UTC
    cutter,
    You don't indicate if you are using Parse::RecDescent , Parse::Yapp, or similar to validate the JIL files but if so, you could make a constraint of the grammar be your insert_job/owner relationship.

    Alternatively, there is nothing wrong with your flag approach. If there are a bunch of relationships that need to be met than the hash approach sounds like a good idea. You can let the key be the first condition and the value be the second condition. It would then just be a matter of iterating over the hash looking for keys without values.

    Cheers - L~R

      I have been looking at re-writing this script to use Parse::RecDescent for over a year, but since the script is "good enough" how it is, other fires get put out first. You know how it is.

      Thanks for the comments

        Even with P::RD, you'd be better off doing the test at the end as you are doing now. It greatly simplifies the grammar. In technical terms, it's best to seperate syntax from semantics. It sounds like you have a good solution.
Re: Testing if A is in a record, The B must be in the Record
by GrandFather (Saint) on Aug 03, 2006 at 21:11 UTC

    The hash solution sounds good to me. The flag solution requires code to be altered in places outside the two areas of concern - where the status is detected, and where the constraints are checked (you have to create and initialise the flag variables). With the hash you may be able to detect a little more, like duplicated data in the record (the key exists already).

    If some of the parsing gets done in subs it is much easier to pass a reference to a context hash around than it is to pass a whole bundle of individual flag variables.

    A down side with the hash is that you can end up with some subtle bugs if you don't spell the key names consistently! At least use strict keeps tabs on that using the flags approach.


    DWIM is Perl's answer to Gödel