note jaldhar <p>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.</p> <code> #!/usr/bin/perl use warnings; use strict; </code> <p>[theDamian] calls this indirect object syntax. what's the direct syntax then?</p> <code> my $life = new life : 20; </code> <p>[theDamian] points out this is a more idiomatic way of doing an infinite loop.</p> <code> loop { $life.display() } </code> <p>In perl 6, a class isn't just a <i>package</i> but a new type of block scope indicated by the keyword <i>class</i>. It can have methods indicated by <i>method</i> and attributes indicated by <i>has</i>. </p> <code> class life { </code> <p>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.</p> <code> has Int $.count; has Int $.dimension; has Array of Bit @.grid; </code> <p>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 <i>CREATE</i> bu [theDamian] says it ought to be called <i>BUILD</i>.</p> <p>You will note functions can have proper prototypes now.</p> <code> method BUILD(Int $dimension) { $.count = 0; $.max = $dimension - 1; </code> <p>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.</p> <code> my Array of Bit @.grid is dim($dimension,$dimension) is default(0); </code> <p>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.</p> <code> @grid[$dimension / 2 - 1][$dimension / 2] = 1; @grid[$dimension / 2 - 1][$dimension / 2 + 1] = 1; @grid[$dimension / 2][$dimension / 2] = 1; @grid[$dimension / 2][$dimension / 2 - 1] = 1; @grid[$dimension / 2 + 1][$dimension / 2] = 1; </code> <p>Because the grid is a multidimensional array it cannot be directly declared as an attribute. That's why we made a local variable called <i>@grid</i>. Now that we know its' size we can assign it to the attibute.</p> <code> @.grid = @grid; } </code> <p>[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 :-) </p> <code> sub iterate (&block) { </code> <p>This is what <i>foreach</i> loops will look like. No need to put a <i>my</i> on the <i>$x</i> and <i>$y</i>, it's implied.</p> <p>Also note parentheses are optional on <i>for</i>s, <i>if</i>s etc.</p> <code> for 0..$.max -> $x { for 0..$.max -> $y { block($x,$y); } } } </code> <p>We can declare methods private to our class. In perl5 you could always get to the internals of an object.</p> <code> method calculate() is private { my @newgrid; iterate { </code> <p><i>$^x</i> and <i>$^y</i> 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.</p> <code> my $live = sum(@.grid[$^x-1..$^x+1][$^y-1..$^y+1]); @newgrid[$^x][$^y] = $live==2 && @.grid[$^x][$^y] || $live==3; } @.grid = @newgrid; } method display { iterate { print $.grid[$^x][$^y] ?? '+' :: '.'; print "\n" if $^x == $.max; } print "\n"; </code> <p>We can interpolate any arbitrary code or variable into a string with $( ... )</p> <code> print "Turn $(++$.count), press enter to continue or ctl-c to quit; </code> <p>File handles are now scalars and STDIN is called IN.</p> <code> <$*IN>; </code> <p>[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.</p> <code> .calculate(); } } </code> <p>-- <br>&#2716;&#2738;&#2727;&#2736;</p> 253366 253366