Home > Perl, Programming, Windows > Format your Perl scripts using Perl-Tidy and Notepad++

Format your Perl scripts using Perl-Tidy and Notepad++

September 24, 2009 Leave a comment Go to comments

UPDATED on 8/15/2011 – The macro now automatically reloads the current file from disk, making this a 1 step re-tidy process.

I primarily use Notepad++ to write my Perl scripts because it is quick, offers code folding, syntax highlighting, and very useful keyboard shortcuts.

I’ve also begun using Perl::Tidy to “beautify” my Perl code. Perl::Tidy is available from CPAN, or available via Activestate’s Perl Package Manager (PPM).

To install via CPAN (if you use Strawberry Perl or Cygwin, this is the easiest method):

If you are not behind a proxy server (most people aren’t), just open a command prompt (or Cygwin shell) and enter:

perl -MCPAN -e shell

Once that has loaded, type:

install Perl::Tidy

Then wait a few moments for it to be installed. If you are behind a proxy server (such as at work), make sure that before you run “perl -MCPAN -e shell” that you set the environment variables in your command prompt. I make this simple by creating a batch file that I save as “proxy.bat” that contains the following:

REM Proxy.bat
set HTTP_proxy=http://myproxy.contosocom:8080
set FTP_proxy=http://myproxy.contosocom:8080

…and then I save that file (proxy.bat) somewhere in my PATH environment variable (such as c:\windows, or in my case I put c:\tools in my PATH environment variable). Then, I just run:

c:\tools\proxy.bat
perl -MCPAN -e shell

Once you have installed the Perl::Tidy module (either from PPM or CPAN), here is how you can integrate it into Notepad++ to re-format (or beautify) your current Perl script.

I am using Notepad++ v5.4.5, but you must ensure that you have the NppExec plugin installed with Notepad++. It is typically part of the standard installation.

Begin by opening Notepad++ and selecting the Plugins menu, NppExec submenu, and select Execute:

perltidy02

In the Execute dialog box, enter the following (note: change the path to perl.exe if your installation is different):

npp_save $(FULL_CURRENT_PATH)
perl -x -S perltidy -b "$(FULL_CURRENT_PATH)" -ole=win
npp_open $(FULL_CURRENT_PATH)

Then click Save…

NOTE, this command tells Perl-Tidy to create a backup of the current script (in the script’s directory) and clean up the code according to Perl-Tidy’s default formatting options. It will only create 1 backup file. If one already exists it is replaced. See here for other Perl-Tidy configuration options.


The second command tell Notepad++ to reload the file from disk once Perl::Tidy completes. This means changes are immediately updated in your active document.


Enter Perltidy as the script name when prompted.

Next select the Plugins menu, NppExec submenu, and select Advanced Options:

perltidy05

On the Advanced Options screen, first enter a menu item name, such as Run PerlTidy on current file. Then select PerlTidy on the Associated script drop-down box. Then be certain to click the Add/Modify button.

Now look at the top-left of the dialog box and click teh Place to the Macros submenu checkbox. Then click OK.

See the image below for a graphic that also shows the order of these operations:

perltidy06

You may receive a message from Notepad++ that you need to restart the program. Do that now.

Now you are ready to test it all out! Open up a Perl script that is in need of some formatting. If you aren’t sure if you have one, open up any Perl script, select all, and then press SHIFT+TAB several times to remove all indentation.

Now click on the Macro menu item and select Run PerlTidy on current file (or whatever you entered for Item Name from the previous step):

perltidy07

Now the last step is to reload the current script to see our change. Simply click on File and select Reload from Disk:

perltidy08

The above step is no longer necessary since I have updated this guide on 8/15/2011. The reason it isn’t necessary is because when creating our macro using NppExec, I specified the internal Notepad++ command, npp_open $(FULL_CURRENT_PATH), which reloads the file from disk once Perl::Tidy has executed.

npp_save $(FULL_CURRENT_PATH)
perl -x -S perltidy -b "$(FULL_CURRENT_PATH)" -ole=win
npp_open $(FULL_CURRENT_PATH)

