A Foolish Manifesto

fREWdiculous!

Perl::Tidy: annoying facts

So I was trying to use perltidy programmatically, that means using Perl::Tidy. Basically I wanted to use an existing .perltidyrc along with the backup option. That is, instead of making a new file with .tdy at the end, replace the original and back it up to .bak. So after reading the docs I figured that this should work:

1
2
3
4
5
6
7
8
9
10
11
   use Perl::Tidy ();
   use File::Spec;

   my $file = File::Spec->catfile( $dir,
      $filename );

   Perl::Tidy::perltidy(
      source     => $file,
      argv        => '-b',
      perltidyrc => $perltidyrc,
   );

Unfortunately that just doesn’t work. Here’s how I got it to work:

1
2
3
4
   Perl::Tidy::perltidy(
      argv        => "-b $file",
      perltidyrc => $perltidyrc,
   );

I also had to modify the .perltidyrc file some as apparently Perl::Tidy doesn’t have a way to choose who wins when there are conflicts in the switches and the config file. One way or another, it was annoying.

Maybe I was doing it wrong?

  • 2 Comments
  • Filed under: Uncategorized
  • More Tools Monday

    So I am working on a new way to use perlcritic, and one of the things I’d like perlcritic to check for is a correctly formatted file. Unfortunately the integration between perlcritic and perltidy goes something like this:

    Tidy the file with perltidy

    Give vague error if tidy file != original file

    That’s fine until you discover that = signs get aligned and apparently you cannot turn that feature off. That means that my code gets marked sketch if I don’t align my = signs. That is terrible. So I figured I’d make it easy to tidy up source files.

    First off you have to install perltidy (I think it’s Acme::Tidy.) This also assumes Win32. On Mac and Linux the commandline isn’t so painful so this isn’t necessary. Next run this code:

    1
    2
    3
    4
    5
    6
    7
    use Win32::TieRegistry;
    $Registry-> Delimiter("/");
    $perlKey = $Registry-> {"HKEY_CLASSES_ROOT/Perl/"};
    $perlKey-> {"shellex/"} = {
        "DropHandler/" =>  {
            "/"=> "{86C86720-42A0-1069-A2E8-08002B30309D}"
        }};

    That will allow for you to drag files onto .pl files and put the file list into @ARGV.

    Then you just make a script with this in it:

    1
    2
    use Perl::Tidy;
    Perl::Tidy::perltidy();

    And drag perl files into it. It will create new files with the .tdy extension in the same dir as the original files. If you create a .perltidyrc and put it in your home it will use those settings. Here’s our .perltidyrc:

    1
    -i=3 -ce -bar -nsbl -sot -sct

    Enjoy!

  • 2 Comments
  • Filed under: Uncategorized