in reply to Error in Camel Book Ver2?
unless($list_info{subscribed_message} ne "" || exists $list_info{subscribed_message}) { $list_info{subscribed_message} = $subscribed_message }
The first thing I notice here is that your test is redundant. If x ne "" is true, then exists x must also be true. (Or equivalently, if exists $h{k} is not true, then k is not in the hash, so $h{k} must be equal to "".) So you should rewrite this as:
which is something of an improvement already.if ($list_info{subscribed_message} eq "") { $list_info{subscribed_message} = $subscribed_message; }
Now, perhaps this would be a good place to use a mutator subroutine?
Or you could abbreviate a little futher and get rid of the mutator:sub set_if_not_set_already { $_[0] = $_[1] if $_[0] eq ""; } set_if_not_set_already($list_info{subscribed_message}, $subscribed_message);
This is neither a recommendation nor a disrecommendation. Just something to consider.sub initialize_list_info { $list_info{$_[0]} = $_[1] if list_info{$_[0]} eq ""; } initialize_list_info('subscribed_message', $subscribed_message);
|
---|