Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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.

<$*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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2014-08-29 17:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (283 votes), past polls