Perl: $variable status: the “if” test

Tutoring computer programming, some issues that seem negligible become very important to consider.  The tutor discusses the status of variables.

 
In computer programming blogs, as well as in some books, you’ll often see the construction

if($variable)
{
these instructions;
}
else
{
these instructions;
}

The idea is that if $variable has been defined and given a non-zero value, for example

$variable=14;

or

$variable=”hello”;

then the program will execute the “if” set of instructions. However, if

$variable=0;

or $variable is simply not declared in the program, the “else” set of instructions will be executed.

As we’ll see in coming posts, this “if” test is very convenient. Moreover, you see it so often in articles about programming, you might think it’s generic. However, not all programming languages support the “if” test as written above. Rather, some require an expression in the parentheses, such as

if($variable equals value)

or

if($variable not equal to value)

Happily, Perl does support the if($variable) test. I wonder if some writers who use it in articles are Perl programmers who forget that it doesn’t work in some other languages.

I’ll be exploring the “if”, “else” construction, as well as the “if” test, in future posts. Cheers:)

Source: Robert’s Perl Tutorial

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Tagged with: , , ,

Leave a Reply