Re: Baby Steps
by davorg (Chancellor) on Jun 23, 2000 at 00:04 UTC
|
Perl values can be defined or undefined and true or false
(all undefined values are also false). Elements in a hash can, in
addition, exist or not. The following code might go some
way to explaining the differences:
#!/usr/bin/perl -w
use strict;
my %hash = (a => 1,
b => 0,
c => undef);
foreach ('a' .. 'd') {
print "\$hash{$_) : ";
if (exists $hash{$_}) {
if (defined $hash{$_}) {
if ($hash{$_}) {
print "exists, defined & true\n";
} else {
print "exists, defined & false\n";
}
} else {
print "exists, undefined & false\n"
}
} else {
print "non-existant, undefined & false\n";
}
}
--
<http://www.dave.org.uk>
European Perl Conference - Sept 22/24 2000
<http://www.yapc.org/Europe/> | [reply] [d/l] |
RE: Baby Steps
by flyfishin (Monk) on Jun 22, 2000 at 23:47 UTC
|
If I remember correctly 0, null string, and empty string are all false answers. However,
0 and empty string are defined answers. $kaka ne "" checks to see if $kaka
is equal to the empty string. Whereas defined $kaka checks
to see if $kaka is something other than 0, null, or empty. I also think there
are some differences for defined and exists. I think exists
only applies to hash values. I know this isn't a complete answer
but it gets you pointed in the right direction, I hope. Check in perlfunc for
more complete answers.
Update:
Oops. You can uses exists to check on array elements. Next time I'll
read perlfunc before shooting off with the fingers. | [reply] [d/l] [select] |
|
|
Well... defined $kaka actually checks to see if $kaka is defined at ALL. something that is defined as zero, null, or empty is still defined.
sean@anticharm:~$ perl -e 'print "defined!\n" if defined $foo'
sean@anticharm:~$ perl -we '$foo = 0; print "defined!\n" if defined $f
+oo'
defined!
sean@anticharm:~$ perl -we '$foo = ""; print "defined!\n" if defined $
+foo'
defined!
As far as I am aware, once you define something you must use undef (or assign it the value of something undefined: undef $baz; $foo=$baz;) if you want it to be undefined again. | [reply] [d/l] |
|
|
| [reply] |
Re: Baby Steps
by tenatious (Beadle) on Jun 23, 2000 at 01:14 UTC
|
| [reply] |
RE: Baby Steps
by Shendal (Hermit) on Jun 22, 2000 at 23:57 UTC
|
| [reply] |
Re: Baby Steps
by Anonymous Monk on Jun 23, 2000 at 02:36 UTC
|
Page 155 of the Camel book distinguishes between an undefined string and a null one. St. Larry writes: "Many operations return the undefined value under exceptional conditions, such as the end of file, uninitialized variable, system error, and such. This defined function allows you to distinguish between an undefined null string and a defined null string when you're using operators that might return a real null string." | [reply] |
Re: Baby Steps
by PipTigger (Hermit) on Jun 22, 2000 at 23:44 UTC
|
... also: Does `$kaka = param("formkaka");` define $kaka (according to if (defined $kaka)) even if the parameter "formkaka" was not submitted? Thanks again. TTFN
-PipTigger
p.s. I just realized that root@eruditorium.org is not necessarily the superuser of that server since Enoch is... =)! p.p.s. Do we have to escape double quotes in titles somehow so that they appear correctly? I missed that too! p.p.p.s. Please forgive me for forgetting to close the anchor tag in the main post. I'd change it if I could. | [reply] |
|
|
If you're using CGI.pm, it looks like calling param() for an unsubmitted parameter returns undef. The snippet I used to test this is:
print "Undefined parameter: >>";
print defined ($q->param('undefined')) ? "defined" : "not defined";
print "<<\n<p>";
(Obviously, there's more to it than that, but anyone who knows enough to ask this question ought to know what else to add.) | [reply] [d/l] |
|
|
'exists' is used to find out if a hash key exists,
this is needed because perl will create a key/value pair even if
you are just 'seeing if it's there'. (e.g, its
not use on scalars as such)
also, the -e (and friends) flags are file test operators
where parameter is the name of a file.
if (-e /mnt/dos/command.com)
{
print "good choice, show who's in charge\n";
}
see 'perldoc perlfunc' for a list of the file tests you can perform.
| [reply] |
| A reply falls below the community's threshold of quality. You may see it by logging in. |