in reply to (tye)Re: Replacement for defined(%)/defined(@)?
in thread Replacement for defined(%)/defined(@)?
Does the so-called empty set (or hash) exist in Perl, and if so, is it possible to distiquish it from the undef case?
Let me back up and go back to my problem from last night. I'm writing test cases for Tie::Hash::Stack, and in the creation set, I want to make sure that the hash was actually tied, as it's possible to send a parameter that breaks the function (normally the function would catch and die at that point, but for test cases, I want to catch that problem). So I used something similar to this (yes, I know defined is wrong here, but stay with me...):
Now, the object that I create with T:H:S is an array of hashes, and upon creation, an empty hash is pushed onto the array (e.g.: "( {} )" ). Thus, the hash may be empty, but it is certainly not undefined. Thus, this code:my %hash; tie( %hash, "Tie::Hash::Stack" ); print defined( %hash ) ? "ok" : "not ok";
fails to note the creation of the tie'd variable. At the time, I didn't know that tie returned anything that I could use to judge success or failure (note that the perlfunc:tie do not state anything about a return value, had to find that out for myself, which appears to be the object that the hash was tied to), but after I got to this problem, I eventually got to:my %hash; tie( %hash, "Tie::Hash::Stack" ); print ( %hash ) ? "ok" : "not ok";
This works, there's no problems, so I can happily continue along wrt T:H:S.my %hash; my $obj = tie( %hash, "Tie::Hash::Stack" ); print defined( $obj ) ? "ok" : "not ok"; # or simply $obj ? "ok" : "not ok";
But the underlying problem of the fact that an empty array or hash is undistinguisable from the undef case somewhat bothers me. The example code in my original post, for example, would require a change of logic as tye's pointed out in order to have the empty set and an error code separated. If I was really dealing with, on mathematical terms, of the empty set, I'd probably revert to some code like Functional.pm that gives me an object that acts like the empty set.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re2: Replacement for defined(%)/defined(@)?
by tye (Sage) on Jun 29, 2001 at 23:41 UTC | |
|
Re: Re: (tye)Re: Replacement for defined(%)/defined(@)?
by John M. Dlugosz (Monsignor) on Jun 29, 2001 at 23:35 UTC |