SPVM::D...tements(3)
okay so this is the manual page for SPVM::D...tements(3), aka User Contributed Perl Documentation, and the actual doc name is SPVM::Document::Language::Statements β we're learning all about statements in the SPVM language. Bestie, grab a snack, because this is the whole statement saga. π
β¨
β¨ Name β¨
SPVM::Document::Language::Statements - Statements in the SPVM Language
π¦ Description π¦
This document describes statements in the SPVM language. That's it, that's the vibe. π«Ά
π Statements π
A statement is a basic instruction that tells the program what to do. No cap. Statements can be written direct under scope block.
# Scope block
{
# Statements
STATEMENT1
STATEMENT2
STATEMENT3
}
π Conditional Statements π
πΈ if Statement
The if statement is a conditional statement with the following syntax.
if (CONDITION1) {
}
elsif (CONDITION2) {
}
elsif (CONDITIONn) {
}
else {
}
The elsif statement and the else statement are optional.
At first, all elsif statements are expanded to the following code using if - else statements.
if (CONDITION1) {
}
else {
if (CONDITION2) {
}
else {
if (CONDITIONn) {
}
else {
}
}
}
The if statement is converted to simple if - else statements, so see a simple if - else statement.
if (CONDITION) {
}
else {
}
The condition evaluation is performed on the condition CONDITION.
- If the evaluated value is not
0, the program jumps to the beginning of theifblock. - If the evaluated value is
0and there is theelseblock, the program jumps to the beginning of theelseblock. - If the evaluated value is
0and there is noelseblock, the program jumps to the end of theifblock.
A if - else statement is enclosed by an invisible simple block.
{
if (CONDITION) {
}
else {
}
}
Examples:
# if statement.
my $flag = 1;
if ($flag == 1) {
say "One";
}
# if statement with elsif and else
my $flag = 2;
if ($flag == 1) {
say "One";
}
elsif ($flag == 2) {
say "Two";
}
elsif ($flag == 3) {
say "Three";
}
else {
say "Other";
}
π else Statement
The else statement is a conditional statement used in if statement.
if (CONDITION) {
}
else {
}
π elsif Statement
The elsif statement is a conditional statement used in if statement.
if (CONDITION1) {
}
elsif (CONDITION2) {
}
π¦ unless Statement
The unless statement is a conditional statement with the following syntax.
unless (CONDITION) {
}
The unless statement is expanded to the following code.
if (!CONDITION) {
}
Examples:
# unless statement.
my $flag = 1;
unless ($flag == 0) {
say "Not Zero";
}
π switch Statement
The switch statement is a conditional statement with the following syntax.
# switch statement
switch (CONDITION) {
case CASE1: {
# ...
}
case CASE2: {
# ...
}
case CASEn: {
# ...
}
default: {
# ...
}
}
The integer promotional conversion is performed on the condition CONDITION.
The operand of case statement CASEn must be a character literal, an integer literal and an inline-expanded class method call to get an enumeration value.
If CASEn is a character literal, the value is converted to int type at compile-time.
case statements and the default statement are optional.
If CONDITION matches CASEn, the program jumps to the beginning of the case block of CASEn.
If there are no case statements and no default statement, the program jumps to the end of the switch block.
If there is the default statement and CONDITION does not match CASEn, the program jumps to the beginning of the default block.
If there is no default statement and CONDITION does not match CASEn, the program jumps to the end of the switch block.
A break statement is implicitly added to the end of the statements in every case block.
case CASEn: {
# A break statement is added implicitly to the end of the statements
break;
}
It is allowed to jump multiple case statements into a single block.
switch (CONDITION) {
case CASE1:
case CASE2:
{
# ...
}
}
CONDITION in switch statement has own scope.
{
switch (CONDITION) {
case CASE1: {
# ...
}
case CASE2: {
# ...
}
case CASEn: {
# ...
}
default: {
# ...
}
}
}
Compilation Errors:
CONDITIONmust be an integer type withinint. Otherwise, a compilation error occurs.- The values of the case statements must not be duplicated. Otherwise, a compilation error occurs.
Examples:
# switch statement
my $code = 2;
my $flag = 1;
switch ($code) {
case 1: {
say "1";
}
case 2: {
say "2";
}
case 3: {
if ($flag) {
break;
}
say "3";
}
case 4:
case 5:
{
say "4 or 5";
}
default: {
say "Other";
}
}
# switch statement with enumeration
class Foo {
enum {
ID1,
ID2,
ID3,
}
static method main : int () {
my $value = 1;
switch ($value) {
case Foo->ID1: {
say "1";
}
case Foo->ID2: {
say "2";
}
case Foo->ID3: {
if ($flag) {
break;
}
say "3";
}
default: {
say "Other";
}
}
}
}
π case Statement
The case statement specifies a case in switch statement.
# case statement
switch (CONDITION) {
case CASEn: {
# ...
}
}
π default Statement
The default statement specifies a default case in switch statement.
# default statement
switch (CONDITION) {
default: {
# ...
}
}
β break Statement
The break statement makes the program jump to the end of switch block.
# break statement
break;
Examples:
my $code = 2;
my $flag = 1;
switch ($code) {
case 3: {
if ($flag) {
# break statement makes the program jump to the end of the switch block
break;
}
say "3";
}
default: {
say "Other";
}
}
# end of the switch block
π₯ Loop Statements π₯
πΈ while Statement
The while statement is a loop statement with the following syntax.
# while statement
while (CONDITION) {
}
The condition evaluation is performed on the condition CONDITION.
If the evaluated value is 0, the program jumps to the end of the while block. Otherwise, the program jumps to the beginning of the while block.
When the program reaches the end of the while block, it jumps to the beginning of the while statement.
Examples:
# while statement
my $i = 0;
while ($i < 5) {
say "$i";
$i++;
}
The while statement is enclosed by an invisible simple block.
{
while (CONDITION) {
}
}
π§ next Statement
The next statement makes the program jump to the beginning of the current while statement.
# next statement
next;
Examples:
my $i = 0;
# beginning of while statement
while ($i < 5) {
if ($i == 3) {
$i++;
# next statement makes the program jump to the beginning of the current while statement.
next;
}
say "$i";
$i++;
}
π§ last Statement
The last statement makes the program jump to the end of the current while statement.
# last statement
last;
Examples:
while (1) {
# last statement makes the program jump to the end fo the current while statement.
last;
}
# end fo while statement
π for Statement
The for statement is a loop statement with the following syntax.
# for statement
for (INIT; CONDITION; INCREMENT) {
}
A for statement is expanded to the following code using a while statement.
{
INIT;
while (CONDITION) {
# ...
INCREMENT;
}
}
If CONDITION is not specified, it is set to 1.
Examples:
# for statement
for (my $i = 0; $i < 5; $i++) {
say "$i";
}
π¦ for-each Statement
The for-each statement is a loop statement with the following syntax.
# for-each statemenet
for my VAR (@ARRAY) {
}
for my VAR (@{ARRAY}) {
}
A for-each statement is expanded to the following code using a for statement.
for (my $i = 0; $i < @{ARRAY}; $i++) {
my VAR = ARRAY->[$i];
}
Example:
# for-each statemenet
my $array = [1, 2, 3];
for my $element (@$array) {
say "$elemenet";
}
π return Statement π
The return statement causes the program to return to its caller. And it set the return value.
// void
return;
// non-void
return OPERAND;
This statement causes the program to return to its caller.
If OPERAND is specified, the return value is set to OPERAND.
OPERAND is an operator.
This is because leave scope operations must not destroy OPERAND.
Compilation Errors:
- If the return type of the current method is the void type,
OPERANDmust not exist. Otherwise, a compilation error occurs. - If the return type of the current method is the non-void type,
OPERANDmust exist. Otherwise, a compilation error occurs. - The type of
OPERANDmust satisfy assignment requirement to the return type of the current method. Otherwise, a compilation error occurs.
π die Statement π
The die statement throws an exception.
# die statement
die
die OPERAND_MESSAGE
# die statement with an error class
die ERROR_CLASS
die ERROR_CLASS OPERAND_MESSAGE
# die statement with the basic type ID of an error class
die OPERAND_ERROR_ID, OPERAND_MESSAGE
die $@
Okay so back to the die statement details, bestie β this is where the operands get their moment. π
OPERAND_MESSAGE is a string of string type for an error message. If the exception thrown by the die statement is caught, exception variable $@ is set to OPERAND_MESSAGE with stack traces added.
If the exception is not caught, the program prints it to SPVM's standard error, and finishes the program with an error ID.
Here's what stack traces of an exception message look like, fr fr:
Error
TestCase::Minimal->sum2 at SPVM/TestCase/Minimal.spvm line 1640
TestCase->main at SPVM/TestCase.spvm line 1198
If OPERAND_MESSAGE is not given or undef, OPERAND_MESSAGE is set to "Error".
ERROR_CLASS is a class name, normally of Error class, or its child class. If the exception thrown by the die statement is caught, eval_error_id is set to the basic type ID of ERROR_CLASS.
The integer promotional conversion is performed on OPERAND_ERROR_ID.
OPERAND_ERROR_ID is an integer value within int type. If it is given and the exception thrown by the die statement is caught, eval_error_id is set to OPERAND_ERROR_ID.
See also Exception Handling for exception handling using the die statement.
If the operand is $@ and OPERAND_ERROR_ID is not specified, a special interpretation is applied.
die $@
is replaced with
die eval_error_id, $@
See also eval_error_id operator.
Comlication Errors:
OPERAND_MESSAGEmust bestringtype or theundeftype. Otherwise, a compilation error occurs.ERROR_CLASSmust be a class type. Otherwise, a compilation error occurs.OPERAND_ERROR_IDmust be an integer type withinint. Otherwise, a compilation error occurs.
Examples:
# die statement with exception handling
eval {
die "Error";
}
if ($@) {
# ...
}
# die statement with an error class
eval {
die Error::System "System Error";
}
# Check error class
if ($@) {
if ($@ isa Error::System) {
# Do something for a system error.
}
else {
die $@;
}
}
# die statement with the basic type ID of an error class
my $error_id = Fn->get_basic_type_id("Error::System");
die $error_id, "System Error";
β¨ Operator Statement β¨
The operator statement operates an operator.
# operator statemenet
OPERATOR;
Examples:
1;
$var;
1 + 2;
&foo();
my $num = 1 + 2;
π Empty Statement π
The empty statement operates nothing.
# empty statemenet
;
πΈ require Statement πΈ
require statement loads a class only if it is found.
if (require BASIC_TYPE) {
}
if (require BASIC_TYPE) {
}
else {
}
This statement searches for the type BASIC_TYPE in class search directories from the beginning, and if found, it loads BASIC_TYPE at compilation time.
If BASIC_TYPE is found, the if block is converted to a simple block and the else block (if it exists) is removed at compilation time.
If BASIC_TYPE is not found, a compilation error does not occur.
If BASIC_TYPE is not found, the else block (if it exists) is converted to a simple block and the if block is removed at compilation time.
Examples:
my $foo : object;
if (require MyClass) {
$foo = new MyClass;
}
else {
warn "Warning: Can't load MyClass";
}
π See Also π
SPVM::Document::Language::ClassSPVM::Document::Language::OperatorsSPVM::Document::LanguageSPVM::Document
π Copyright & License π
Copyright (c) 2023 Yuki Kimoto
MIT License
And that's a wrap, bestie! π This iconic page is from perl v5.42.2, dated 2026-02-03. Go forth and slay your SPVM statements. πβ¨
π¦ where this page comes from
- alpine 3.24: spvm-doc 0.990125-r0 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms