Perl Editing

Introduction

Installing Strawberry Perl is relatively easy, but how to create and edit the scripts?


Visual Studio Code

Perl scripts are just plain text files so can be edited in almost anything, Notepad++, Notepad and so on. I wanted something a little different and so chose Visual Studio Code, sometimes known as VS Code or just VSC.


VS Code Perl Extensions

There are extensions for Visual Studio Code to make it far more useful for writing Perl scripts. Opening up View > Extensions in VSC and typing Perl, there appeears to be any number of extensions for it.

Visual Studio Code Perl extensions

Visual Studio Code Perl extensions

After a bit of reading I chose two:

Perl Navigator

Perl Navigator provides syntax checking, autocompletion and more.

The instructions say that "Install the VSCode extension and it should just work.", except it did not. Nothing it should have been doing in the short video on the Visual Studio Marketplace page was happening. It turns out I had to edit the extension's settings a little. Showing the installed extensions in VS Code, click on the tools setting for Perl Navigator. Scroll down to the perlnavigator.perlPath section and put the full absolute path to Perl interpreter, including the interpreter's name. In my case this was c:\perl\perl\bin\perl. After doing that, the extension worked exactly as promised.

Perl Navigator settings

Perl Navigator settings

Perl Debug Adapter

Perl Debug Adapter by Johannes Ritter invokes perl -d and handles communication with VS Code. This means the VS Code debugger works properly with Perl code. Once installed, it required no further set up.


Standard Practices

Take a look at the following script:

#!C:/Strawberry/perl/bin/perl.exe -T

use strict;
use warnings;

print "Hello World.\n";

The first line is called the shebang and contains the path to the Perl interpreter. It is not necessary for Windows scripts, but is for those running on Linux. The line also contains any Perl command switches, in this case -T which invokes taint mode. Taint mode makes Perl perform certain security checks. PerlDocs says this about taint mode:

This command is strongly suggested for server programs and any program run on behalf of someone else, such as a CGI script. Once taint mode is on, it's on for the remainder of your script.

While in this mode, Perl takes special precautions called taint checks to prevent both obvious and subtle traps. Some of these checks are reasonably simple, such as verifying that path directories aren't writable by others.

Set strict enforces the declaration of variables.

Set warnings finds typing mistakes in the script such as a missed semicolon, you used 'elseif' and not 'elsif', and so on. The command will simply warn of an error but using:

use warnings FATAL => 'all';

will end execution of the script.


Including Modules

Perl's PM files are modules are a collection of related subroutines and variables that perform a task. Modules may be dependent on other modules being present to run. Strawberry Perl includes a file named distributions txt that lists all 439 modules included with version 5.38.2.2-64bit.

There are two methods of including them in a Perl script, use and require, of these use is usually used.

Michael Carman describes the use of them like this:

use Module is the normal way of using code from a module. Note that Module is the package name as a bareword and not a quoted string containing a file name. Perl handles the translation from a package name to a file name for you. use statements happen at compile time and throw an exception if they fail. This means that if a module your code depends on isn't available or fails to load the error will be apparent immediately. Additionally, use automatically calls the import() method of the module if it has one which can save you a little typing.

require Module is like use Module except that it happens at runtime and does not automatically call the module's import() method. Normally you want to use use to fail early and predictably, but sometimes require is better. For example, require can be used to delay the loading of large modules which are only occasionally required or to make a module optional. (i.e. use the module if it's available but fall back on something else or reduce functionality if it isn't.)

Strictly speaking, the only difference between require Module and require 'file' is that the first form triggers the automatic translation from a package name like Foo::Bar to a file name like Foo/Bar.pm while the latter form expects a filename to start with. By convention, though, the first form is used for loading modules while the second form is used for loading libraries.

An example of using a module in a script would be:

use Math::Calc;


Sources & Resources

In Perl, is it better to use a module than to require a file? (Stack Overflow}
Perl Debug Adapter- Visual Studio Code Perl extension to provide Perl debugging capabilities.
Perl Navigator - Visual Studio Code Perl extension to provide syntax checking, autocompletion and more.
Setting Up Perl Development Environment (Perl Tutorial)
Taint mode (Perl Docs)
Visual Studio Code