I guess the question starts to boil down to is:

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...):

my %hash; tie( %hash, "Tie::Hash::Stack" ); print defined( %hash ) ? "ok" : "not ok";
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 ( %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; my $obj = tie( %hash, "Tie::Hash::Stack" ); print defined( $obj ) ? "ok" : "not ok"; # or simply $obj ? "ok" : "not ok";
This works, there's no problems, so I can happily continue along wrt T:H:S.

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.


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Re: (tye)Re: Replacement for defined(%)/defined(@)? by Masem
in thread Replacement for defined(%)/defined(@)? by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.