Undefined values: Difference between revisions
Content added Content deleted
m (Correct omit from use) |
(→Tcl: Added implementation) |
||
Line 2: | Line 2: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<lang perl>#!/usr/bin/perl -w |
<lang perl>#!/usr/bin/perl -w |
||
use strict; |
use strict; |
||
Line 47: | Line 46: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<lang php><?php |
<lang php><?php |
||
// Check to see whether it is defined |
// Check to see whether it is defined |
||
Line 90: | Line 88: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
⚫ | |||
<lang python> |
|||
⚫ | |||
try: var |
try: var |
||
except NameError: print "var is undefined at first check" |
except NameError: print "var is undefined at first check" |
||
Line 122: | Line 118: | ||
# Because most of the output is conditional, this serves as |
# Because most of the output is conditional, this serves as |
||
# a clear indicator that the program has run to completion. |
# a clear indicator that the program has run to completion. |
||
print "Done" |
print "Done"</lang> |
||
⚫ | |||
Results in: |
Results in: |
||
Line 133: | Line 128: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
⚫ | |||
<lang ruby> |
|||
⚫ | |||
puts "var is undefined at first check" unless defined? var |
puts "var is undefined at first check" unless defined? var |
||
Line 149: | Line 142: | ||
# Because most of the output is conditional, this serves as |
# Because most of the output is conditional, this serves as |
||
# a clear indicator that the program has run to completion. |
# a clear indicator that the program has run to completion. |
||
puts "Done" |
puts "Done"</lang> |
||
</lang> |
|||
Results in: |
Results in: |
||
<pre>var is undefined at first check |
<pre>var is undefined at first check |
||
Done |
|||
⚫ | |||
=={{header|Tcl}}== |
|||
Tcl does not have undefined ''values'', but ''variables'' may be undefined by being not set. |
|||
<lang tcl># Variables are undefined by default and do not need explicit declaration |
|||
# Check to see whether it is defined |
|||
if {![info exists var]} {puts "var is undefind at first check"} |
|||
# Give it a value |
|||
set var "Screwy Squirrel" |
|||
# Check to see whether it is defined |
|||
if {![info exists var]} {puts "var is undefind at second check"} |
|||
# Remove its value |
|||
unset var |
|||
# Check to see whether it is defined |
|||
if {![info exists var]} {puts "var is undefind at third check"} |
|||
# Give it a value again |
|||
set var 12345 |
|||
# Check to see whether it is defined |
|||
if {![info exists var]} {puts "var is undefind at fourth check"} |
|||
puts "Done"</lang> |
|||
Yields this output: |
|||
<pre> |
|||
var is undefind at first check |
|||
var is undefind at third check |
|||
Done |
Done |
||
</pre> |
</pre> |