Although this reloads the file from disk, when you click a different window on your desktop and then later click on Notepad++, you may receive a message stating that the file has been modified and it will ask if you want to reload it:

If you would like to suppress this message:

  • Click the Settings menu
  • Select Preferences
  • Select the MISC. tab.
  • Check Update Silently


Advertisements
Categories: Perl, Programming, Windows
  1. Albert
    February 26, 2010 at 8:29 AM

    Very helpful. Thanks for sharing this.

  2. Alex
    March 5, 2010 at 1:44 PM

    To make it even faster, you can make a new macro which runs the perlTidy macro and then reloads the file from disk, and add a shortcut to it. Very useful, thanks!

    • Mick
      March 5, 2010 at 2:13 PM

      Good call!

    • Mick
      August 15, 2011 at 1:30 PM

      I have updated the guide to include automatically reloading the file from disk.

  3. DanyC
    February 14, 2012 at 11:34 AM

    Hi,

    I just done what you said using cygwin on XP and i get this message in Notepad++

    perl -x -S perltidy -b “new 1” -ole=win
    CreateProcess() failed with error code 2:
    The system cannot find the file specified.

    NPP_OPEN: new 1
    ================ READY ================

    Any idea?

    Many thanks.

  4. DanyC
    February 14, 2012 at 11:39 AM

    Hi again,

    I now changed to

    c:\cygwin\bin\perl -x -S perltidy -b “$(FULL_CURRENT_PATH)” -ole=win
    npp_open $(FULL_CURRENT_PATH)

    and i get

    NPP_EXEC: “Perltidy”
    c:\cygwin\bin\perl -x -S perltidy -b “new 1” -ole=win
    Process started >>>
    Can’t find perltidy on PATH.
    <<< Process finished.
    NPP_OPEN: new 1
    ================ READY ================

    Thanks,
    Dani

    • Mick
      March 3, 2012 at 9:29 AM

      I would suggest adding Perl’s bin path to your system PATH. For example, since I use Strawberry Perl I have this added:
      C:\Strawberry\perl\site\bin

  5. June 20, 2012 at 3:45 AM

    Good, thank you very much!

  6. Samuel
    July 6, 2012 at 9:35 AM

    Great post! Instructions worked perfectly thanks

  7. Keith
    October 17, 2012 at 11:09 AM

    Useful post, thanks.

  8. Mark
    November 5, 2012 at 4:22 PM

    It is not working for me for some reason. When I go to the command prompt and type in :
    perltidy -v

    I get this confirming that it is indeed installed:
    This is perltidy, v20120714

    And when I just type in:
    perltidy new2.pl

    I get the following back:
    skipping file: ‘new2.pl’: no matches found

    • Mark
      November 5, 2012 at 4:43 PM

      My file name had a space in it that I did not catch. All is good now

  9. SAM
    September 1, 2013 at 11:10 AM

    Cool. Thank you. It worked well.

  10. Lady Aleena
    November 15, 2013 at 6:37 PM

    One little problem, when it tidied my perl, it changed the End of Line to Windows/DOS. I use UNIX EOLs. Other than that, wonderful! :)

  11. December 6, 2013 at 7:55 AM

    Very good tip and tutorial in how to install it. But can I have a _perltidyrc file anywhere that this will use? Or can I oterwise add config options to PerlTidy?

    Thanks!

  12. March 29, 2014 at 11:30 AM

    This is just what I was looking for. However for my situation I found it useful to add the following line at the beginning so you can run the script without loosing data if you haven’t saved it first.

    npp_save $(FULL_CURRENT_PATH)

    • Mick
      March 29, 2014 at 5:19 PM

      Great advice! I’ll add it to the post.

      • Matthew
        May 4, 2014 at 10:56 AM

        Since you’ve added the npp_save command I would suggest always say no as the default responce to the Reload dialogue, the situation now arises that one can run perl tidy continue working for a while then switch to another application and switch back. In this event one should never accept the Reload dialogue or they will loose all their work from the time they ran the macro. In fact I can’t think of a typical situation where one would accept it – only exceptional ones..

  13. Joe
    May 22, 2016 at 9:06 AM

    Thanks for the info!! it worked well.
    Looks so much beter now…

    Cheers.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s