in reply to -w in production environment

You actually want this:
if (exists $USER{coffee} && $USER{coffee} > 5){$msg='You drink too muc +h coffee'}

It checks that the coffee key exists in %USER. You can just use

if ($USER{coffee} && $USER{coffee} > 5){$msg='You drink too much coffe +e'}
It will get rid of the warning, but won't actually do anything useful. exists checks that the coffee key was created. This check actually tells you a potentially relevant piece of information. e.g
if (! exists $USER{coffee}){ $msg = 'You drank no coffee?!?!'; } elsif ($USER{coffee} > 5){ $msg='You drink too much coffee'; } else { $msg='You drank a moderate amount of coffee'; }
There is no point in adding code just to get rid of a warning if the new code does nothing useful! It is a poor practice to get into and can be confusing to the people that have to maintain you application. Like Abigail said check your input to make sure no one made a mistake inputting a value (or is pulling a fast one on you).

This doesn't seem to be the behaviour that you want.

HTH,
Clayton

Update:
I removed a section that said testing if a non exsistant key was defined would autovivify it and added my support for Abigail's answer. I swear with God as my witness...

Replies are listed 'Best First'.
Re: Re: -w in production environment
by arturo (Vicar) on Jul 06, 2001 at 01:45 UTC
    If you used defined $USER{coffee} as a check it will actually autovivify (automatically create) the coffee key as it does when you do your current test.

    That's not true:

    use warnings; use strict; my %c = ( hey=>'Joe', where=>'you goin\'', with=>'that gun in your hand'); if (defined $c{Hendrix}) { print "Jimi is alive and well\n"; } foreach ( sort keys %c ) { print "Key : $_ => $c{$_}\n"; }

    This does not print "Hendrix" and yield up a warning on the loop that goes through the keys (Perl 5.6.1, Win32).

    Of course defined $hash{key} and exists $hash{key} do test for different things:

    $hash{key} = undef; #defined $hash{key} false, exists $hash{key} true
    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'