Perl – Formats

Perl – Formats ”; Previous Next Perl uses a writing template called a ”format” to output reports. To use the format feature of Perl, you have to define a format first and then you can use that format to write formatted data. Define a Format Following is the syntax to define a Perl format − format FormatName = fieldline value_one, value_two, value_three fieldline value_one, value_two . Here FormatName represents the name of the format. The fieldline is the specific way, the data should be formatted. The values lines represent the values that will be entered into the field line. You end the format with a single period. Next fieldline can contain any text or fieldholders. The fieldholders hold space for data that will be placed there at a later date. A fieldholder has the format − @<<<< This fieldholder is left-justified, with a field space of 5. You must count the @ sign and the < signs to know the number of spaces in the field. Other field holders include − @>>>> right-justified @|||| centered @####.## numeric field holder @* multiline field holder An example format would be − format EMPLOYEE = =================================== @<<<<<<<<<<<<<<<<<<<<<< @<< $name $age @#####.## $salary =================================== . In this example, $name would be written as left justify within 22 character spaces and after that age will be written in two spaces. Using the Format In order to invoke this format declaration, we would use the write keyword − write EMPLOYEE; The problem is that the format name is usually the name of an open file handle, and the write statement will send the output to this file handle. As we want the data sent to the STDOUT, we must associate EMPLOYEE with the STDOUT filehandle. First, however, we must make sure that that STDOUT is our selected file handle, using the select() function. select(STDOUT); We would then associate EMPLOYEE with STDOUT by setting the new format name with STDOUT, using the special variable $~ or $FORMAT_NAME as follows − $~ = “EMPLOYEE”; When we now do a write(), the data would be sent to STDOUT. Remember: if you are going to write your report in any other file handle instead of STDOUT then you can use select() function to select that file handle and rest of the logic will remain the same. Let”s take the following example. Here we have hard coded values just for showing the usage. In actual usage you will read values from a file or database to generate actual reports and you may need to write final report again into a file. Live Demo #!/usr/bin/perl format EMPLOYEE = =================================== @<<<<<<<<<<<<<<<<<<<<<< @<< $name $age @#####.## $salary =================================== . select(STDOUT); $~ = EMPLOYEE; @n = (“Ali”, “Raza”, “Jaffer”); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000); $i = 0; foreach (@n) { $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; } When executed, this will produce the following result − =================================== Ali 20 2000.00 =================================== =================================== Raza 30 2500.00 =================================== =================================== Jaffer 40 4000.00 =================================== Define a Report Header Everything looks fine. But you would be interested in adding a header to your report. This header will be printed on top of each page. It is very simple to do this. Apart from defining a template you would have to define a header and assign it to $^ or $FORMAT_TOP_NAME variable − Live Demo #!/usr/bin/perl format EMPLOYEE = =================================== @<<<<<<<<<<<<<<<<<<<<<< @<< $name $age @#####.## $salary =================================== . format EMPLOYEE_TOP = =================================== Name Age =================================== . select(STDOUT); $~ = EMPLOYEE; $^ = EMPLOYEE_TOP; @n = (“Ali”, “Raza”, “Jaffer”); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000); $i = 0; foreach (@n) { $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; } Now your report will look like − =================================== Name Age =================================== =================================== Ali 20 2000.00 =================================== =================================== Raza 30 2500.00 =================================== =================================== Jaffer 40 4000.00 =================================== Define a Pagination What about if your report is taking more than one page? You have a solution for that, simply use $% or $FORMAT_PAGE_NUMBER vairable along with header as follows − format EMPLOYEE_TOP = =================================== Name Age Page @< $% =================================== . Now your output will look like as follows − =================================== Name Age Page 1 =================================== =================================== Ali 20 2000.00 =================================== =================================== Raza 30 2500.00 =================================== =================================== Jaffer 40 4000.00 =================================== Number of Lines on a Page You can set the number of lines per page using special variable $= ( or $FORMAT_LINES_PER_PAGE ), By default $= will be 60. Define a Report Footer While $^ or $FORMAT_TOP_NAME contains the name of the current header format, there is no corresponding mechanism to automatically do the same thing for a footer. If you have a fixed-size footer, you can get footers by checking variable $- or $FORMAT_LINES_LEFT before each write() and print the footer yourself if necessary using another format defined as follows − format EMPLOYEE_BOTTOM = End of Page @< $% . For a complete set of variables related to formating, please refer to the Perl Special Variables section. Print Page Previous Next Advertisements ”;

