Perl: can a computer understand “or” the way a human does?

Self-tutoring about computer science: the tutor explores the concept of “or”.

In English, “or” means a choice: ham or bacon, for instance.

In computer science, “or” is often called a logical operator, implying it yields 1 if true, but 0 if false. Yet, can a computer use “or” to make a choice, like a person can? Apparently so. Consider the following Perl code:

$var1=’hello’; $var2=$var3||$var1; print $var2;

The output:

hello

$var3 is undefined, while $var1=”hello”. || means “or”. Given the choice between the undefined $var3 and the defined $var1, Perl chooses the latter and returns “hello” rather than 1.

Source:

McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.

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

Leave a Reply