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

Class Magic Functions - GetFromEnd

This allows a class to override all features that try get a value such as ["key"] for example:

// define a class
class test
{
    my $arr;
    Function __Construct()
    {
        $arr = array();
    }
    Function __Get($index)
    {
        return $arr[$index];
    }
    Function __GetFromEnd($index)
    {
        return $arr[count($arr) - $index];
    }
    Function __Set($index, $value)
    {
        $arr[$index] = $value;
    }
    Function __SetFromEnd($index, $value)
    {
        $arr[count($arr) - $index] = $value;
    }
}

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

// use the __set function to set some values
$a[0] = "Cat";
$a[1] = "Dog";
$a[2] = "Fox";

printr $a;

// now use the __get function to return them values
say $a[1];
// now use the __getfromend function to return them values
say $a[^1];

// PRINTS
// Dog
// Fox

See also Operator [^] (Index From End).

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


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