Changeset 10038 for docs/Perl6/FAQ

Show
Ignore:
Timestamp:
04/19/06 15:12:00 (3 years ago)
Author:
arathorn
Message:

Add elucidation from audrey on how Captures work in the context of
Rules, and how they can be used as a natural data type for XML-style
annotated nodes with children. Summarized from:

http://colabti.de/irclogger/irclogger_log/perl6?date=2006-04-19,Wed&sel=212#l318

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • docs/Perl6/FAQ/Capture.pod

    r9985 r10038  
    1717 
    1818A Capture is an object representing arguments in a function call. 
     19More generally, Capture objects express annotated nodes with children 
     20such as capture groups returned by rules, XML DOM nodes, argument lists 
     21and more. 
    1922 
    2023For example, the arguments in C<say("Hello", "World")> is a Capture 
     
    342345    $x = 42;        # FAIL - Int doesn't handle scalar assignment either 
    343346 
     347=head3 So how do Captures work with Rules? 
     348 
     349In the context of rules, Captures are superclasses of Match. So in the example of: 
     350     
     351    my $rv = ("x-y-z-moose" ~~ /(.)-(.)-(.)-<animal> :{ return give_birth(*$/) }) 
     352 
     353C<give_birth()> gets called with 'x', 'y', and 'z' as positional arguments, 
     354and :animal<moose> as a named argument.  C<give_birth()> can return a Moose object - 
     355and C<$rv> is assigned a Capture object with the Moose object in its invocant slot. 
     356C<$rv> has the same positional and named slots as the Moose object - and you can 
     357retrieve the Moose back through C<$rv as Animal>. 
     358 
     359Nested captures in the rule then become nested Capture objects within positional 
     360slots in $/, which allows them to be retrieved as arguments for additional functions. 
     361And so you can bind annotated nodes of a tree to particular function calls, passing 
     362the data straight in thanks to the equivalence of Captures returned by rules and 
     363Captures used to invoke functions and methods.  As such, Captures could be considered 
     364a natural data type for XML nodes, and provide considerable power for parsing DOMs using 
     365Rules, and providing native tree manipulations. 
     366 
     367 
    344368=head2 Why 
    345369