PipTigger has asked for the wisdom of the Perl Monks concerning the following question:

I know this is way simple stuff but I'm not clear on the behaviour of different "if" accompaniments. Is if (defined $kaka) { print $kaka; } the same as...if ($kaka ne "") { print $kaka; } I know that sometimes $kaka = 0; so just if ($kaka) { print $kaka; } fails when I don't want it to. My questions are: How exactly does "defined" behave? Is there a difference between if (-e $kaka) && if (exists $kaka)? Are there other if options (like -E for !-e)?
I know this is totally a RTFM post but there's so much stuff to wade through just to find these answers that I thought some monk would know precisely or at least the location of a concise description of Perl if options and behavior. Thanks very much. TTFN & Shalom.

-PipTigger

p.s. HaDouKen!

Replies are listed 'Best First'.
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/>
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.
      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.
      Using exists on an array is a new feature in 5.6

      For those of us too lazy (busy) to upgrade, you were right. :-)

      Russ

Re: Baby Steps
by tenatious (Beadle) on Jun 23, 2000 at 01:14 UTC

    there are three possibilities with CGI parameters

    • Parameter was submitted with a value
    • Parameter was submitted without a value (i.e. an empty text field)
    • Parameter was not submitted
    The first case the parameter will have a value.
    The second case the parameter will be defined but will not have a value
    The third case the parameter will neither be defined nor will it have a value

RE: Baby Steps
by Shendal (Hermit) on Jun 22, 2000 at 23:57 UTC
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."
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.
      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.)
      '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.
A reply falls below the community's threshold of quality. You may see it by logging in.