4

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.

George 2.0 Hope
  • 583
  • 1
  • 6
  • 21
  • 1
    Try `Ctrl+Z`, it should be end-of-file on MSWin (`Ctrl+D` on *nix). Or enter `__END__`. – choroba Jul 09 '21 at 17:42
  • There's a `re.pl` script that comes with [Devel::REPL](https://metacpan.org/pod/Devel%3A%3AREPL) that you might find handy. – Jim Davis Jul 09 '21 at 17:50
  • 1
    Keep in mind that `perl` is not a REPL. It's the Perl compiler, so it expects a full program. You can't run line by line. The easiest way to run stuff on the fly is to use an IDE and to configure a shortcut to run your current file. On VSCode I use [code runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner). Or you can save, `Alt+Tab` and `up` and `enter` to rerun in the terminal. – simbabque Jul 09 '21 at 17:51
  • 1
    You can just run perl code as a one-liner: `perl -we"print qq(Hello world!\n)"` – TLP Jul 09 '21 at 17:56
  • 1
    In Windows cmd, you need to use double quotes to surround the code. Hence, you need to use `qq()` for double quoted strings. I believe people rarely use the shell interpretation, like the one people use for python for example. – TLP Jul 09 '21 at 17:58
  • Thanks @JimDavis && simbabque for your suggestions, good ideas for alternatives. – George 2.0 Hope Jul 09 '21 at 17:59
  • @TLP I needed multiple statements, but now I know how to do a one-liner, thanks. – George 2.0 Hope Jul 09 '21 at 17:59
  • @George2.0Hope You can do multiple statements in one-liners.... You can do as many commands as you can with a program file, you just put them on "one line" because you can do that in Perl. Heck, you can even add line endings with backslash in some shells. – TLP Jul 09 '21 at 18:00
  • `perl -we"print qq(Hello world\n); print qq(Hey look, you can do multiple statements!\n); for (0 .. 10) { print }"` – TLP Jul 09 '21 at 18:02
  • @choroba if you convert your comment to an answer, and make one small change (*when you press Ctrlz, it puts `^Z` in the terminal window, you still need to press Enter after it ... also `__END__`Enter works as well*), I'll mark it as correct. Thanks. – George 2.0 Hope Jul 09 '21 at 18:03

3 Answers3

3

Entering Ctrl + Z (final Enter still needed) corresponds to Ctrl + D on *nix and means End of file. You can also enter __END__.

choroba
  • 231,213
  • 25
  • 204
  • 289
2

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.

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • That doesn't seem to be what's asked. If so, they should consult [How can I start an interactive console for Perl?](https://stackoverflow.com/a/73703/589924) – ikegami Jul 09 '21 at 21:08
1

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.

TLP
  • 66,756
  • 10
  • 92
  • 149