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

Class Magic Functions - IsTrue

This allows a class to override all true/false tests within Sputnik when used on a class.for example:

// define a class
class test
{
    my $x;
    my $y;
    Function __Construct($a, $b)
    {
        $x = $a;
        $y = $b;
    }
    Function __IsTrue()
    {
        return $x > 0 && $y > 0;
    }
}

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

// find out the classes values
say "First: " . IsTrue($a);

// negate the class variables
$a->$x = -$a->$x;
$a->$y = -$a->$y;

// find out the classes values
say "Second: " . IsTrue($a);

// PRINTS
// First: true
// Second: false

Reverse example:

// define a class
class test
{
    my $x;
    my $y;
    Function __Construct($a, $b)
    {
        $x = $a;
        $y = $b;
    }
    Function __IsTrue()
    {
        return $x > 0 && $y > 0;
    }
}

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

// find out the classes values
say "First: " . IsFalse($a);

// negate the class variables
$a->$x = -$a->$x;
$a->$y = -$a->$y;

// find out the classes values
say "Second: " . IsFalse($a);

// PRINTS
// First: false
// Second: true

Sputnik tests variables for true/false states in many internal functions and deep within its core so the ability to control if your class will return true or false is very important. This can be used to control all kinds of stuff such as if your class will trigger an if statement or not for example:

// define a class
class test
{
    my $x;
    my $y;
    Function __Construct($a, $b)
    {
        $x = $a;
        $y = $b;
    }
    Function __IsTrue()
    {
        return $x > 0 && $y > 0;
    }
}

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

// first test
if ($a)
{
    say "First True!";
}
else
{
    say "First False!";
}

// negate the class variables
$a->$x = -$a->$x;
$a->$y = -$a->$y;

// second test
if ($a)
{
    say "Second True!";
}
else
{
    say "Second False!";
}

// PRINTS
// First True!
// Second False!

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


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