Reputation: 648
How do you type and then (execute/run) Perl statements directly in the Perl shell/interpreter?
First, I'm on Windows... and I have Strawberry Perl (v5.32.1) built for MSWin32-x64-multi-thread installed.
So, if I type at the command line, just :
perl
... it appears to enter into a Perl "shell/interpreter", but if I enter some Perl statements :
my $greeting = "Hello World :-)\n";
print($greeting);
... how do I make it then "execute/run" those two statements?
If I Ctrlc, then it just says: Terminating on signal SIGINT(2)
If it matters, the reason I'd like to do this is so that I can just test Perl language as I'm learning about Perl.
Upvotes: 4
Views: 2285
Reputation: 52344
You can get a REPL using the debugger:
$ perl -d -e 1
Loading DB routines from perl5db.pl version 1.49
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(-e:1): 1
DB<1> x "blah" x 5
0 'blahblahblahblahblah'
Use x expression
or p expression
to evaluate an expression and display the result in different ways.
Upvotes: 3
Reputation: 67900
You can just use a so-called one-liner (*) and type code directly into the shell. It is the idiomatic way to test Perl statements:
perl -we"my $greeting = qq(Hello World :-)\n); print $greeting;"
Note that in Windows cmd shell you need to surround the code with double quotes, hence you use qq()
for double quoted strings inside the code.
I always use the -l
switch on the command line, so that I don't have to add line endings to print:
perl -lwe"my $greeting = 'Hello World :-)'; print $greeting;"
You may also consider using -E
and say
, which adds a newline as well:
perl -wE"say 'Hello world :-)'"
You can even use multiple lines, in some shells, though not in Windows.
(*) = Don't be fooled by the word "one-liner". That is not the number of statements you can use, that is just a way to name a statement that is on "one line". In Perl you can add multiple statements on a single line if you like.
Upvotes: 1
Reputation: 241808
Entering Ctrl + Z (final Enter still needed) corresponds to Ctrl + D on *nix and means End of file. You can also enter __END__
.
Upvotes: 3