Skip to main content
Perl

Syntax Highlighting Perl Scripts

By February 12, 2019September 12th, 2022No Comments

When you create you very own Perl scripts the big debate is to add .pl to the end or not! For example if you wanted to create a script to check the existence of a user in the Linux system you may want the program to be called check_user. You may want to call the script simply check_user or check_user.pl, but which is better? The bottom line often is allowing syntax highlighting Perl Scripts

Many people will choose adding the .pl suffix as most Linux distributions use file extensions to match syntax patterns. An extension of .pl will allow the correct syntax highlighting Perl scripts. To get the syntax highlighting shown below on a standard Ubuntu distribution the file needs the .pl suffix.

However; you can create your own file type matches in vim. It is quite simple to allow the shebang to control syntax highlighting in Perl scripts. In your $HOME directory create a file called .vim adding the following content:

if did_filetype()
finish
endif
if getline(1) =~ '^#!/usr/bin/perl'
setfiletype perl
endif

Now it is the shebang that is used to set the filetype and not just the extension. A file called check_user that has the Perl shebang will offer the correct syntax highlighting. The script without the .pl extension or suffix is more simple for the user to understand and hides elements such as the script type which is not important to the user.