|No such thing as a small change|
Re: Prolegemona To A Future Scripting Language: Game Of Life In Perl 6by jaldhar (Vicar)
|on May 01, 2003 at 05:43 UTC||Need Help??|
Thanks to all that replied. This was very enlightening for me and I hope it was for others too. Here is the script again with suggestions and corrections various monks made and some comments.
theDamian calls this indirect object syntax. what's the direct syntax then?
theDamian points out this is a more idiomatic way of doing an infinite loop.
In perl 6, a class isn't just a package but a new type of block scope indicated by the keyword class. It can have methods indicated by method and attributes indicated by has.
Perl 6 will still have the regular data types but you will have more say over what range of values they can hold or how they should be represented at the machine level. For instance my grid only holds 0 and 1 values. I originally had Int but several monks point out a Bit is potentially more space-efficient.
There's no need to create a constructor now it is automatically done for you. Instead you should do initialization in a special method. I was led by the Linux Magazine article to believe it should be called CREATE bu theDamian says it ought to be called BUILD.
You will note functions can have proper prototypes now.
Here we are creating an array of bits. We are saying that it is two-d, size $dimension x $dimension, and the default value of each element is 0.
For those who are not up on their game of life theory, what's happening here is an initial grouping of on cells is being created in the center of the grid. The shape is called an R-pentomino and it becomes static after 100 turns.
Because the grid is a multidimensional array it cannot be directly declared as an attribute. That's why we made a local variable called @grid. Now that we know its' size we can assign it to the attibute.
theDamian suggested adding this routine as a utility. Why is it declared as sub and not method? I'll take a guess and say so it can be inherited by subclasses but not used outside the clase sort of like c++ protected functions. Or it's a typo :-)
This is what foreach loops will look like. No need to put a my on the $x and $y, it's implied.
Also note parentheses are optional on fors, ifs etc.
We can declare methods private to our class. In perl5 you could always get to the internals of an object.
$^x and $^y are placeholders. They are an implicit way of saying my($x, $y) = @_; You should use them as you need them and they are assigned alphabetically (in UTF-8) to the parameters of your function.
We can interpolate any arbitrary code or variable into a string with $( ... )
File handles are now scalars and STDIN is called IN.
jkahn wondered why I have calculate here and not in the main loop. That's how I had it originally and how I would do it in a real program but I confess I broke one of my own rules for this exercise and made calculate a private method just to show off that feature.