"Sputnik" help  
Sputnik Help
Language Reference - Classes - Class Magic Functions - Count

Class Magic Functions - Count

Set the return value whenever a class is asked to provide its count such as with the count() function or anything else.

// define a class
class test
{
    my $x;
    my $y;
    Function __Construct($a, $b)
    {
        $x = $a;
        $y = $b;
    }
    Function __Count()
    {
        return $x + $y;
    }
}

// create new instance
my $a = new test(10, 20);

// ask the class to return its count
say Count($a);

// PRINTS
// 30

Although it seems like this is only useful to control what the count() function returns in fact it is used internally by Sputnik in many many ways that go far beyond that one function. For example Sputnik might want the count of a class to decide if it should be placed higher in a list than another class or many other little details think of it as a Length property or something.

You can also make use the count for stuff like huristics etc.

You can also use the ^^ operator instead of the count() function like so

// define a class
class test
{
    my $x;
    my $y;
    Function __Construct($a, $b)
    {
        $x = $a;
        $y = $b;
    }
    Function __Count()
    {
        return $x + $y;
    }
}

// create new instance
my $a = new test(10, 20);

// ask the class to return its count
say ^^$a;

// PRINTS
// 30

A full list of all possible magic functions can be found here.


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org