Question

I have a test generator written in Perl. It generates tests that connect to a simulator. These tests are themselves written in Perl and connect to the simulator via its API. I would like the generated code to be human-readable, which means I'd like it to be properly indented and formatted. Is there a good way to do it?

Details follow, or you can skip to the actual question below.

This is an example:


my $basic =  ABC
        TRIGGER        => DELAY(
            NUM            => 500,
            ),
        )
BASIC

my $additional =  STATE_IS(
        STATE          => DEF,
        INDEX          => 0,
        ),

ADDITIONAL

I'd like the command ABC to be executed with a delay of 500 (units aren't relevant just now) after I call &event, and the state of index 0 is DEF. Sometimes I'll also want to wait for indeces 1, 2, 3 etc...

For only one index I'd like to see this in my test:


    &event(
        CMD            => ABC
        TRIGGER        => DELAY(
            NUM            => 500,
            TRIGGER        => STATE_IS(
                STATE          => DEF,
                INDEX          => 0,
                ),
            ),
        )

For two indeces I'd like to see:


    &event(
        CMD            => ABC
        TRIGGER        => DELAY(
            NUM            => 500,
            TRIGGER        => STATE_IS(
                STATE          => DEF,
                INDEX          => 0,
                TRIGGER        => STATE_IS(
                    STATE          => DEF,
                    INDEX          => 1,
                    ),
                ),
            ),
        )

So basically I'm adding a block of:


            TRIGGER        => STATE_IS(
                STATE          => DEF,
                INDEX          => 0,
                ),

for each index, and the index number changes.

Here's how I'm doing it:


for $i (0..$num_indeces) {
    # update the index number
    $additional =~ s/(INDEX\s*=>\s*)\d+,/$1 $i,/;

    $basic =~ s/(
                (\),\s*)  # capture sequences of ),
                +         # as many as possible 
                \)\s*     # end with ) without a , 
}               )/$additional $1/sx; # replace with the additional data

Here's the actual question

The problem here is that the code comes out poorly indented. I'd like to run the resulting $basic through a prettifier like this:

&prettify($basic, "perl");

Which would format it nicely according to Perl's best practices. Is there any good way to do this?

Was it helpful?

Solution

I have used this:

use Perl::Tidy;
sub Format {
    my $source = shift;
    my $result;
    Perl::Tidy::perltidy(
        source      => \$source,
        destination => \$result,
        argv        => [qw(-pbp -nst)]
    );
    return $result;
}

OTHER TIPS

PerlTidy makes your code not only tidy, but really beautiful. You can easily tweak it according to your local coding standards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top