"Sputnik" help  
Sputnik Help
scope( <args> )
{
statements
...
}

Description

Does a variety of things all related to variable scopes in an easy to use way.

Parameters

args Way too much stuff to explain here just see the examples below.

Remarks

Scope statements may be nested.

The scope statement in Sputnik is a versatile tool that simplifies working with variable scopes. It provides a convenient way to manage variable access and control within different scopes, making your code more organized and easier to maintain.

By using the scope statement, you can create nested scopes and define which variables are accessible within each scope. You have the flexibility to specify whether variables should be copied or shifted from the previous scope, allowing you to control their behavior and ensure data integrity.

Additionally, you can use various argument formats such as individual variables, strings, arrays, or even variable references enclosed in parentheses to define the variables that should be included in the scope. This enables fine-grained control over the scope's contents and allows you to tailor it to your specific needs.

The scope statement is particularly useful when you want to isolate and manipulate specific variables without affecting the outer scopes. It provides a clean and intuitive way to modify variables within a scope while preserving the values in the outer scopes.

Take advantage of the scope statement to enhance your code's readability, maintainability, and encapsulation. By effectively managing variable scopes, you can write cleaner, more modular code that is easier to understand and maintain over time.

Related

Scopes.

Example

First go see the examples found in Scopes then once you understand that come back here for further examples.

Now that you have seen the most basic examples such as:

$a = 10;
// create new scope that can access variables outside of it
Scope()
{
    $a = 20;
    say "First: $a";
}
say "Second: $a";
// PRINTS
// First: 20
// Second: 10

This uses a blank Scope() with no arguments that causes it to use the variables from the previous scopes inside the new one so if you += on a variable it will show the change for example:

$a = 10;
// create new scope that can access variables outside of it
Scope()
{
    $a += 20;
    say "First: $a";
}
say "Second: $a";
// PRINTS
// First: 30
// Second: 30

See how it modified outside the scope? Of course = by itself would stay in the scope but += will modify out of it.

Now lets try it with the true argument this will cause it to copy the previous scope instead of moving it in directly for example:

$a = 10;
// create new scope that can access variables outside of it
Scope(true)
{
    $a += 20;
    say "First: $a";
}
say "Second: $a";
// PRINTS
// First: 30
// Second: 10

As you may have guessed the default argument is false which means shift the previous scope in.

At this point we can choose to only allow specific variables to enter the scope for example:

$a = 10;
$b = 50;
// create new scope that can access variables outside of it
Scope($a)
{
    $a += 20;
    say "B: $b";
    say "First: $a";
}
say "Second: $a";
// PRINTS
// B: 
// First: 30
// Second: 30

Here we allowed $a to enter but did not allow $b to enter now lets allow $b and $a like so:

$a = 10;
$b = 50;
// create new scope that can access variables outside of it
Scope($a, $b)
{
    $a += 20;
    say "B: $b";
    say "First: $a";
}
say "Second: $a";
// PRINTS
// B: 50
// First: 30
// Second: 30

Remember the true/false from earlier? We can use that before each variable allow so we can decide which variables will be shifted and will be copied so lets make $a a copy and $b a shift:

$a = 10;
$b = 50;
// create new scope that can access variables outside of it
Scope(true, $a, false, $b)
{
    $a += 20;
    say "B: $b";
    say "First: $a";
}
say "Second: $a";
// PRINTS
// B: 50
// First: 30
// Second: 10

As you saw above $a was copied and $b was not of course if we add more variables now they will be set to shifted automatically since it uses the last found true/false.

We can also define variables to keep using "" quotations and seperate them using , for example:

$a = 10;
$b = 50;
// create new scope that can access variables outside of it
Scope("a,b")
{
    $a += 20;
    say "B: $b";
    say "First: $a";
}
say "Second: $a";
// PRINTS
// B: 50
// First: 30
// Second: 30
That defined them using a string however we can still use the true/false before the string if we want to so lets make everything a copy:
$a = 10;
$b = 50;
// create new scope that can access variables outside of it
Scope(true, "a,b")
{
    $a += 20;
    say "B: $b";
    say "First: $a";
}
say "Second: $a";
// PRINTS
// B: 50
// First: 30
// Second: 10

We can use the + and - before the variables name in the string to force a copy/shift here we force $a to be copied:

$a = 10;
$b = 50;
// create new scope that can access variables outside of it
Scope(false, "+a,b")
{
    $a += 20;
    say "B: $b";
    say "First: $a";
}
say "Second: $a";
// PRINTS
// B: 50
// First: 30
// Second: 10

And here we force $a to be shifted even when copying is turned on before it:

$a = 10;
$b = 50;
// create new scope that can access variables outside of it
Scope(true, "-a,b")
{
    $a += 20;
    say "B: $b";
    say "First: $a";
}
say "Second: $a";
// PRINTS
// B: 50
// First: 30
// Second: 30

We can also define what variables to place in the scope by using another variable but encasing it inside ( ) braces for example:

$a = 10;
$b = 50;
$moo = "a";
$kitten = "b";
// create new scope that can access variables outside of it
Scope(($moo), ($kitten))
{
    $a += 20;
    say "B: $b";
    say "First: $a";
}
say "Second: $a";
// PRINTS
// B: 50
// First: 30
// Second: 30

We can also define what variables to keep using an array:

$a = 10;
$b = 50;
$arr = ["a", "b"];
// create new scope that can access variables outside of it
Scope(($arr))
{
    $a += 20;
    say "B: $b";
    say "First: $a";
}
say "Second: $a";
// PRINTS
// B: 50
// First: 30
// Second: 30

 


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