"Sputnik" help  
Sputnik Help
Language Reference - Variables - References

References

In Sputnik, references provide a way to create aliases or pointers to variables. This allows you to access and manipulate variables indirectly, which can be useful in various scenarios, such as passing variables to functions by reference or creating complex data structures.

Creating a Reference

To create a reference to a variable in Sputnik, you use the & operator followed by the variable name. For example:

my $a = "Hello";
my $b = &$a;
say $a;
say $b;

// PRINTS
// Hello
// {Ref;Depth=1}

Accessing a Reference

To access the value of a variable through a reference in Sputnik, you use the * operator followed by the reference variable name. For example:

my $a = "Hello";
my $b = &$a;
say $a;
say $b;
say *$b; // Accessing

// PRINTS
// Hello
// {Ref;Depth=1}
// Hello

This will print the value of the variable referenced by $b.

Function with Pointer Passed Manually

In this example, the function Foo accepts a pointer to a variable as its argument. This is achieved by passing the variable $p as a reference using the & operator when calling the function. Inside the function, the reference needs to be dereferenced explicitly using the * operator to access and modify the value of the original variable.

Function Foo($var)
{
    *$var .= "Bar";
}

my $p = "Hello";
Foo(&$p);
say $p;

// PRINTS
// HelloBar

In this case, the function Foo modifies the value of $p directly through the reference.

Function Automatically Dereferences Argument

In this example, the function Foo automatically dereferences its argument when it is passed. The function signature explicitly specifies the argument will be dereferenced using the *, indicating that the function expects a reference to a variable. Inside the function, the argument is already dereferenced, allowing direct manipulation of the original variable.

Function Foo("*" $var)
{
    $var .= "Bar";
}

my $p = "Hello";
Foo(&$p);
say $p;

// PRINTS
// HelloBar

In this scenario, the function Foo modifies the value of $p directly without needing explicit dereferencing.

Function Handles Reference Automatically

In this example, the function Foo automatically handles the request for a reference to a variable and dereferences it internally. The function signature specifies the argument as a reference by using *&, indicating that the function expects a reference to a variable. When calling the function, there's no need to explicitly pass the variable as a reference. Inside the function, the argument is automatically dereferenced, allowing direct manipulation of the original variable.

Function Foo("*&" $var)
{
    $var .= "Bar";
}

my $p = "Hello";
Foo($p);
say $p;

// PRINTS
// HelloBar

In this case, the function Foo handles both the request for a reference to the variable and the dereferencing internally, simplifying the usage of the function for the caller.

Putting it all together and also making an array of references

In this example, we first create two variables $cat and $dog, each containing strings "meow" and "woof" respectively. Then, we create an array $arr containing references to these variables $cat and $dog. By modifying these references inside the function Foo, which takes the array as an argument, we effectively modify the original variables $cat and $dog. This is possible because Sputnik passes arrays by reference (internal data structure is moved around and not cloned), allowing modifications made to the array elements within the function to affect the original variables outside the function. As a result, when we print the values of $cat and $dog after calling Foo, we see that they have been updated according to the modifications made inside the function.

my $cat = "meow";
my $dog = "woof";

my $arr = [&$cat, &$dog];
*$arr[0] .= "Doggy";
*$arr[1] .= "Catty";

say $cat;
say $dog;

Foo($arr);

say $cat;
say $dog;

Function Foo($a)
{
    *$a[0] .= "777";
    *$a[1] .= "555";
}

// PRINTS
// meowDoggy
// woofCatty
// meowDoggy777
// woofCatty555

In this context, an array of references proves to be a powerful tool, enabling manipulation of multiple variables through a single data structure. By storing references to variables within an array, developers gain the ability to conveniently pass and modify multiple variables within functions, facilitating more efficient and flexible programming practices.

Understanding References

References are important in Sputnik because they allow you to manipulate variables indirectly and create more flexible and dynamic code. They are commonly used in situations where you need to pass variables to functions by reference, return multiple values from a function, or create complex data structures.

When you modify a variable through a reference, you are actually modifying the original variable. This can lead to unexpected behavior if you're not careful, but it also provides a powerful way to share and manipulate data across different parts of your code.

In summary, references in Sputnik provide a way to create aliases or pointers to variables, allowing you to access and manipulate variables indirectly. They are an important feature of the language that enables more flexible and dynamic programming techniques.


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