# New Ticket Created by  Colin Kuskie 
# Please include the string:  [perl #45185]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=45185 >


This patch adds tutorial information about exists.  It was written completely 
from scratch, and it would be good for some of the PIR-knowledgeable guys to 
review it before it is applied, to make sure that all the semantics are 
correct.                   
Index: examples/tutorial/57_exists.pir
===================================================================
--- examples/tutorial/57_exists.pir     (revision 0)
+++ examples/tutorial/57_exists.pir     (revision 0)
@@ -0,0 +1,48 @@
+=head1 exists
+
+The C<exists> opcode tells you if the contents of a PMC, such as an array or
+hash, exists.  It differs from C<defined>, because C<defined> will return false
+on undefined values, which are valid entries.
+
+In the example below, C<my_array[0]> exists and is defined, but C<my_array[1]> 
only
+exists.  Similar to C<defined>, the behavior of C<exists> for a given PMC 
depends
+on how that PMC implements vtable functions.
+
+=cut
+
+.sub main :main
+
+    .local pmc my_array
+    my_array = new 'ResizablePMCArray'
+    $P0 = new 'String'
+    $P0  = "Hello"
+    $P1 = new 'Undef'
+    push my_array, $P0
+    push my_array, $P1
+
+    $I0 = defined my_array[0]
+    $I1 = exists my_array[0]
+    $I2 = $I0 && I1
+
+    unless $I2 goto not_def_not_exists
+    say "my_array[0] is defined and it exists"
+  not_def_not_exists:
+
+    $I0 = defined my_array[1]
+    $I1 = exists my_array[1]
+
+    unless $I0 goto not_def
+    say "my_array[1] is defined"
+  not_def:
+    unless $I1 goto not_exists
+    say "my_array[1] exists"
+  not_exists:
+       
+.end
+
+# Local Variables:
+#   mode: pir
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4:
+
Index: t/examples/tutorial.t
===================================================================
--- t/examples/tutorial.t       (revision 21079)
+++ t/examples/tutorial.t       (working copy)
@@ -7,7 +7,7 @@
 use lib qw( . lib ../lib ../../lib );
 
 use Test::More;
-use Parrot::Test tests => 34;
+use Parrot::Test tests => 35;
 use Parrot::Config;
 
 =head1 NAME
@@ -81,6 +81,7 @@
 
     '22_string_ops_length.pir' => << 'END_EXPECTED',
 5
+13
 END_EXPECTED
 
     '23_string_ops_substr.pir' => << 'END_EXPECTED',
@@ -164,6 +165,11 @@
 $P3 is undefined
 END_EXPECTED
 
+    '57_exists.pir' => << 'END_EXPECTED',
+my_array[0] is defined and it exists
+my_array[1] exists
+END_EXPECTED
+
     '60_subroutines.pir' => << 'END_EXPECTED',
 Hello, Zaphod
 END_EXPECTED

Reply via email to