Visual Studio Code for Perl on Legacy Systems

Photo by Markus Spiske on Unsplash

Within the lives of software developers, we sometimes need to veer off our path and take a side route. In this particular case, I needed to review some Perl code. I was pretty confident that Visual Studio Code for Perl should be a no-brainer however I overlooked the fact that the corporate system I was running had only Perl 5.16 where the Visual Studio Code extension required 5.18. Unfortunately, this is the situation many developers face as corporate systems are refreshed at a slower pace that developers would like.

Managing Perl versions with plenv

Having worked with pyenv for Python, this was an easy transition to plenv for Perl. plenv functions similarly to pyenv and allows the user to easily maintain multiple versions of Perl. The only limitation I could tell is that there isn’t a way to establish virtual environments where a developer can have multiple environments of the exact same version. I’ve seen some scripts to manage this but didn’t explore.

There isn’t a plenv-installer so the initial part will be manual. The instructions are fairly straightforward so just follow along. The only change I made was to move plenv initialization to a separate script so I can manually enable when I need to debug Perl.

$ git clone https://github.com/tokuhirom/plenv.git ~/.plenv
$ echo 'export PATH="$HOME/.plenv/bin:$PATH"' >> ~/.plenv-init
$ echo 'eval "$(plenv init -)"' >> ~/.plenv-init
$ source ~/.plenv-init

After this has been completed, install the version of your choice. For myself, I installed the latest stable release at time of writing which is 5.34.1 (odd minor versions such as 5.35.11 are considered unstable development releases).

$ git clone https://github.com/tokuhirom/Perl-Build.git ~/.plenv/plugins/perl-build/
$ plenv install 5.34.1
$ plenv rehash

Installing the Perl Language Server for Visual Studio Code

In order to use a Perl debugger, we need to install a language server. For Perl, a fully functional language server is implemented in Perl::LanguageServer. We’ll first need to switch to the installed version of Perl and then install the language server.

$ plenv global 5.34.1
$ perl -v

This is perl 5, version 34, subversion 1 (v5.34.1) built for x86_64-linux
$ cpan Perl::LanguageServer

Once the installation has been completed, we can check if the module was successfully built and installed.

$ plenv list-modules
...
Perl::LanguageServer
...

Installing the Visual Studio Code for Perl Extension

The extension for VS code that is used by the community is Perl. Search for “perl” in the Extensions and the install the one published by Girald Richter. Once this is installed, we’ll need to add the path of the language server and supporting modules to @INC. This is done by modifying the VS Code settings.json file and adding the following JSON snippet.

    "perl.perlInc": [
        "/home/chinghwa/.plenv/versions/5.34.1/lib/perl5/site_perl/5.34.1/",
        "/home/chinghwa/.plenv/versions/5.34.1/lib/perl5/site_perl/5.34.1/x86_64-linux/"
    ],

After adding this, VS Code will be able to locate the language server but it will continue to use the system perl. We’ll need to instruct VS Code to use the perl version installed through plenv by adding the following JSON snippet to settings.json.

"perl.perlCmd": "/home/chinghwa/.plenv/versions/5.34.1/bin/perl",

You’ll want to check your launch.json to make sure that the Perl debugging configuration was created.

        {
            "type": "perl",
            "request": "launch",
            "name": "Perl-Debug",
            "program": "${workspaceFolder}/${relativeFile}",
            "stopOnEntry": true,
            "reloadModules": true,
        }

Add additional configurations as needed. For example, if you’re passing in arguments, add another configuration with with an “args” key.

Once this has been completed, open a Perl file and start debugging!

Leave a Reply