Perl – Loops

Perl – Loops ”; Previous Next There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages − Perl programming language provides the following types of loop to handle the looping requirements. Sr.No. Loop Type & Description 1 while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. 2 until loop Repeats a statement or group of statements until a given condition becomes true. It tests the condition before executing the loop body. 3 for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. 4 foreach loop The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. 5 do…while loop Like a while statement, except that it tests the condition at the end of the loop body 6 nested loops You can use one or more loop inside any another while, for or do..while loop. Loop Control Statements Loop control statements change the execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Perl supports the following control statements. Click the following links to check their detail. Sr.No. Control Statement & Description 1 next statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. 2 last statement Terminates the loop statement and transfers execution to the statement immediately following the loop. 3 continue statement A continue BLOCK, it is always executed just before the conditional is about to be evaluated again. 4 redo statement The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed. 5 goto statement Perl supports a goto command with three forms: goto label, goto expr, and goto &name. The Infinite Loop A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty. #!/usr/local/bin/perl for( ; ; ) { printf “This loop will run forever.n”; } You can terminate the above infinite loop by pressing the Ctrl + C keys. When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but as a programmer more commonly use the for (;;) construct to signify an infinite loop. Print Page Previous Next Advertisements ”;

Parrot – Useful Resources

Parrot – Useful Resources ”; Previous Next The following resources contain additional information on Parrot. Please use them to get more in-depth knowledge on this topic. Useful Links on Parrot Parrot official websites This site is maintaining updated information related to Parrot. Source Code Packages & Source Code for Parrot Tracking Tools Parrot Testing Status Tools Document our testing coverage Parrot Issue Tracker The Parrot Bugtracking System Parrot Issue Tracker: Summary Report Breakdown of tickets by age, platform, etc. Open Patches Patches to get applied Glossary A glossary of important parrot terms Patch & Bug Report Submission Information A brief on patch submission Useful Books on Parrot To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;

Parrot – Garbage Collection

Parrot – Garbage Collection ”; Previous Next Like Java Virtual Machine, Parrot also keep you free from worrying about memory de-allocation. Parrot provides garbage collection. Parrot programs do not need to free memory explicitly. Allocated memory will be freed when it is no longer in use i.e. no longer referenced. Parrot Garbage Collector runs periodically to take care of unwanted memory. Print Page Previous Next Advertisements ”;

Parrot – Operations

Parrot – Operations ”; Previous Next There are a variety of operations you can perform. For instance, we can print out the contents of a register or a constant: set I1, 10 print “The contents of register I1 is: ” print I1 print “n” The above instructions will result in The contents of register I1 is: 10 We can perform mathematical operations on registers: # Add the contents of I2 to the contents of I1 add I1, I1, I2 # Multiply I2 by I4 and store in I3 mul I3, I2, I4 # Increment I1 by one inc I1 # Decrement N3 by 1.5 dec N3, 1.5 We can even perform some simple string manipulation: set S1, “fish” set S2, “bone” concat S1, S2 # S1 is now “fishbone” set S3, “w” substr S4, S1, 1, 7 concat S3, S4 # S3 is now “wishbone” length I1, S3 # I1 is now 8 Print Page Previous Next Advertisements ”;

Perl – Date & Time

