in reply to Re: Uninitialized value ?
in thread Uninitialized value ?

You fill your hash:
%words = qw ( fred camel barney llama betty alpaca wilma alpaca );
It now contains four elements with the names of famous actors as keys.

If the user enters one of the keys, everything will be fine, because e.g. $words{fred} is defined, so $secretword will get the value 'camel'.
If the user enters something, that is not in the list of keys, e.g. 'busunsl', then $words{busunsl} is not defined.
And $secretword will become undef.

And you check for an undef value with defined.

You wanted to fill something into $secretword if there was nothing in it, that is, if it is undef.
So your if-statement has to be: if (! defined $secretword) { With the ! meaning 'not'.

Replies are listed 'Best First'.
Re: Re: Re: Uninitialized value ?
by Cine (Friar) on Aug 20, 2001 at 17:55 UTC
    This however alters the way the program works.
    if (!defined $secretword or $secretword eq '') {
    It will now both check if $secretword has some value (possible empty) and if it is empty.
    This can be boiled down to the line
    if (!$secretword) {
    or if you like the unless operator:
    unless ($secretword) {


    T I M T O W T D I
      Well, it can't be '', because it is either undef or has a value from the hash, which are all known to be not ''.
      CINE, Thks for that, learning new stuff all the time.... Steve.
        You're welcome ;)

        T I M T O W T D I