Commits

Anonymous committed 5baa49d

Added the lookup hash by key.

Comments (0)

Files changed (4)

perl5/ext-embed-internals/docbook/examples/incremental-examples-1/XSTest/MANIFEST

 t/09-assign-to-array.t
 t/10-array-from-scratch.t
 t/11-lookup-hash-constant.t
+t/12-lookup-hash-by-key.t
 t/boilerplate.t
 t/lib/scripts/get-24-leak-test.pl
 t/lib/scripts/get-string-leak-test.pl

perl5/ext-embed-internals/docbook/examples/incremental-examples-1/XSTest/lib/XSTest.pm

 This function is meant to demonstrate hash lookup. Returns undef if the
 hash does not contain the key C<'mystring'> .
 
+=head2 $value = lookup_value_in_hash(\%hash, $key)
+
+Equivalent to C<< $value = $hash{$key} >>
+
+This function is meant to demonstrate hash lookup with a user-given or
+run-time specified key. Returns undef if the hash does not contain the key.
+
 =head1 AUTHOR
 
 Shlomi Fish, C<< <shlomif@iglu.org.il> >>

perl5/ext-embed-internals/docbook/examples/incremental-examples-1/XSTest/lib/XSTest.xs

     OUTPUT:
         RETVAL
 
+SV *
+lookup_value_in_hash(HV * hash, SV * key)
+    PREINIT:
+        HE * hash_entry;
+    CODE:
+        /* hv_fetch_ent() looks up a hash based on an SV * key. It returns
+         * the hash entry, which needs to be looked up using HeVAL.
+         *
+         * It also accepts a hash value, which is based on the hash table
+         * calculations, and an lval.
+         * */
+        if ((hash_entry = hv_fetch_ent(hash, key, 0, 0)))
+        {
+            RETVAL = newSVsv(HeVAL(hash_entry));
+        }
+        else
+        {
+            RETVAL = &PL_sv_undef;
+        }
+
+    OUTPUT:
+        RETVAL
+

perl5/ext-embed-internals/docbook/examples/incremental-examples-1/XSTest/t/12-lookup-hash-by-key.t

+use strict;
+use warnings;
+
+use Test::More tests => 5;
+
+use XSTest;
+
+{
+    my $value = XSTest::lookup_value_in_hash(
+        { 'key1' => "Success100" }, 
+        "key1"
+    );
+
+    # TEST
+    is ($value, "Success100", "Lookup is OK.")
+}
+
+{
+    my %hash =
+    (
+        'first' => "David",
+        'last' => "Ben-Yishay",
+        'title' => "King",
+        'sex' => "M",
+        'age' => 25,
+        'wife' => "Michal",
+    );
+
+    # TEST
+    is (XSTest::lookup_value_in_hash(
+            \%hash,
+            "first"
+        ),
+        "David",
+        "Lookup first",
+    );
+
+    # TEST
+    is (XSTest::lookup_value_in_hash(
+            \%hash,
+            "age"
+        ),
+        25,
+        "Lookup age",
+    );
+
+    # TEST
+    ok (!defined(XSTest::lookup_value_in_hash(
+            \%hash,
+            "does-not-exist",
+        )),
+        "Lookup of non-existent key."
+    );
+}
+
+{
+    my $value = XSTest::lookup_value_in_hash(
+        { "012\0abc" => "That's OK", }, 
+        "012\0abc",
+    );
+
+    # TEST
+    is ($value, "That's OK", "Lookup of strings containing NUL is OK.")
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.