Perl – Date and Time ”; Previous Next This chapter will give you the basic understanding on how to process and manipulate dates and times in Perl. Current Date and Time Let”s start with localtime() function, which returns values for the current date and time if given no arguments. Following is the 9-element list returned by the localtime function while using in list context − sec, # seconds of minutes from 0 to 61 min, # minutes of hour from 0 to 59 hour, # hours of day from 0 to 24 mday, # day of month from 1 to 31 mon, # month of year from 0 to 11 year, # year since 1900 wday, # days since sunday yday, # days since January 1st isdst # hours of daylight savings time Try the following example to print different elements returned by localtime() function − Live Demo #!/usr/local/bin/perl @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); print “$mday $months[$mon] $days[$wday]n”; When the above code is executed, it produces the following result − 16 Feb Sat If you will use localtime() function in scalar context, then it will return date and time from the current time zone set in the system. Try the following example to print current date and time in full format − Live Demo #!/usr/local/bin/perl $datestring = localtime(); print “Local date and time $datestringn”; When the above code is executed, it produces the following result − Local date and time Sat Feb 16 06:50:45 2013 GMT Time The function gmtime() works just like localtime() function but the returned values are localized for the standard Greenwich time zone. When called in list context, $isdst, the last value returned by gmtime, is always 0. There is no Daylight Saving Time in GMT. You should make a note on the fact that localtime() will return the current local time on the machine that runs the script and gmtime() will return the universal Greenwich Mean Time, or GMT (or UTC). Try the following example to print the current date and time but on GMT scale − Live Demo #!/usr/local/bin/perl $datestring = gmtime(); print “GMT date and time $datestringn”; When the above code is executed, it produces the following result − GMT date and time Sat Feb 16 13:50:45 2013 Format Date and Time You can use localtime() function to get a list of 9-elements and later you can use the printf() function to format date and time based on your requirements as follows − Live Demo #!/usr/local/bin/perl ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); printf(“Time Format – HH:MM:SSn”); printf(“%02d:%02d:%02d”, $hour, $min, $sec); When the above code is executed, it produces the following result − Time Format – HH:MM:SS 06:58:52 Epoch time You can use the time() function to get epoch time, i.e., the numbers of seconds that have elapsed since a given date, in Unix is January 1, 1970. Live Demo #!/usr/local/bin/perl $epoc = time(); print “Number of seconds since Jan 1, 1970 – $epocn”; When the above code is executed, it produces the following result − Number of seconds since Jan 1, 1970 – 1361022130 You can convert a given number of seconds into date and time string as follows − Live Demo #!/usr/local/bin/perl $datestring = localtime(); print “Current date and time $datestringn”; $epoc = time(); $epoc = $epoc – 24 * 60 * 60; # one day before of current date. $datestring = localtime($epoc); print “Yesterday”s date and time $datestringn”; When the above code is executed, it produces the following result − Current date and time Tue Jun 5 05:54:43 2018 Yesterday”s date and time Mon Jun 4 05:54:43 2018 POSIX Function strftime() You can use the POSIX function strftime() to format date and time with the help of the following table. Please note that the specifiers marked with an asterisk (*) are locale-dependent. Specifier Replaced by Example %a Abbreviated weekday name * Thu %A Full weekday name * Thursday %b Abbreviated month name * Aug %B Full month name * August %c Date and time representation * Thu Aug 23 14:55:02 2001 %C Year divided by 100 and truncated to integer (00-99) 20 %d Day of the month, zero-padded (01-31) 23 %D Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01 %e Day of the month, space-padded ( 1-31) 23 %F Short YYYY-MM-DD date, equivalent to %Y-%m-%d 2001-08-23 %g Week-based year, last two digits (00-99) 01 %G Week-based year 2001 %h Abbreviated month name * (same as %b) Aug %H Hour in 24h format (00-23) 14 %I Hour in 12h format (01-12) 02 %j Day of the year (001-366) 235 %m Month as a decimal number (01-12) 08 %M Minute (00-59) 55 %n New-line character (”n”) %p AM or PM designation PM %r 12-hour clock time * 02:55:02 pm %R 24-hour HH:MM time, equivalent to %H:%M 14:55 %S Second (00-61) 02 %t Horizontal-tab character (”t”) %T ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S 14:55 %u ISO 8601 weekday as number with Monday as 1 (1-7) 4 %U Week number with the first Sunday as the first day of week one (00-53) 33 %V ISO 8601 week number (00-53) 34 %w Weekday as a decimal number with Sunday as 0 (0-6) 4 %W Week number with the first Monday as the first day of week one (00-53) 34 %x Date representation * 08/23/01 %X Time representation * 14:55:02 %y Year, last two digits (00-99) 01 %Y Year 2001 %z ISO 8601 offset from UTC in timezone (1 minute = 1, 1 hour = 100) If timezone cannot be termined, no characters +100 %Z Timezone name or abbreviation * If timezone cannot be termined, no characters CDT %% A % sign % Let”s check the following example to understand the usage − Live Demo #!/usr/local/bin/perl use POSIX qw(strftime); $datestring = strftime “%a %b %e %H:%M:%S %Y”, localtime; printf(“date and time – $datestringn”); # or for GMT formatted appropriately for your

