DEV Community

Dean Hamstead
Dean Hamstead

Posted on • Updated on

 

VSCode as a Perl IDE

Overview

The VSCode IDE implements it's language support via the Language Server Protocol which is designed to cleanly separate language support from the editor. For Perl a fully functional language server is implemented in Perl::LanguageServer and will work with any LSP capable editor.

VSCode can edit both locally or in a remote environment. This brief guide will get you started editing locally (i.e. on your desktop or laptop), however the Perl VSCode extension and Perl's LanguageServer will work in either scenario.

Install a Perl interpreter for development

Although a Perl interpreter is likely already installed (i.e. system perl) on Linux , BSD, or MacOS the best practice is to leave if for the system to use and install your own interpreter for your development work. On Windows you likely will install Strawberry Perl.

Please follow this guide to installing a perl interpreter for your development.

Install Perl's LanguageServer

Assuming cpanm is installed, simply run:

cpanm Perl::LanguageServer
Enter fullscreen mode Exit fullscreen mode

That's it. The Perl LanguageServer is now ready.

Install VSCode (actually VSCodium)

VSCodium is the fully-foss version of VSCode (think Chromium). I recommend you install VSCodium rather than VSCode.

Fedora, SuSE, and the Debian family of Linux distributions can install from this maintained repo.

For other OS including MacOS and Windows simply pick your favourite approach from the official install instructions

Install the Perl extension for VSCode

Run VSCode (VSCodium) and using the familiar "File -> Open" interface, open a Perl source file. Observe that whilst syntax highlighting is available already via the built-in "Perl Language Basics" plugin, actual IDE functions do not - i.e. try "Run -> Start Debugging".

We need to configure VSCode to use our Perl LanguageServer by installing the Perl extension. The good news is that the extension is in the official VS Marketplace making it very quick and easy.

In actual VSCode the quickest route is hit Ctrl-P then type:

ext install richterger.perl
Enter fullscreen mode Exit fullscreen mode

You could also click "File -> Preferences -> Extensions" then search for "perl" and click install.

In VSCodium the Open VSX registry is used which doesn't yet include the perl extensions.

From the VS Marketplace plugin page download the extension with the "Download Extension" link on the right hand side.

On your local system's command line (there is no need to close VSCodium), change directory to your downloads and run:

codium --install-extension richterger.perl-*.vsix 
Enter fullscreen mode Exit fullscreen mode

Returning to VSCode or VSCodium, observe that the Perl extension is now installed in the extensions list ("File -> Preferences -> Extensions" or Ctrl-Shift-X)

Open a Perl source file, click "Run -> Start Debugging" or hit F5 and observe there is no error as before.

Now explore all VSCocde IDE functions working nicely with Perl!

Top comments (23)

Collapse
 
johntocher profile image
John Tocher

This nearly got me there. Getting the perl language server installed was a pain point for me. For others stumbling on this page, on ubuntu linux 20.04 LTS, the process for me was:

(The following three may not be necessary if you've already got your computer configutred for development, but for a fresh isntall I was helping someone else with, this was necessary).

sudo apt update
sudo apt upgrade
sudo apt install build-essential

The missing library I needed was installed with:
sudo apt install libanyevent-perl libio-aio-perl

I found this from the following source if you would like more details:
github.com/richterger/Perl-Languag...

Then I could successfully install the language server with:
sudo cpan Perl::LanguageServer

VS code would then successfully install the plugin and run as expected.

Collapse
 
kaicarver profile image
Kai Carver

Thank you!

Collapse
 
peterdragon profile image
Peter Edwards

Nice article!
I've got VSC working from my Mac remoting into a virtualbox Linux VM.
Perl::LanguageServer was requiring perl 5.18.
We are using Centos/RHEL 7 with system perl 5.16.3 and I have modified Perl::LanguageServer so it will run on 5.16.
github.com/richterger/Perl-Languag...
Might of interest @davehodg.

Collapse
 
david_hodgkinson_8da1a1d0 profile image
DAVID HODGKINSON

No excuse. System perl is for the system. Brew a perl. We just did. It was easy.

Collapse
 
peterdragon profile image
Peter Edwards

Your customers are Security Cleared and require RedHat security releases.
Which include patches to perl modules to make the yum rpm modules safe?
So no to using stock CPAN perl modules. Thought not.
BTW hope you are keeping well ;^)

Thread Thread
 
david_hodgkinson_8da1a1d0 profile image
DAVID HODGKINSON

We’re a bank. With a legal department that cares about licensing. Does that help? We’re on recent Perl and recent CPAN.

Thread Thread
 
peterdragon profile image
Peter Edwards • Edited

