Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check if a Perl hash is defined when it doesn't have any key/value pairs
    primarykey
    data
    text
    <p><em>Update Note: I've modified this question to hopefully ask it in a better way. Originally, I had more code that was attempting to demonstrate what I was after, but it was only getting in the way. Also, I was making references to "defined". It turns out that wasn't really what I was after. The updated question should clarify what I'm trying to do.</em></p> <hr> <p>I'm trying to figure out a way to check if a hash has been declared in a perl script while inside a module function.</p> <p>Given the following script (script.pl):</p> <pre><code>#!/usr/bin/perl -w use strict; use checkHash; our %testHash = ("key"=&gt;"value"); print &amp;checkHash() ? 1 : 0; </code></pre> <p>Along with this module (checkHash.pm);</p> <pre><code>sub checkHash { if(%main::testHash) { return 1; } else { return 0; } } 1; </code></pre> <p>Running on a Mac with OS X 10.6.7 and perl v5.10.0, the return value of checkHash is "1" which I would expect. However, if I remove the key value pair from the script by changing the line:</p> <pre><code>our %testHash = ("key"=&gt;"value"); </code></pre> <p>to:</p> <pre><code>our %testHash = (); </code></pre> <p>The return value is "0". My original assumption was that this would be "1", but after reading about the way the test I'm using is testing for the size in the hash, I see why that was incorrect thinking. </p> <p>My questions is:</p> <p><strong>Is there a test that can be used in the checkHash module that will return true if the hash it's testing exists, but doesn't have any keys assigned to it?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload