Undefined values: Difference between revisions
Content added Content deleted
(slight correction) |
|||
Line 21: | Line 21: | ||
# Give the variable an undefined value. |
# Give the variable an undefined value. |
||
$var = undef; |
$var = undef; |
||
# or, equivalently: |
|||
undef $var; |
|||
# Check to see whether it is defined after we've explicitely |
# Check to see whether it is defined after we've explicitely |
Revision as of 06:19, 28 November 2009
Undefined values
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
For languages which have an explicit notion of an undefined value, (as opposed to a NULL value), identify and exercise those language's mechanisms for identifying and manipulating a variable's value's status as being undefined
Perl
<lang perl>#!/usr/bin/perl -w use strict;
- Declare the variable
our $var;
- Check to see whether it is defined
print "var is undefined at first check\n" unless defined $var;
- Give it a value
$var = "Chocolate";
- Check to see whether it is defined after we gave it the
- value "Chocolate"
print "var is undefined at second check\n" unless defined $var;
- Give the variable an undefined value.
$var = undef;
- or, equivalently:
undef $var;
- Check to see whether it is defined after we've explicitely
- given it an undefined value.
print "var is undefined at third check\n" unless defined $var;
- Give the variable a value of 42
$var = 42;
- Check to see whether the it is defined after we've given it
- the value 42.
print "var is undefined at fourth check\n" unless defined $var;
- Because most of the output is conditional, this serves as
- a clear indicator that the progrma has run to completion.
print "Done\n";</lang>
Results in:
var is undefined at first check var is undefined at third check Done