Parrot – Quick Guide

Parrot – Quick Guide ”; Previous Next What is Parrot When we feed our program into conventional Perl, it is first compiled into an internal representation, or bytecode; this bytecode is then fed into almost separate subsystem inside Perl to be interpreted. So there are two distinct phases of Perl”s operation: Compilation to bytecode and Interpretation of bytecode. This is not unique to Perl. Other languages following this design include Python, Ruby, Tcl and even Java. We also know that there is a Java Virtual Machine (JVM) which is a platform independent execution environment that converts Java bytecode into machine language and executes it. If you understand this concept then you will understand Parrot. Parrot is a virtual machine designed to efficiently compile and execute bytecode for interpreted languages. Parrot is the target for the final Perl 6 compiler, and is used as a backend for Pugs, as well as variety of other languages like Tcl, Ruby, Python etc. Parrot has been written using most popular language “C”. Parrot Installation Before we start, let”s download one latest copy of Parrot and install it on our machine. Parrot download link is available in Parrot CVS Snapshot. Download the latest version of Parrot and to install it follow the following steps: Unzip and untar the downloaded file. Make sure you already have Perl 5 installed on your machine. Now do the following: % cd parrot % perl Configure.pl Parrot Configure Copyright (C) 2001 Yet Another Society Since you”re running this script, you obviously have Perl 5 — I”ll be pulling some defaults from its configuration. … You”ll then be asked a series of questions about your local configuration; you can almost always hit return/enter for each one. Finally, you”ll be told to type – make test_prog, and Parrot will successfully build the test interpreter. Now you should run some tests; so type ”make test” and you should see a readout like the following: perl t/harness t/op/basic…..ok,1/2 skipped:label constants unimplemented in assembler t/op/string….ok, 1/4 skipped: I”m unable to write it! All tests successful, 2 subtests skipped. Files=2, Tests=6,…… By the time you read this, there could be more tests, and some of those which skipped might not skip, but make sure that none of them should fail! Once you have a parrot executable installed, you can check out the various types of examples given in Parrot ”Examples” section. Also you can check out the examples directory in the parrot repository. Parrot Instructions Format Parrot can currently accept instructions to execute in four forms. PIR (Parrot Intermediate Representation) is designed to be written by people and generated by compilers. It hides away some low-level details, such as the way parameters are passed to functions. PASM (Parrot Assembly) is a level below PIR – it is still human readable/writable and can be generated by a compiler, but the author has to take care of details such as calling conventions and register allocation. PAST (Parrot Abstract Syntax Tree) enables Parrot to accept an abstract syntax tree style input – useful for those writing compilers. All of the above forms of input are automatically converted inside Parrot to PBC (Parrot Bytecode). This is much like machine code, but understood by the Parrot interpreter. It is not intended to be human-readable or human-writable, but unlike the other forms execution can start immediately without the need for an assembly phase. Parrot bytecode is platform independent. The instruction set The Parrot instruction set includes arithmetic and logical operators, compare and branch/jump (for implementing loops, if…then constructs, etc.), finding and storing global and lexical variables, working with classes and objects, calling subroutines and methods along with their parameters, I/O, threads and more. Garbage Collection in Parrot Like Java Virtual Machine, Parrot also keep you free from worrying about memory de-allocation. Parrot provides garbage collection. Parrot programs do not need to free memory explicitly. Allocated memory will be freed when it is no longer in use i.e. no longer referenced. Parrot Garbage Collector runs periodically to take care of unwanted memory. Parrot Datatypes The Parrot CPU has four basic data types: IV An integer type; guaranteed to be wide enough to hold a pointer. NV An architecture-independent floating-point type. STRING An abstracted, encoding-independent string type. PMC A scalar. The first three types are pretty much self-explanatory; the final type – Parrot Magic Cookies, are slightly more difficult to understand. What are PMCs? PMC stands for Parrot Magic Cookie. PMCs represent any complex data structure or type, including aggregate data types (arrays, hash tables, etc.). A PMC can implement its own behavior for arithmetic, logical and string operations performed on it, allowing for language-specific behavior to be introduced. PMCs can be built in to the Parrot executable or dynamically loaded when they are needed. Parrot Registers The current Perl 5 virtual machine is a stack machine. It communicate values between operations by keeping them on a stack. Operations load values onto the stack, do whatever they need to do and put the result back onto the stack. This is easy to work with, but it is slow. To add two numbers together, you need to perform three stack pushes and two stack pops. Worse, the stack has to grow at runtime, and that means allocating memory just when you don”t want to be allocating it. So Parrot is going to break the established tradition for virtual machines, and use a register architecture, more akin to the architecture of a real hardware CPU. This has another advantage. We can use all the existing literature on how to write compilers and optimizers for register-based CPUs for our software CPU! Parrot has specialist registers for each type: 32 IV registers, 32 NV registers, 32 string registers and 32 PMC registers. In Parrot assembler, these are named I1…I32, N1…N32, S1…S32, P1…P32 respectively. Now let”s look at some assembler. We can set these registers with the set operator: set I1, 10 set N1, 3.1415 set S1, “Hello, Parrot” All Parrot ops have the same

