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.
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: