How can I cover up Perl code generated by Perl?

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

Details follow, or you can go to the current issue below.

This is an example:

my $basic = ABC TRIGGER => DELAY( NUM => 500, ), ) BASIC my $additional = STATE_IS( STATE => DEF, INDEX => 0, ), ADDITIONAL 

I would like to execute the ABC command with a delay of 500 (units are irrelevant just now) after calling &event , and the status of index 0 is DEF . Sometimes I also want to wait for queues 1, 2, 3, etc ...

Only for one index that I would like to see in my test:

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

For two indexes, I would 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 add a block from

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

for each index, and the index number changes.

Here is how I do 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 is the actual question

The problem is that the code comes out with bad indentation. I would like to run the resulting $basic through a prettifier as follows:

 &prettify($basic, "perl"); 

To format it according to Perl's best practices. Is there a good way to do this?

+6
code-formatting perl
source share
2 answers

I 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; } 
+10
source share

PerlTidy makes your code not only neat, but also very beautiful. You can easily customize it to your local coding standards.

+21
source share

All Articles