Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: "but" versus "and"

by muba (Priest)
on Sep 27, 2004 at 19:44 UTC ( #394316=note: print w/replies, xml ) Need Help??

in reply to "but" versus "and"
in thread A "but" operator.

If I get that right... it is possible to give one variable multiple values, including undef?
That's cool indeed! But ("but" :) )... how can a variable have a (or more) value(s) and still be undefined?

"2b"||!"2b";$$_="the question"
Besides that, my code is untested unless stated otherwise.
One more: please review the article about regular expressions (do's and don'ts) I'm working on.

Replies are listed 'Best First'.
Re^2: "but" versus "and"
by jonadab (Parson) on Sep 27, 2004 at 20:11 UTC
    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
Re^2: "but" versus "and"
by hardburn (Abbot) on Sep 27, 2004 at 20:32 UTC

    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://394316]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2018-06-18 16:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.