The debugger as a REPL
Start the debugger
Perl has no separate interactive shell. The debugger does the job: point it at an empty program with the -d flag and you get a prompt that evaluates each line you type.
The number 1 is a program that does nothing, which is exactly what you want for a scratch pad:
perl -de 1
Loading DB routines from perl5db.pl version 1.82
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(-e:1): 1
DB<1>
Try expressions
The p command prints the value of a Perl expression. Type an expression after the prompt, and the debugger answers on the next line:
p 2 + 2
p "hi" x 3
p join "-", qw(a b c)
perl -de 1
DB<1> p 2 + 2
4
DB<2> p "hi" x 3
hihihi
DB<3> p join "-", qw(a b c)
a-b-c
DB<4>
The prompt shows what you typed and the answer follows on the line below it. For whole data structures the x command is easier to read: it dumps a reference with its keys and values instead of printing one flattened string.
x { name => "Ada", age => 36 }
perl -de 1
DB<1> x { name => "Ada", age => 36 }
0 HASH(0x7aa7774d8688)
'age' => 36
'name' => 'Ada'
DB<2>
The keys come out in alphabetical order, and the hexadecimal number is the address of the hash in memory, so it differs on every run. Older perls print the Data::Dumper layout instead, with $VAR1 around the braces.
Quick one-liners
For a single expression you do not even need the prompt. The -E flag runs code from the command line and enables the modern features, including say, which prints a line and adds the newline for you:
perl -E 'say 2 + 2'
4
Keep the code in single quotes so the shell leaves it alone. Double quotes would let the shell expand $ variables before Perl ever sees them.
Exit
The q command leaves the debugger. Ctrl+D on an empty prompt does the same, and so does exit on most terminals.
q
Nothing you type here is saved, so the debugger is the right place for quick checks. When an idea works, copy it into a script file and keep it.
Next: Scalars and sigils
Article author: Arthur Isaev