Collecting my attempts to improve at tech, art, and life

NQP - Not Quite Perl

Tags: parrot perl coolnamehere

Does any of this enlighten, entertain, or otherwise please you? Please consider a Tip. Every little bit helps!

Parrot is more than just PIR and PASM. I’m not talking about the ability to use languages like Raku written for the Parrot virtual machine. I am also not talking about the ability to write your own language. Both of those are quite nifty, of course. It is fair to say that those two items are probably why you are experimenting with Parrot in the first place. However, the Parrot distribution also ships with an extra language: NQP.

NQP—Not Quite Perl—is an implementation of a small subset of Perl 6 that can be used as a higher level Parrot language than PIR. It is especially useful in defining the grammars for your Parrot languages.

Getting NQP

NQP ships with Parrot!

Example

Let’s just do a brutally fast NQP example.

Code Sample
    #!/usr/local/bin/parrot-nqp

my $name := get_input("What is your name?");
say("Hello $name");

my $valid_input := 0;

while ($valid_input == 0) {
	my $in_good_mood := get_input("Are you in a good mood?[y/n]");
	if ($in_good_mood eq "y") {
		say("Glad to hear it! Must be all the Parrot hacking.");
		$valid_input := 1;
	} elsif ($in_good_mood eq "n") {
		say("Oh, that's too bad. Try hacking on Parrot.");
		$valid_input := 1;
	} else {
		say("Sorry, I'm not too bright. Please answer 'y' or 'n'.");
	}
}

sub get_input($prompt) {
	print("$prompt ");
	my $name := Q:PIR{
		.local pmc stdin
		stdin = getstdin
		%r = stdin.'readline_interactive'()
	};

	return $name
}
  

We see that NQP variables look a little like Perl variables, and familiar control structures like if and while are supported. Another bit of niftiness is that inline PIR is supported.

Code Sample
    $ parrot-nqp hello.nqp
What is your name? Brian
Hello Brian
Are you in a good mood?[y/n] waffles
Sorry, I'm not too bright. Please answer 'y' or 'n'.
Are you in a good mood?[y/n] y
Glad to hear it! Must be all the Parrot hacking.

  

Mind you, NQP is not really great for casual scripting. It’s intended to be a higher level bootstrap language that makes it easier to define grammars. I wouldn’t go complaining to anybody that it’s missing Feature X. Unless you know how to implement it. Maybe. If you want Feature X, it might be better to use Rakudo or implement it in your own Parrot language.

You could even use NQP to implement it. Is my logic circular enough for you?

Learning NQP

There is a good overview of NQP available at wikibooks.