Run and check Perl 5.38 scripts safely from the shell
You will use the installed Perl interpreter for a quick one-liner, create a small script, check it without running it, and then execute it with useful warnings enabled. Allow about 15 minutes. The examples use only a working directory and do not need sudo.
The route
Jump straight to the step you need, or tick off Done means at the end.
- 1. Confirm the interpreter and version
- 2. Try a one-liner without creating a file
- 3. Create a small script with warnings and strict checks
- 4. Check syntax before running the script
- 5. Run the script and pass an argument
- 6. Understand warnings and the tempting -w shortcut
- 7. Inspect the library search path when a module is missing
- 8. Recover from a failed check without losing the original
1. Confirm the interpreter and version
Start by checking which executable your shell will run. This avoids debugging a script against a different Perl installation from the one used in production.
$ command -v perl
/usr/bin/perl
$ perl -V:version
version='5.38.2';
On this Ubuntu installation, the executable comes from package libperl5.38t64:amd64, version 5.38.2-3.2ubuntu0.6. The interpreter identifies itself as Perl 5.38.2, built for x86_64-linux-gnu-thread-multi. Package revisions and build details vary, so record this check when a script depends on a particular environment.
2. Try a one-liner without creating a file
The -e option supplies program text on the command line. Quote the program with single quotes so the shell does not expand Perl variables or interpret punctuation before Perl receives it.
$ perl -e 'print "hello from Perl\n"'
hello from Perl
This is a good smoke test for the interpreter. It does not read standard input and it leaves no file behind. For anything longer than a short experiment, use a script file: shell quoting becomes a distraction and mistakes are harder to review.
3. Create a small script with warnings and strict checks
Make a file in a disposable working directory. use strict catches several accidental uses of variables and other legacy constructs. In Perl 5.38, use warnings is enabled by default from use v5.35 or higher, but writing it explicitly makes the script's intent visible and keeps the habit portable to older interpreters.
$ mkdir -p ~/perl-check
$ cd ~/perl-check
$ cat > greet.pl <<'PERL'
use strict;
use warnings;
my $name = shift // 'world';
print "Hello, $name\n";
PERL
The quoted here-document marker matters. It prevents the shell from expanding $name while the file is being created. If you use a different directory, substitute its path consistently; do not paste an untrusted value into shell syntax.
4. Check syntax before running the script
Use -c for a compile-only check. Perl parses the file and reports whether its syntax is valid, but it does not perform the script's normal run.
$ perl -c greet.pl
greet.pl syntax OK
A syntax error includes a line number and a nearby token. Fix the reported line, then repeat the exact check. Do not treat a clean compile as proof that the program's output or file handling is correct: compile-only checks do not replace a controlled run.
5. Run the script and pass an argument
Put the script name after Perl's options. Arguments after the script are available through @ARGV; the example uses shift to take the first one.
$ perl greet.pl 'Ada'
Hello, Ada
$ perl greet.pl
Hello, world
Keep the argument quoted when it contains spaces or shell punctuation. If the script name or an argument begins with a hyphen, use -- before the script or argument boundary as appropriate. This prevents a value from being mistaken for another Perl option. Never build a Perl command by concatenating untrusted input into its option area.
6. Understand warnings and the tempting -w shortcut
The manual documents -w, but recommends the use warnings pragma instead. The command-line flag applies warnings to all Perl code that runs, including code outside your control. That can make a dependency noisy or expose warnings you cannot fix in the current script.
$ perl -we 'my $x = 2 + 3; print "sum=$x\n"'
sum=5
This harmless command demonstrates the flag, not a preferred project setup. Keep use warnings in code you own, where its scope is visible. If output unexpectedly contains warnings, reproduce the command without suppressing them and inspect the line named in the diagnostic.
7. Inspect the library search path when a module is missing
Perl searches the directories in its built-in @INC list for libraries and modules. A module error can therefore mean that the module is absent, or that the command is using a different interpreter or environment.
$ perl -e 'print join("\n", @INC), "\n"'
/etc/perl
/usr/local/lib/x86_64-linux-gnu/perl/5.38.2
/usr/local/share/perl/5.38.2
/usr/lib/x86_64-linux-gnu/perl5/5.38
/usr/share/perl5
/usr/lib/x86_64-linux-gnu/perl-base
/usr/lib/x86_64-linux-gnu/perl/5.38
/usr/share/perl/5.38
The exact directories depend on the host. For a one-off private library, the interpreter also accepts -I<dir>, but prefer a clearly managed environment over silently changing search paths in a production command. Check perl -V when you need the full configuration rather than only the version.
8. Recover from a failed check without losing the original
Syntax checking changes nothing. Running a script may change files, call services or process input, depending on the code. Before running an unfamiliar script, read it, work on a copy of important input, and confirm its output paths. Do not run unknown Perl code as root.
If you need to edit the example, keep the original until the replacement passes its check:
$ cp --preserve=all greet.pl greet.pl.bak
$ editor greet.pl
$ perl -c greet.pl
$ perl greet.pl 'Ada'
Hello, Ada
If the edited version is wrong, restore the known copy with cp --preserve=all greet.pl.bak greet.pl, then run the compile check again. Remove the backup only after you have verified the replacement and no longer need recovery.
Done means
command -v perlandperl -V:versionidentify the interpreter you intended to use.- A short
-ecommand runs with predictable shell quoting. - Your script has explicit
use strictanduse warningswhere appropriate. perl -creportssyntax OKbefore the script is run.- You understand where arguments and module search paths come from.
- Important inputs and scripts can be restored from a backup, and no example requires elevated privileges.