how can a variable have a (or more) value(s) and still be undefined?
You wouldn't normally want the variable to be undef,
if it has meaningful values. But what if its value
is an error code? Say, you want to return undef
(so that the calling code knows it is getting an
error, not a real value), but you want to return
different _kinds_ of undef -- for example, if the
error message is due to a builtin failing maybe
you want to
return ($! but undef), so that the
calling code can try (if it wants to bother) to
figure out what went wrong. One of the Apocalypse
articles talks about unthrown protoexceptions --
that is, returning a value that is undef (to
signal failure) but also knows how to complain in
a meaningful way if it is thrown as an exception.
However, the string/number duality is easier to see
the benefit of for normal code. For example,
my $postoffice = ("Galion" but 44833);
Of course, you won't _have_ to do things this way.
You can always go with more standard data structures,
such as
my $postoffice = +{ name => "Galion", zip => 44833 }; however, that requires the calling code to
understand your data structure. In some cases, especially
for modules, it might be better to return an object that
knows how to behave different ways depending on how the
calling code chooses to treat it. In other words, an
object that understands and can respond to context.
0 but true is probably the most obviously
useful example. We have people doing "0 but true" in
Perl5 already, when returning false is wrong but 0 is
the correct number. This is a symptom of the fact that
the rich set of different kinds of context in Perl
really makes it necessary for objects to be able to
evaluate differently in different contexts.
"In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."
— Pratico & Van Pelt, BBHG, p68
| [reply] [d/l] [select] |
it is possible to give one variable multiple values, including undef?
Yes. In fact, it's already done that way in Perl5, though you can't access the data directly from pure Perl (I believe there are XS modules that can do it).
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.
| [reply] |