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

Declaring Variables

Variables are declared and created with the My and Global keywords.

This is how you define a local (my) variable:

// define a new local variable
// "my" causes the variable to be local to the
// current scope in this case the main script
my $var1; // contains UNDEF (not defined)
my $var2 = "Hello"; // contains the string "Hello" (without quotes)
my $var3 = 1337; // contains the integer 1337

This is how you define a global variable:

// define a new global variable
// "global" causes the variable to be global so
// that all functions etc in Sputnik can access it
global $var1; // contains UNDEF (not defined)
global $var2 = "Hello"; // contains the string "Hello" (without quotes)
global $var3 = 1337; // contains the integer 1337

If you have local and global variables with the same name here is how to access them:

global $cat = "meowzor";
global $dog = "woofzor";
my $cat = "meow";
my $dog = "woof";

say $cat;
say $dog;

say global $cat;
say global $dog;

say my $cat;
say my $dog;

// PRINTS
// meow
// woof
// meowzor
// woofzor
// meow
// woof

Using this trick you can still access a global variable by the same name as a current local variable.

Here is a larger example of this:

global $cat = "meowzor"; // global variable
global $dog = "woofzor"; // global variable

function MyFunction()
{
    my $cat = "meow"; // local variable
    my $dog = "woof"; // local variable
    
    say $cat; // "meow" (local variable)
    say $dog; // "woof" (local variable)
    
    say global $cat; // "meowzor" (global variable)
    say global $dog; // "woofzor" (global variable)
    
    // You can continue using the local variables here
    say $cat; // "meow" (local variable)
    say $dog; // "woof" (local variable)
}
MyFunction();

// PRINTS
// meow
// woof
// meowzor
// woofzor
// meow
// woof

Or you can declare multiple variables at once per line to same value:

my $a = ($b = ($c = "Cat"));

say $a;
say $b;
say $c;

// PRINTS
// Cat
// Cat
// Cat

Or you can declare multiple variables at once per line to any value(s):

// define undef variables
my list ($var1, $var2, $var3);
// define variables with content
my list ($var1 = 100, $var2 = "Hello", $var3 = "Cat");
// print it
say $var1 . $var2 . $var3;
// PRINTS
// 100HelloCat

We used a List() to define them to learn more about List() click on it.


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