x
Loading
 Loading
Hello, Guest | Login | Register

Programming Reusable PIR: Learn how to write and test PIR subroutines

PIR is Parrot’s native programming language. In this conclusion of his series, chromatic presents a variety of techniques to program and validate subroutines. Learn how to create libraries and test your code in this hands-on primer.

PIR is Parrot’s native programming language. In this conclusion of his series, chromatic presents a variety of techniques to program and validate subroutines. Learn how to create libraries and test your code in this hands-on primer.

The first article in this series explained Parrot and its philosophy. The second article introduced PIR, Parrot’s native programming language. PIR is a line-oriented assembly language. Even though it has plenty of high level features, everything is either a compiler directive or an opcode.

To recap, a simple program is:

 .sub 'greet_and_count' :main .local string hello hello = "Hello, world!" .local int num_chars num_chars = hello print "'" print hello print "' has " print num_chars print "charsn" .end 

Each line starting with a period contains a compiler directive (.sub, .local), and every remaining line is an opcode (print, or == which translates to the set opcode).

Parrot can do much more.

Powerful, Powerful Subroutines

Until now, all of the PIR subroutines shown have used positional arguments. The arguments you pass to a subroutine map directly to the parameter list inside the subroutine. Parrot also supports named arguments, where you can pass the arguments in any order as long as you give them appropriate names. It’s easier to show the example first:

  .sub 'show_stats' .param string name :named( 'name' ) .param int years :named( 'age' ) print name print " is " print years print " years old.\n" .end .sub 'main' :main show_stats( 'age' => 4,...

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace