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

Class Magic Functions - Invoke

This causes a class variable to accept being called like any other function for example:

class Cat
{
    my $name;
    Function __Construct($n)
    {
        $name = $n;
    }
    Function __Invoke($num)
    {
        say "$name meows $num time(s)";
    }
}

my $felix = new Cat("Felix");
my $fluffy = new Cat("Fluffy");
$felix(3);
$fluffy(5);

// PRINTS
// Felix meows 3 time(s)
// Fluffy meows 5 time(s)

Now the class variable can be used like a function and of course its possible to do infinite arguments and anything else you want for example:

class Cat
{
    my $name;
    Function __Construct($n)
    {
        $name = $n;
    }
    Function __Invoke("..." $values)
    {
        my $count = (count)$values;
        my $sum = 0;
        foreach(my $var in $values)
            $sum += $var;
        return $sum;
    }
}

my $felix = new Cat("Felix");
my $fluffy = new Cat("Fluffy");
say "Felix: " . $felix(3, 7, 1, 2);
say "Fluffy: " . $fluffy(5, 10, 20);

// PRINTS
// Felix: 13
// Fluffy: 35

This is similar to User Variable Functions.

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


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