"Sputnik" help  
Sputnik Help
Language Reference - Datatypes - Undef

Undef

Undef in Sputnik is a special value that signifies the absence of a value. Unlike null, which is a valid value, undef indicates that a variable is undefined and does not have any assigned value.

Variables start out as undef if you create them with no assignment for example:

my $cat;
my list($dog, $fox);

printrd $cat;
printrd $dog;
printrd $fox;

// PRINTS
// UNDEF
// UNDEF
// UNDEF

Unsetting a variable (destroying it) with the unset function will make it appear as undef when accessed again after:

my $cat = "meow";

unset($cat);

printrd $cat;

// PRINTS
// UNDEF

To check if a variable is defined using the defined function like so:

my $a = undef;
if (defined($a))
    say "It is defined";
else
    say "It is not defined";
    
my $b = 77;
if (defined($b))
    say "It is defined";
else
    say "It is not defined";
    
// PRINTS
// It is not defined
// It is defined

You can specifically check if a variable is set to undef like so:

my $a = undef;
if (IsUndef($a))
    say "It is undef";
else
    say "It is not undef";
// PRINTS
// It is undef

You can explicitly set a variable to undef using the undefine function or simply setting the variable to undef like so:

my $a = 77;
undefine($a);

my $b = 77;
$b = undef;

say defined($a) ? "Defined" : "Not Defined";
say defined($b) ? "Defined" : "Not Defined";
    
// PRINTS
// Not Defined
// Not Defined

The primary purpose of undef is to provide a distinct and unambiguous indicator that something went "wrong" in a function. While other values like numbers, letters, strings, arrays, and null may be valid returns from a function, undef is unique in that it always signifies an undefined state, ensuring clarity when handling function results.

Here is how such a function can be made:

Function Cat($v)
{
    if ($v == "cat")
        return undef;
    return $v;
}

Cat(1);
Cat(10);
Cat(null);

if (!defined(Cat("cat")))
    say "Function failed";
else
    say "Function was fine";
    
// PRINTS
// Function failed

In the provided code snippet, the Cat function serves as a perfect example of the utility of undef in distinguishing a meaningful error state. The function checks if the input parameter $v is equal to the string "cat" and, in such a case, deliberately returns undef. Otherwise, it returns the original value of $v.

The interesting aspect here is that the function is designed to accept a diverse range of input values, including numbers (Cat(1) and Cat(10)) and null (Cat(null)). Typically, all these values could be considered valid returns from the function, making it challenging to differentiate between intended results and potential errors solely based on the returned value.

However, by returning undef when the condition $v == "cat" is met, the function provides a clear and unambiguous indicator that something went "wrong" or diverged from the expected behavior. The subsequent check using !defined(Cat("cat")) effectively catches this specific error condition, allowing the distinction between a successful execution and a function failure. This illustrates how using undef as a distinctive error signal enhances the robustness of the code by offering a reliable means of identifying exceptional cases, even in scenarios where other values might be valid returns.

Same as the function above but using === instead:

Function Cat($v)
{
    if ($v == "cat")
        return undef;
    return $v;
}

Cat(1);
Cat(10);
Cat(null);

if (Cat("cat") === undef)
    say "Function failed";
else
    say "Function was fine";
    
// PRINTS
// Function failed

The undef feature in a language, even when nulls are present, adds a valuable dimension to error handling and result signaling. It provides a clear and intentional way for functions to communicate that something unexpected or erroneous has occurred. The explicit use of undef helps distinguish error states from other valid return values, contributing to code clarity and making it easier for developers to handle exceptional cases. This approach enhances the reliability and maintainability of the codebase by offering a standardized method for signaling and responding to unexpected conditions.


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