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.
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.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How do you start an interactive Perl scratch pad?
Answer
perl -de 1The
-dflag starts the debugger and-e 1gives it a program that does nothing, so every line you type is a statement of your own. -
What does the
pcommand of the debugger do?Answer
It prints the value of the Perl expression that follows it
pevaluates one expression and prints its value on the next line, which is the whole point of the prompt. -
What does the
xcommand do, and why is it better for a hash?Answer
It dumps a reference with its keys and values, one pair per line
A hash printed as a string loses the pairing, while
xshows each key with its value. -
Two runs of
x { name => "Ada", age => 36 }print the same keys but a different number after HASH. Why?Answer
That number is the address of the hash in memory, which changes from run to run
The hex number is an address, so the page shows it as an example rather than as output you can compare.
-
Which command leaves the debugger?
Answer
qqquits the debugger; Ctrl+D on an empty prompt does the same. -
What does
perl -Egive you thatperl -edoes not?Answer
It enables the modern features, so
sayprints a line with its newline-Eturns on the feature bundle of the running Perl, which is what makessayavailable in a one-liner. -
What does this program print?
print "hi" x 3, "\n";Answer
hihihi
The
xoperator repeats the string on its left as many times as the number on its right gives. -
What does this program print?
my %h = (name => "Ada", age => 36); print join("-", sort keys %h), "\n";Answer
age-name
sortputs the keys in string order, and a comes before n, as the debugger dump on the page shows too. -
What does this program print?
print join("-", qw(a b c)), "\n";Answer
a-b-c
joinputs the separator between the elements only, so the newline stays outside the joined string. -
Why does the page put the code of
perl -E 'say 2 + 2'in single quotes?Answer
The shell would expand
$variables in the code before Perl ever sees themThe shell interpolates
$nameand backticks inside double quotes, so the code would arrive changed; single quotes pass it through untouched.
Article author: Arthur Isaev