@@ -248,10 +248,10 @@ between '/'s must be one of those CRUD values, otherwise the parsing fails.
248
248
Exactly what we want.
249
249
250
250
There's another technique that provides greater flexibility and improved
251
- readability when options grow large: proto-regexes.
251
+ readability when options grow large: I < proto-regexes > .
252
252
253
- To utilize these proto-regexes (multimethods, in fact) to limit ourselves to the
254
- valid CRUD options, we'll replace " token command" with the following:
253
+ To utilize these proto-regexes (multimethods, in fact) to limit ourselves to
254
+ the valid CRUD options, we'll replace C < token command > with the following:
255
255
256
256
= begin code
257
257
proto token command {*}
@@ -261,26 +261,27 @@ valid CRUD options, we'll replace "token command" with the following:
261
261
token command:sym<delete> { <sym> }
262
262
= end code
263
263
264
- The 'sym' keyword is used to create the various proto-regex options. Each option
265
- is named (e.g., sym<update>), and for that option's use, a special <sym> token is
266
- auto-generated with the same name.
267
-
268
- The <sym> token, as well as other user-defined tokens, may be used in the proto-
269
- regex option block to define the specific 'match condition'. Regex tokens are
270
- compiled forms and, once defined, cannot subsequently be modified by adverb
271
- actions (e.g., :i). Therefore, as it's auto-generated, the special <sym> token
272
- is useful only where an exact match of the option name is required.
273
-
274
- If, for one of the proto-regex options, a match condition occurs, then the whole
275
- proto's search terminates. The matching data, in the form of a match object, is
276
- assigned to the parent proto token. If the special <sym> token was employed and
277
- formed all or part of the actual match, then it's preserved as a sub-level in
278
- the match object, otherwise it's absent.
279
-
280
- Using proto-regexes like this gives us a lot of flexibility. For example,
281
- instead of returning <sym>, which in this case is the entire string that was
264
+ The C < sym > keyword is used to create the various proto-regex options.
265
+ Each option is named (e.g., C « sym<update> » ), and for that option's use,
266
+ a special C « <sym> » token is auto-generated with the same name.
267
+
268
+ The C « <sym> » token, as well as other user-defined tokens, may be used in the
269
+ proto-regex option block to define the specific I < match condition > .
270
+ Regex tokens are compiled forms and, once defined, cannot subsequently be
271
+ modified by adverb actions (e.g., C < :i > ). Therefore, as it's auto-generated,
272
+ the special C « <sym> » token is useful only where an exact match of the option
273
+ name is required.
274
+
275
+ If, for one of the proto-regex options, a match condition occurs, then
276
+ the whole proto's search terminates. The matching data, in the form of a match
277
+ object, is assigned to the parent proto token. If the special C « <sym> » token
278
+ was employed and formed all or part of the actual match, then it's preserved as
279
+ a sub-level in the match object, otherwise it's absent.
280
+
281
+ Using proto-regex like this gives us a lot of flexibility. For example,
282
+ instead of returning C « <sym> » , which in this case is the entire string that was
282
283
matched, we could instead enter our own string, or do other funny stuff. We
283
- could do the same with the " token subject" method and limit it also to only
284
+ could do the same with the C < token subject > method and limit it also to only
284
285
parsing correctly on valid subjects (like 'part' or 'people', etc.).
285
286
286
287
= head2 Putting our RESTful grammar together
@@ -306,7 +307,7 @@ This is what we have for processing our RESTful URIs, so far:
306
307
307
308
Let's look at various URIs and see how they work with our grammar.
308
309
309
- = begin code :skip-test
310
+ = begin code :preamble<grammar REST{};>
310
311
my @uris = ['/product/update/7/notify',
311
312
'/product/create',
312
313
'/item/delete/4'];
@@ -317,16 +318,19 @@ Let's look at various URIs and see how they work with our grammar.
317
318
}
318
319
319
320
# OUTPUT: «Sub: product Cmd: update Dat: 7/notify
320
- # Sub: product Cmd: create Dat:
321
- # Sub: item Cmd: delete Dat: 4»
321
+ # Sub: product Cmd: create Dat: 
322
+ # Sub: item Cmd: delete Dat: 4 »
322
323
= end code
323
324
325
+ Note that since C « <data> » matches nothing on the second string, C « $m<data> »
326
+ will be C < Nil > , then using it in string context in the C < say > function warns.
327
+
324
328
With just this part of a grammar, we're getting almost everything we're
325
329
looking for. The URIs get parsed and we get a data structure with the data.
326
330
327
331
The I < data > token returns the entire end of the URI as one string. The 4 is
328
- fine. However from the '7/notify', we only want the 7. To get just the 7, we'll use
329
- another feature of grammar classes: actions.
332
+ fine. However from the '7/notify', we only want the 7. To get just the 7,
333
+ we'll use another feature of grammar classes: I < actions > .
330
334
331
335
= head1 Grammar Actions
332
336
@@ -338,10 +342,9 @@ grammars. A lot of the time you'll be happy using grammars all by their own.
338
342
But when you need to further process some of those strings, you can plug in the
339
343
Actions expansion module.
340
344
341
- To work with actions, you use a named parameter called
342
- "actions" which should contain an instance of your actions class. With the
343
- code above, if our actions class called REST-actions, we would parse the
344
- URI string like this:
345
+ To work with actions, you use a named parameter called C < actions > which
346
+ should contain an instance of your actions class. With the code above, if our
347
+ actions class called REST-actions, we would parse the URI string like this:
345
348
346
349
= begin code :skip-test
347
350
my $matchObject = REST.parse($uri, actions => REST-actions.new);
@@ -353,12 +356,12 @@ URI string like this:
353
356
354
357
If you I < name your action methods with the same name as your grammar methods >
355
358
(tokens, regexes, rules), then when your grammar methods match, your action
356
- method with the same name will get called automatically. The method will also be
357
- passed the corresponding match object (represented by the $/ variable).
359
+ method with the same name will get called automatically. The method will also
360
+ be passed the corresponding match object (represented by the C < $/ > variable).
358
361
359
362
Let's turn to an example.
360
363
361
- = head1 Grammars by Example with Actions
364
+ = head2 Grammars by example with actions
362
365
363
366
Here we are back to our grammar.
364
367
@@ -380,9 +383,9 @@ Here we are back to our grammar.
380
383
= end code
381
384
382
385
Recall that we want to further process the data token "7/notify", to get the 7.
383
- To do this, we'll create an action class that has a method with the same name as
384
- the named token. In this case, our token is named " data" so our method is also
385
- named " data" .
386
+ To do this, we'll create an action class that has a method with the same name
387
+ as the named token. In this case, our token is named C < data > so our method
388
+ is also named C < data > .
386
389
387
390
= begin code
388
391
class REST-actions
@@ -391,25 +394,25 @@ named "data".
391
394
}
392
395
= end code
393
396
394
- Now when we pass the URI string through the grammar, the " data" token match
395
- will be passed to the REST-actions' " data" method. The action method will split
396
- the string by the '/' character and the first element of the returned
397
+ Now when we pass the URI string through the grammar, the I < data token match >
398
+ will be passed to the I < REST-actions' data method > . The action method will
399
+ split the string by the '/' character and the first element of the returned
397
400
list will be the ID number (7 in the case of "7/notify").
398
401
399
402
But not really; there's a little more.
400
403
401
- = head2 Keeping grammars with actions tidy with " make" and " made"
404
+ = head2 Keeping grammars with actions tidy with C < make > and C < made >
402
405
403
406
If the grammar calls the action above on data, the data method will be called,
404
- but nothing will show up in the big TOP grammar match result returned to our
405
- program. In order to make the action results show up, we need to call " make" on
406
- that result. The result can be many things, including strings, array or
407
+ but nothing will show up in the big C < TOP > grammar match result returned to our
408
+ program. In order to make the action results show up, we need to call L < make >
409
+ on that result. The result can be many things, including strings, array or
407
410
hash structures.
408
411
409
- You can imagine that the " make" places the result in a special contained area
410
- for a grammar. Everything that we " make" can be accessed later by " made" .
412
+ You can imagine that the C < make > places the result in a special contained area
413
+ for a grammar. Everything that we C < make > can be accessed later by L < made > .
411
414
412
- So instead of the REST-actions class above, we should write
415
+ So instead of the REST-actions class above, we should write:
413
416
414
417
= begin code
415
418
class REST-actions
@@ -418,13 +421,13 @@ So instead of the REST-actions class above, we should write
418
421
}
419
422
= end code
420
423
421
- When we add " make" to the match split (which returns a list), the action will
424
+ When we add C < make > to the match split (which returns a list), the action will
422
425
return a data structure to the grammar that will be stored separately from
423
- the " data" token of the original grammar. This way, we can work with both
426
+ the C < data > token of the original grammar. This way, we can work with both
424
427
if we need to.
425
428
426
429
If we want to access just the ID of 7 from that long URI, we access the first
427
- element of the list returned from the " data" action that we " made" with "make" :
430
+ element of the list returned from the C < data > action that we C < made > :
428
431
429
432
= begin code :skip-test
430
433
my $uri = '/product/update/7/notify';
@@ -435,21 +438,20 @@ element of the list returned from the "data" action that we "made" with "make":
435
438
say $match<command>.Str; # OUTPUT: «update»
436
439
= end code
437
440
438
- Here we call " made" on data, because we want the result of the action that
439
- we " made" (with " make" ) to get the split array. That's lovely! But, wouldn't
440
- it be lovelier if we could " make" a friendlier data structure that contained
441
+ Here we call C < made > on data, because we want the result of the action that
442
+ we C < made > (with C < make > ) to get the split array. That's lovely! But, wouldn't
443
+ it be lovelier if we could C < make > a friendlier data structure that contained
441
444
all of the stuff we want, rather than having to coerce types and remember
442
445
arrays?
443
446
444
- Just like TOP, which over-arches and matches the entire
445
- string, actions have a TOP method as well. We can "make" all of the
446
- individual match components, like "data" or "subject" or "command", and then we
447
- can place them in a data structure that we will "make" in
448
- TOP. When we return the final match object, we can
449
- then access this data structure.
447
+ Just like C < TOP > , which overarches and matches the entire string, actions
448
+ have a TOP method as well. We can C < make > all of the individual match
449
+ components, like C < data > or C < subject > or C < command > , and then we can
450
+ place them in a data structure that we will C < make > in TOP. When we return
451
+ the final match object, we can then access this data structure.
450
452
451
- To do this, we add the method TOP to the action class and " make" whatever data
452
- structure we like from the component pieces.
453
+ To do this, we add the method C < TOP > to the action class and C < make >
454
+ whatever data structure we like from the component pieces.
453
455
454
456
So, our action class becomes:
455
457
@@ -466,17 +468,17 @@ So, our action class becomes:
466
468
}
467
469
= end code
468
470
469
- Here in the TOP method, the " subject" remains the same as the subject we
470
- matched in the grammar. Also, " command" returns the valid <sym> that was
471
- matched (create, update, retrieve, or delete). We coerce each into .Str, as
471
+ Here in the C < TOP > method, the C < subject > remains the same as the subject we
472
+ matched in the grammar. Also, C < command > returns the valid C « <sym> » that was
473
+ matched (create, update, retrieve, or delete). We coerce each into C < .Str > , as
472
474
well, since we don't need the full match object.
473
475
474
- We want to make sure to use the " made" method on the
475
- $<data> object, since we want to access the split one that we " made" with
476
- "make" in our action, rather than the proper $<data> object.
476
+ We want to make sure to use the C < made > method on the C « $<data> » object,
477
+ since we want to access the split one that we C < made > with C < make > in our
478
+ action, rather than the proper C « $<data> » object.
477
479
478
- After we " make" something in the TOP method of a grammar action, we can then
479
- access all the custom values by calling the " made" method on the grammar
480
+ After we C < make > something in the C < TOP > method of a grammar action, we can then
481
+ access all the custom values by calling the C < made > method on the grammar
480
482
result object. The code now becomes
481
483
482
484
= begin code :skip-test
@@ -491,7 +493,7 @@ result object. The code now becomes
491
493
= end code
492
494
493
495
If the complete return match object is not needed, you could return only the made
494
- data from your action's TOP.
496
+ data from your action's C < TOP > .
495
497
496
498
= begin code :skip-test
497
499
my $uri = '/product/update/7/notify';
@@ -504,8 +506,8 @@ data from your action's TOP.
504
506
= end code
505
507
506
508
Oh, did we forget to get rid of that ugly array element number? Hmm. Let's make
507
- something new in the grammar's custom return in TOP... how about we call it
508
- " subject-id" and have it set to element 0 of <data>.
509
+ something new in the grammar's custom return in C < TOP > ... how about we call it
510
+ C < subject-id > and have it set to element 0 of C « <data> » .
509
511
510
512
= begin code
511
513
class REST-actions
@@ -521,7 +523,7 @@ something new in the grammar's custom return in TOP... how about we call it
521
523
}
522
524
= end code
523
525
524
- Now we can do this instead
526
+ Now we can do this instead:
525
527
526
528
= begin code :skip-test
527
529
my $uri = '/product/update/7/notify';
0 commit comments