DEV Community

OpossumPetya
OpossumPetya

Posted on

Use Notepad++ as a GUI for your Perl application

You can use Notepad++ as a poor man's GUI for your Perl applications/scripts. This could allow less technical people, who might shy away from a terminal, to use your tools too!

Example of Notepad++ interfacing with a Perl program

Here's how, plus a few use case ideas below.

Meet Pork to Sausage

This creatively named Notepad++ plugin allows you to pass selected text from the editor to any command line tool, and replace it with the output of that tool.

Perl Sample Code

Text manipulation? Of course we'll use Perl, what else?!

Here's a poor man's hex viewer (you probably already getting a hint where I am on the richness spectrum):

use strict;
use warnings;
use feature 'say';

# hex representation of a string: 'ABC' -> '41 42 43'
sub str2hex { return uc join ' ', unpack "(A2)*", unpack 'H*', shift }

# removes CR and LF (whatever is present) from the end of a string
# NP++ is a Windows tool, but who knows where that file came from!
sub chomp_any { my $ret = shift; $ret =~ s/\r?\n\z//; return $ret }

# that highlighted text from NP++
my $str_from_npp = $ARGV[0];

# process it one line at a time
for my $line (split /^/, $str_from_npp) {
    say str2hex chomp_any $line ;
}
Enter fullscreen mode Exit fullscreen mode

A quick test from the command prompt:

PS C:\dev\demo> perl .\hex-view.pl "ABC"
41 42 43
Enter fullscreen mode Exit fullscreen mode

Configure the Plugin

To use our program from within Notepad++ we must add it to the previously mentioned Pork to Sausage plugin (the plugin should be already installed) config file, which is located at %AppData%\Notepad++\plugins\config\pork2Sausage.ini:

[Poor Mans Hex Viewer]
progPath=C:\dev\_tools\perl\perl\bin\perl.exe
progCmd=perl C:\dev\demo\hex-view.pl $(SELECTION)
workDir=C:\dev\demo\
Enter fullscreen mode Exit fullscreen mode

I use Strawberry Perl portable, and it's location on my machine here: C:\dev\_tools\perl\

And that's it!

Restart Notepad++, for the plugin to pickup the config changes. You will find your tool in Notepad++ top menu under Plugins -> Pork to Sausage -> Poor Mans Hex Viewer. Highlight some text, invoke your program, and see the magic!

You don't need to restart the editor if you need to update your Perl script.

Usage Ideas

Here are a few use cases you may find useful for interfacing NP++ with a command line tool:

  1. Inserting external data: weather, currency conversions, whole snippets, etc.

2: Completing data to be more meaningful to the user. As an example, our team would receive a list of bare Jira IDs of the stories that were recently deployed. A script that called Jira API to get titles for those stories gave us more understanding of what has been deployed in a short glance.

3: Formatting text data: maybe something like the Hex viewer example above, or something more complicated, like my FixedWidthDataReader tool, that is showcased in the animation at the beginning of this article.

Tips

  • Pack your Perl program to Windows executable using PAR::Packer's pp for easy distribution:
pp -o cool-tool.exe cool-tool.pl
Enter fullscreen mode Exit fullscreen mode

Then the plugin config could look like this:

[Cool Tool]
progPath=C:\CoolTool\cool-tool.exe
progCmd=cool-tool.exe --some-args $(SELECTION)
workDir=C:\CoolTool
Enter fullscreen mode Exit fullscreen mode
  • Already have a command line Perl tool that processes a file? You can open a string variable just like a file! This allows you to only add some logic around what's being open (file or a string), and don't need to touch any of the "business logic" code that processes the lines of data.
open my $fh, '<:crlf', \$multiline_string_from_npp;
Enter fullscreen mode Exit fullscreen mode

Hope you will find this useful!

Top comments (0)