Format your Perl scripts using Perl-Tidy and Notepad++
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:
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:
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:
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):
Now the last step is to reload the current script to see our change. Simply click on File and select Reload from Disk:
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
Very helpful. Thanks for sharing this.
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!
Good call!
I have updated the guide to include automatically reloading the file from disk.
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.
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
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
Good, thank you very much!
Great post! Instructions worked perfectly thanks
Useful post, thanks.
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
My file name had a space in it that I did not catch. All is good now
Cool. Thank you. It worked well.
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! :)
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!
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)
Great advice! I’ll add it to the post.
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..
Thanks for the info!! it worked well.
Looks so much beter now…
Cheers.
Here it is, 2019 – and this still works perfectly
Thanks for sharing!
Still working great. Thank you!