Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) 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.

#!/usr/bin/perl use warnings; use strict;

theDamian calls this indirect object syntax. what's the direct syntax then?

my $life = new life : 20;

theDamian points out this is a more idiomatic way of doing an infinite loop.

loop { $life.display() }

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.

class life {

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.

has Int $.count; has Int $.dimension; has Array of Bit @.grid;

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.

method BUILD(Int $dimension) { $.count = 0; $.max = $dimension - 1;

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.

my Array of Bit @.grid is dim($dimension,$dimension) is default(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.

@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;

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.

@.grid = @grid; }

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

sub iterate (&block) {

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.

for 0..$.max -> $x { for 0..$.max -> $y { block($x,$y); } } }

We can declare methods private to our class. In perl5 you could always get to the internals of an object.

method calculate() is private { my @newgrid; iterate {

$^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.

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";

We can interpolate any arbitrary code or variable into a string with $( ... )

print "Turn $(++$.count), press enter to continue or ctl-c to quit +;

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.

.calculate(); } }


In reply to Re: Prolegemona To A Future Scripting Language: Game Of Life In Perl 6 by jaldhar
in thread Prolegemona To A Future Scripting Language: Game Of Life In Perl 6 by jaldhar

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2021-06-15 22:43 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (74 votes). Check out past polls.