Parrot – Examples

Parrot – Programming Examples ”; Previous Next Parrot programing is similar to assembly language programing and you get a chance to work at lower level. Here is the list of programming examples to make you aware of the various aspects of Parrot Programming. Classic Hello world! Using registers Summing squares Fibonacci Numbers Computing factorial Compiling to PBC PIR vs. PASM Classic Hello world! Create a file called hello.pir that contains the following code: .sub _main print “Hello world!n” end .end Then run it by typing: parrot hello.pir As expected, this will display the text ”Hello world!” on the console, followed by a new line (due to the n). In this above example, ”.sub _main” states that the instructions that follow make up a subroutine named ”_main”, until a ”.end” is encountered. The second line contains the print instruction. In this case, we are calling the variant of the instruction that accepts a constant string. The assembler takes care of deciding which variant of the instruction to use for us. The third line contains the ”end” instruction, which causes the interpreter to terminate. Using Registers We can modify hello.pir to first store the string Hello world!n in a register and then use that register with the print instruction. .sub _main set S1, “Hello world!n” print S1 end .end Here we have stated exactly which register to use. However, by replacing S1 with $S1 we can delegate the choice of which register to use to Parrot. It is also possible to use an = notation instead of writing the set instruction. .sub _main $S0 = “Hello world!n” print $S0 end .end To make PIR even more readable, named registers can be used. These are later mapped to real numbered registers. .sub _main .local string hello hello = “Hello world!n” print hello end .end The ”.local” directive indicates that the named register is only needed inside the current compilation unit (that is, between .sub and .end). Following ”.local” is a type. This can be int (for I registers), float (for N registers), string (for S registers), pmc (for P registers) or the name of a PMC type. Summing squares This example introduces some more instructions and PIR syntax. Lines starting with a # are comments. .sub _main # State the number of squares to sum. .local int maxnum maxnum = 10 # Some named registers we”ll use. # Note how we can declare many # registers of the same type on one line. .local int i, total, temp total = 0 # Loop to do the sum. i = 1 loop: temp = i * i total += temp inc i if i <= maxnum goto loop # Output result. print “The sum of the first ” print maxnum print ” squares is ” print total print “.n” end .end PIR provides a bit of syntactic sugar that makes it look more high level than assembly. For example: temp = i * i Is just another way of writing the more assembly-ish: mul temp, i, i And: if i <= maxnum goto loop Is the same as: le i, maxnum, loop And: total += temp Is the same as: add total, temp As a rule, whenever a Parrot instruction modifies the contents of a register, that will be the first register when writing the instruction in assembly form. As is usual in assembly languages, loops and selections are implemented in terms of conditional branch statements and labels, as shown above. Assembly programming is one place where using goto is not a bad form! Fibonacci Numbers The Fibonacci series is defined like this: take two numbers, 1 and 1. Then repeatedly add together the last two numbers in the series to make the next one: 1, 1, 2, 3, 5, 8, 13, and so on. The Fibonacci number fib(n) is the n”th number in the series. Here”s a simple Parrot assembler program that finds the first 20 Fibonacci numbers: # Some simple code to print some Fibonacci numbers print “The first 20 fibonacci numbers are:n” set I1, 0 set I2, 20 set I3, 1 set I4, 1 REDO: eq I1, I2, DONE, NEXT NEXT: set I5, I4 add I4, I3, I4 set I3, I5 print I3 print “n” inc I1 branch REDO DONE: end This is the equivalent code in Perl: print “The first 20 fibonacci numbers are:n”; my $i = 0; my $target = 20; my $a = 1; my $b = 1; until ($i == $target) { my $num = $b; $b += $a; $a = $num; print $a,”n”; $i++; } NOTE: As a fine point of interest, one of the shortest and certainly the most beautiful ways of printing out a Fibonacci series in Perl is perl -le ”$b=1; print $a+=$b while print $b+=$a”. Recursively computing factorial In this example we define a factorial function and recursively call it to compute factorial. .sub _fact # Get input parameter. .param int n # return (n > 1 ? n * _fact(n – 1) : 1) .local int result if n > 1 goto recurse result = 1 goto return recurse: $I0 = n – 1 result = _fact($I0) result *= n return: .return (result) .end .sub _main :main .local int f, i # We”ll do factorial 0 to 10. i = 0 loop: f = _fact(i) print “Factorial of ” print i print ” is ” print f print “.n” inc i if i <= 10 goto loop # That”s it. end .end Let”s look at the _fact sub first. A point that was glossed over earlier is why the names of subroutines, all start with an underscore! This is done simply as a way of showing that the label is global rather than scoped to a particular subroutine. This is significant as the label is then visible to other subroutines. The first line, .param int n, specifies that this subroutine takes one integer parameter and that we”d like to refer to the register it was passed in

