How can I prettify Perl code generated by Perl?
Asked Answered
Z

2

6

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?

Zippel answered 30/4, 2009 at 12:50 Comment(0)
W
10

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;
}
Williamwilliams answered 30/4, 2009 at 14:47 Comment(0)
K
21

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

Kori answered 30/4, 2009 at 13:4 Comment(0)
W
10

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;
}
Williamwilliams answered 30/4, 2009 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.