<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: OpossumPetya</title>
    <description>The latest articles on DEV Community by OpossumPetya (@opossumpetya).</description>
    <link>https://dev.to/opossumpetya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F104578%2Faafa1918-a02f-4939-8e86-23c137e006f8.png</url>
      <title>DEV Community: OpossumPetya</title>
      <link>https://dev.to/opossumpetya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/opossumpetya"/>
    <language>en</language>
    <item>
      <title>Use Notepad++ as a GUI for your Perl application</title>
      <dc:creator>OpossumPetya</dc:creator>
      <pubDate>Mon, 28 Aug 2023 14:55:20 +0000</pubDate>
      <link>https://dev.to/opossumpetya/use-notepad-as-a-gui-for-your-perl-application-2oic</link>
      <guid>https://dev.to/opossumpetya/use-notepad-as-a-gui-for-your-perl-application-2oic</guid>
      <description>&lt;p&gt;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!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FOpossumPetya%2FFixedWidthDataReader%2Fraw%2Fmain%2Fmisc%2Ftest-npp-animation.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FOpossumPetya%2FFixedWidthDataReader%2Fraw%2Fmain%2Fmisc%2Ftest-npp-animation.gif" alt="Example of Notepad++ interfacing with a Perl program"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's how, plus a few use case ideas below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Meet Pork to Sausage
&lt;/h2&gt;

&lt;p&gt;This creatively named &lt;a href="https://github.com/npp-plugins/pork2sausage" rel="noopener noreferrer"&gt;Notepad++ plugin&lt;/a&gt; allows you to pass selected text from the editor to any command line tool, and replace it with the output of that tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  Perl Sample Code
&lt;/h2&gt;

&lt;p&gt;Text manipulation? Of course we'll use Perl, what else?!&lt;/p&gt;

&lt;p&gt;Here's a poor man's hex viewer (you probably already getting a hint where I am on the richness spectrum):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nv"&gt;strict&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nv"&gt;warnings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nv"&gt;feature&lt;/span&gt; &lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;say&lt;/span&gt;&lt;span class="p"&gt;';&lt;/span&gt;

&lt;span class="c1"&gt;# hex representation of a string: 'ABC' -&amp;gt; '41 42 43'&lt;/span&gt;
&lt;span class="k"&gt;sub &lt;/span&gt;&lt;span class="nf"&gt;str2hex&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;uc&lt;/span&gt; &lt;span class="nb"&gt;join&lt;/span&gt; &lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="p"&gt;',&lt;/span&gt; &lt;span class="nb"&gt;unpack&lt;/span&gt; &lt;span class="p"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;(A2)*&lt;/span&gt;&lt;span class="p"&gt;",&lt;/span&gt; &lt;span class="nb"&gt;unpack&lt;/span&gt; &lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;H*&lt;/span&gt;&lt;span class="p"&gt;',&lt;/span&gt; &lt;span class="nb"&gt;shift&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# removes CR and LF (whatever is present) from the end of a string&lt;/span&gt;
&lt;span class="c1"&gt;# NP++ is a Windows tool, but who knows where that file came from!&lt;/span&gt;
&lt;span class="k"&gt;sub &lt;/span&gt;&lt;span class="nf"&gt;chomp_any&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nv"&gt;$ret&lt;/span&gt; &lt;span class="o"&gt;=~&lt;/span&gt; &lt;span class="sr"&gt;s/\r?\n\z//&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$ret&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# that highlighted text from NP++&lt;/span&gt;
&lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$str_from_npp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$ARGV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;# process it one line at a time&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$line&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;split&lt;/span&gt; &lt;span class="sr"&gt;/^/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$str_from_npp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;say&lt;/span&gt; &lt;span class="nv"&gt;str2hex&lt;/span&gt; &lt;span class="nv"&gt;chomp_any&lt;/span&gt; &lt;span class="nv"&gt;$line&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A quick test from the command prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;PS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;C:\dev\demo&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;perl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;\hex-view.pl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ABC"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;41&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;43&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configure the Plugin
&lt;/h2&gt;

&lt;p&gt;To use our program from within Notepad++ we must add it to the previously mentioned Pork to Sausage plugin (the plugin should be already &lt;a href="https://npp-user-manual.org/docs/plugins/" rel="noopener noreferrer"&gt;installed&lt;/a&gt;) config file, which is located at &lt;code&gt;%AppData%\Notepad++\plugins\config\pork2Sausage.ini&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[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\
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I use Strawberry Perl portable, and it's location on my machine here: &lt;code&gt;C:\dev\_tools\perl\&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And that's it!&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;You don't need to restart the editor if you need to update your Perl script.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage Ideas
&lt;/h2&gt;

&lt;p&gt;Here are a few use cases you may find useful for interfacing NP++ with a command line tool:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Inserting external data:&lt;/strong&gt; weather, currency conversions, whole snippets, etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2: &lt;strong&gt;Completing data&lt;/strong&gt; 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.&lt;/p&gt;

&lt;p&gt;3: &lt;strong&gt;Formatting text data&lt;/strong&gt;: maybe something like the Hex viewer example above, or something more complicated, like my &lt;a href="https://github.com/OpossumPetya/FixedWidthDataReader" rel="noopener noreferrer"&gt;FixedWidthDataReader&lt;/a&gt; tool, that is showcased in the animation at the beginning of this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Pack your Perl program to Windows executable using &lt;a href="https://metacpan.org/pod/pp" rel="noopener noreferrer"&gt;&lt;code&gt;PAR::Packer&lt;/code&gt;'s &lt;code&gt;pp&lt;/code&gt;&lt;/a&gt; for easy distribution:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pp -o cool-tool.exe cool-tool.pl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the plugin config could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Cool Tool]
progPath=C:\CoolTool\cool-tool.exe
progCmd=cool-tool.exe --some-args $(SELECTION)
workDir=C:\CoolTool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;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.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="nb"&gt;open&lt;/span&gt; &lt;span class="k"&gt;my&lt;/span&gt; &lt;span class="nv"&gt;$fh&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;:crlf&lt;/span&gt;&lt;span class="p"&gt;',&lt;/span&gt; &lt;span class="o"&gt;\&lt;/span&gt;&lt;span class="nv"&gt;$multiline_string_from_npp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope you will find this useful!&lt;/p&gt;

</description>
      <category>perl</category>
      <category>notepadplusplus</category>
      <category>npp</category>
      <category>porktosausage</category>
    </item>
  </channel>
</rss>
