Aaron Sherman <[EMAIL PROTECTED]> writes:
> On Wed, 2003-01-29 at 14:54, Jonathan Scott Duff wrote:
>
>> Can someone give me a realish world example of when you would want an
>> array that can store both undefined values and default values and those
>> values are different?
>
> my @send_partner_email is default(1);
> while $websignups.getline {
> ($id) = /UserID: (\d+)/;
> if /Source: External DB With No Privacy Policy/ {
> @send_partner_email[$id] = undef; # No answer given
> } elsif /Spam Me: Yes/ {
> @send_partner_email[$id] = 1;
> } else {
> @send_partner_email[$id] = 0;
> }
> }
> # If you were not in the websignups list, you signed up before privacy
> # policy, so we spam you (default=1)
>
> In this case, there's a true "shrug" answer, which is hard to deal with.
> We need to do something later on with the undefined case (no answer was
> given, and no spam warning issued). This sort of logic deferral is
> common to many uses of undefined values (or "NULL") in databases, even
> when columns have defaults that are non-null.
In this case surely you're better off doing this with a state object.
class UnspecifiedState {
method maybe_send_mail($user) { go_get_more_info }
}
class SendMailState {
method maybe_send_mail($user) { generate_and_send_mail_to($user) }
}
class NoSendMailState {
method maybe_send_mail($user) { }
}
my @send_partner_email is default(SendMailState.new);
while $websignups.getline {
($id) = /UserId: (\d+)/;
when /Source: External DB With No Privacy Policy/ {
@send_partner_email[$id] = UnspecifiedState.new;
}
when /Spam Me: Yes/ {
@send_partner_email[$id] = SendMailState.new;
}
otherwise {
@send_partner_email[$id] = NoSendMailState.new;
}
}
in other words, when you have to deal with tristate logic, make it
explicit tri state logic.
--
Piers