I’m somewhat amused at one of the more recent comments here – Nathan L. Walls defends his "choice" of Ruby with some very woolly justifications (emphasis mine):
"Ruby’s community feels more vibrant. No, not something you can measure. It is a feeling."
"Yes, there are equivalents in Perl, but they are far rougher. Again, not really measurable, but a feeling."
Of course, his day job is still writing Perl. Moving swiftly on…
Devel::Repl
The main thing I got out of the comment apart from a chuckle, was it motivated me to look at Devel::REPL.
One of the other main tools in my toolbox is emacs and when writing emacs lisp, I make full use of the REPL. But I’ve never even wanted an equivalent in Perl.
One cpanm invocation later and I’m ready.
Wait, no I’m not. I copied Caleb’s repl.rc config to make it more usable. I added MultiLine::PPI which resulted in a bunch of errors at start-up. It turns out I need to add File::Next and B::Keywords separately.
$ cpanm File::Next $ cpanm B::Keywords
Okay, now I’m good to go.
First REPL session
$ jared@localhost $ re.pl
$ sub f
$ {
> say 'h';
> say 'hello';
> }
h
hello
1 $ f();
Runtime error: Undefined subroutine &Devel::REPL::Plugin::Packages::DefaultScratchpad::f called at (eval 290) line 5.
$ sub f {
> say 'h';
> say 'hello';
> }
$ f();
h
hello
1 $
It still isn’t quite perfect. But to be honest, I find it (and the Python and irb REPLs) pretty useless. I probably need to look into integrating it with emacs comint.
I always just do ‘perl -de0′ for that sort of thing.
Sepia and PDE, both available on CPAN, already have REPLs and integrate with Emacs.
The thing is, a single sub declaration is valid Perl code, useful for those rare cases when you have something like a recursive prototyped function. It’s called a “forward” declaration by perlsub.
Simple example (warning! funky code ahead):
perl -Mstrict -Mwarnings -E ‘sub foo; sub foo; sub foo;’
of course, it’s valid Perl, because you’re just declaring a function, not defining it.
perl -Mstrict -Mwarnings -E ‘sub foo; sub foo; sub foo; foo(42)’
Undefined subroutine &main::foo called at -e line 1.
And now you probably now what went wrong inside the REPL as well.
The shell prefix should have given you enough of a hint, take a look at how it stays a “$” after you declare your sub:
$ sub f
$ {
and changes to a long-call “>” prefix in your latter example:
$ sub f {
> say ‘h’;
what the REPL did on the first try was declare a sub, then go inside a block – just notice how it processes and return the block right after you enter “}”, which does not happen on the second try.
Granted, a REPL specific warning about sub declaration without definition would be very helpful, specially to REPL newcomers.
Anyway, nice article!
Hi Sue,
I had always used perl -d -e 1 too. It looks like there are some advantages to using re.pl if you like that sort of thing though.
@Anonymous – I’ll take a look at those. Thanks for the tip.
@garu – yes I noticed that the prompt hadn’t changed. However, clearly the re.pl has done the wrong thing. I didn’t finish the statement with a semi-colon so it should have been able to detect the continuation.