Use Notepad++ as a GUI for your Perl application
Tags: perl, notepadplusplus, npp, porktosausage
Published: 2023-08-28 14:55:20

---
title: Use Notepad++ as a GUI for your Perl application
published: true
description: Notepad++ and Pork to Sausage to interface with Perl scripts
tags: Perl, NotepadPlusPlus, npp, PorkToSausage
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-08-28 13:12 +0000
---

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](https://github.com/OpossumPetya/FixedWidthDataReader/raw/main/misc/test-npp-animation.gif)

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

## Meet Pork to Sausage

This creatively named [Notepad++ plugin](https://github.com/npp-plugins/pork2sausage) 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):

```perl
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 ;
}
```

A quick test from the command prompt:

```powershell
PS C:\dev\demo> perl .\hex-view.pl "ABC"
41 42 43
```

## 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](https://npp-user-manual.org/docs/plugins/)) 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\
```

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](https://github.com/OpossumPetya/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`](https://metacpan.org/pod/pp) for easy distribution:

```
pp -o cool-tool.exe cool-tool.pl
```

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
```

* 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.

```Perl
open my $fh, '<:crlf', \$multiline_string_from_npp;
```

Hope you will find this useful!