"Sputnik" help  
Sputnik Help
Language Reference - Variables - Code Blocks

Code Blocks

The Code Blocks in Sputnik are very powerful since it allows you to store code on a variable and execute that code later.

Here is the most basic example:

// create a code block using {{ }}
Hello({{ say "Hello!"; }});
Function Hello($a)
{
    $a(); // execute the code block
}
// PRINTS
// Hello!

As you can see they are created with the {{ }} syntax unlike normal blocks using the { } syntax this is because it's telling Sputnik that you want to save this block for later and not execute it immediately.

They can span multiple lines and even return values here is the example of returning a value:

// create a code block using {{ }}
Hello({{ say "Hello!"; return 777; }});
Function Hello($a)
{
    my $a = $a(); // execute the code block
    say "Value $a";
}
// PRINTS
// Hello!
// Value 777

Here is a multi-line example:

// create a code block using {{ }}
Hello({{
        if ($cat == "meow")
        {
            say "Hello Meow!";
            return 1337;
        }
        else
        {
            say "Hello!";
            return 777;
        }
    }});
Function Hello($a)
{
    my $cat = "meow";
    my $a = $a(); // execute the code block
    say "Value $a";
}
// PRINTS
// Hello Meow!
// Value 1337

As you can see the {{ }} code block allows pretty much the same content as a function { } block.

You can also use CodeExecute flags to the () when calling Code Blocks like this:

// create a code block using {{ }}
Hello({{
        if ($cat == "meow")
        {
            say "Hello Meow!";
            return 1337;
        }
        else
        {
            say "Hello!";
            return 777;
        }
    }});
Function Hello($a)
{
    my $cat = "meow";
    my $a = $a([@CeCreateNewStack]); // execute the code block
    say "Value $a";
}
// PRINTS
// Hello!
// Value 777

Its important to remember the FIRST argument of a Code Block is reserved for the Code Block itself such as flags and what not you can pass your own arguments AFTER the FIRST argument that example will come shortly.

Notice the Code Block no longer sees the local variable $cat ? This is because we told it to create a new stack so it's basically the same as calling a function at that location now. There are many flags to use.

Here is how to pass your own arguments of course the first argument we will ignore for now so just null it:

// create a code block using {{ }}
Hello({{
        say "\$0 is $0";
        say "\$1 is $1";
        say "\$2 is $2";
        if ($cat == "meow")
        {
            say "Hello Meow!";
            return 1337;
        }
        else
        {
            say "Hello!";
            return 777;
        }
    }});
Function Hello($a)
{
    my $cat = "meow";
    my $a = $a(null, "Cat", "Dog", 777); // execute the code block
    say "Value $a";
}
// PRINTS
// $0 is Cat
// $1 is Dog
// $2 is 777
// Hello Meow!
// Value 1337

Example of using your own arguments but also passing the flags etc:

// create a code block using {{ }}
Hello({{
        say "\$0 is $0";
        say "\$1 is $1";
        say "\$2 is $2";
        if ($cat == "meow")
        {
            say "Hello Meow!";
            return 1337;
        }
        else
        {
            say "Hello!";
            return 777;
        }
    }});
Function Hello($a)
{
    my $cat = "meow";
    my $a = $a([@CeCreateNewStack], "Cat", "Dog", 777); // execute the code block
    say "Value $a";
}
// PRINTS
// $0 is Cat
// $1 is Dog
// $2 is 777
// Hello!
// Value 777

If you do not wish to store code and simply wish to execute the code block instant you can do this:

say {{ my $a = 10; $a++; return $a; }}();
// PRINTS
// 11

This creates the code block and immediately executes it however we can create a different type of code block which instantly executes itself without having it to prompt it using () like so:

say @{{ my $a = 10; $a++; return $a; }};
// PRINTS
// 11

This special code block begins with a @ symbol which means to instantly execute and return the value this is useful when you wish to execute code inside a function call like this:

say Min(@{{ my $a = 10; $a++; return $a; }}, 20);
say Max(@{{ my $a = 10; $a++; return $a; }}, 20);
// PRINTS
// 11
// 20

 


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