Parrot – Datatypes

Parrot – Datatypes ”; Previous Next The Parrot CPU has four basic data types: IV An integer type; guaranteed to be wide enough to hold a pointer. NV An architecture-independent floating-point type. STRING An abstracted, encoding-independent string type. PMC A scalar. The first three types are pretty much self-explanatory; the final type – Parrot Magic Cookies, are slightly more difficult to understand. What are PMCs? PMC stands for Parrot Magic Cookie. PMCs represent any complex data structure or type, including aggregate data types (arrays, hash tables, etc.). A PMC can implement its own behavior for arithmetic, logical and string operations performed on it, allowing for language-specific behavior to be introduced. PMCs can be built in to the Parrot executable or dynamically loaded when they are needed. Print Page Previous Next Advertisements ”;

Parrot – Branches

Parrot – Branches ”; Previous Next Code gets a little boring without flow control; for starters, Parrot knows about branching and labels. The branch op is equivalent to Perl”s goto: branch TERRY JOHN: print “fjordsn” branch END MICHAEL: print ” pining” branch GRAHAM TERRY: print “It”s” branch MICHAEL GRAHAM: print ” for the ” branch JOHN END: end It can also perform simple tests to see whether a register contains a true value: set I1, 12 set I2, 5 mod I3, I2, I2 if I3, REMAIND, DIVISOR REMAIND: print “5 divides 12 with remainder ” print I3 branch DONE DIVISOR: print “5 is an integer divisor of 12” DONE: print “n” end Here”s what that would look like in Perl, for comparison: $i1 = 12; $i2 = 5; $i3 = $i1 % $i2; if ($i3) { print “5 divides 12 with remainder “; print $i3; } else { print “5 is an integer divisor of 12”; } print “n”; exit; Parrot Operator We have the full range of numeric comparators: eq, ne, lt, gt, le and ge. Note that you can”t use these operators on arguments of disparate types; you may even need to add the suffix _i or _n to the op, to tell it what type of argument you are using, although the assembler ought to divine this for you, by the time you read this. Print Page Previous Next Advertisements ”;