Of course, explain it to our customers too. One is List X MOD. Maybe they will welcome your insight.
Within 30 minutes of posting I had another person replying they were trying my fix.
It doesn't do to assume everyone else has the freedom and expert knowledge you have to implement solutions.
One of the reasons perl is going down is no-one thinks about these "hygiene factors" [Henry Mintzberg] which actually put a lot of people off perl because they are: too unread, too stupid, too lazy. We should address that. Using the system perl is part of making people's lives easy. We don't really have a choice, unfortunately.

Collapse
 
andrewsolomon profile image
Andrew Solomon • Edited

There are two grant proposals in the area of Perl IDEs which have been bouncing around. If anyone's interested in working on one of these, please get in touch!

Collapse
 
toddbruner profile image
Todd Bruner

Great guide, thank you.
I followed this guide to get this working on a Ubuntu 22 VM.
At first many errors with Perl, LanguageServer, and many modules due to using a custom local perl instance installed in /opt/perl.
Here's what the settings.json file looks like to fix:
{
"perl.perlInc": [
"/opt/perl/lib/5.34.1",
"/opt/perl/lib/5.34.1/x86_64-Linux",
"/opt/perl/lib/site_perl/5.34.1",
"/opt/perl/lib/site_perl/5.34.1/x86_64-linux"
],
"perl.perlCmd": "/opt/perl/bin/perl"
}

Obvious, now, but hopefully this helps someone in the future.

Collapse
 
rhopkins7428 profile image
rhopkins7428

Love VSCode with Perl (even with Windows 10) , but can't get language server to work with Windows 10,(strawberry Perl) seems to be a known problem with running on windows platform

github.com/richterger/Perl-Languag...

Collapse
 
perldean profile image
Dean Hamstead

I dont personally use windows, so i have made an assumption that turns out to be wrong. Your best bet might be to develop in a remote linux or bsd vm. Be that actually remotely or on a vm running locally

Collapse
 
davehodg profile image
Dave Hodgkinson

Great article. Now I'd like a recipe book of useful things you can do with it. I've also got perl-toolbox installed which has a nice interface to perlcritic.

Collapse
 
sigzero profile image
sigzero

You can also install the extension by using the command palette and selecting "Extensions: Install from VSIX...".

Collapse
 
perldean profile image
Dean Hamstead

I did not know this, thanks!

Collapse
 
sigzero profile image
sigzero

You are very welcome.

Collapse
 
otalado profile image
otalado

I've tried the steps above in Widows11 both with VsCode and with VsCodium. Yet (in both), when I try to debug a Perl script it just shows the debug toolbar for a few seconds and nothing else happens. The VsCode version is 1.74.3 and the VsCodium is at the same version.
Jus to be sure I did not make any stuid errors, I uninstalled thr perl, VsCode and VsCodium and repeated whole procedure again, sadly with same result.
As I can run/debug perl scripts in Activestate Komodo I think perl is not the culprit.
Any ideas how should I troubleshoot?

Collapse
 
randalschwartz profile image
Randal L. Schwartz

Perl::LanguageServer must be rather persnickety to install. I have not had success installing this for either my primary macos system, or my client's centos machnine. Sad.

Collapse
 
adefaria profile image
Andrew DeFaria

The problem for me, Ubuntu 22.04, was that I didn't have the libperl-dev package installed. I found this by looking in the config.log for IO-AIO-4.76:

configure:2725: x86_64-linux-gnu-gcc -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fwrapv -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fwrapv -fno-strict-aliasing -pipe -I/usr/local/include -I/usr/lib/x86_64-linux-gnu/perl/5.34/CORE  -fstack-protector-strong -L/usr/local/lib -Wl,-E conftest.c -L/usr/lib/x86_64-linux-gnu/perl/5.34/CORE -L/usr/share/perl/5.34 -lperl -ldl -lm -lpthread -lc -lcrypt >&5
/bin/ld: cannot find -lperl: No such file or directory
collect2: error: ld returned 1 exit status
Enter fullscreen mode Exit fullscreen mode

Apparently libperl does not get installed unless you install the dev package for libperl. Kinda makes sense since this LanguageServer is about debugging your Perl scripts thus you're a dev.

Collapse
 
szabgab profile image
Gabor Szabo • Edited

There are markdown typos in two links and the article says "cpan Perl::LanguageServer" instead of "cpanm Perl::LanguageServer"

Collapse
 
perldean profile image
Dean Hamstead

Fixed. Thanks

Collapse
 
szabgab profile image
Gabor Szabo

Some of the links still show the markdown.

Thread Thread
 
perldean profile image
Dean Hamstead

I think i have got them all now.

Collapse
 
surajmandal profile image
Suraj_Mandal

I have successfully run the Perl::LanguageServer but not able to get the code-autocompletion feature

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git