You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/grammar_tutorial.pod6
+26-28Lines changed: 26 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,15 +38,15 @@ For example, in the case of HTML, you could define a grammar that would recogniz
38
38
39
39
Grammars are defined as an object, just like everything else in Perl. Technically, they are normal classes with a little extra magic thrown in, which we'll get to later -- and a few limitations. You name and define a grammar exactly as you would a class, except using the "grammar" keyword instead of "class".
40
40
41
-
=begincode
41
+
=begincode :skip-test
42
42
grammar My::Gram { ..methods 'n stuff... }
43
43
=endcode
44
44
45
45
Grammars contain elements like methods, called I<regex, token or rule>. These elements are named, just like methods are named. And each one defines a regex, token or rule (which are mostly the same thing (not really) but we'll get to that later).
46
46
47
47
Once you have your grammar defined, in your program call it by name and pass in the string you want to parse, and that string will be run through the rules you defined based upon your regex, token and rule "methods". When done, you get back a I<Match> object that has been populated with data structured and stored by the names you used to define your methods.
48
48
49
-
=begincode
49
+
=begincode :skip-test
50
50
my $matchObject = My::Gram.parse($what-a-big-string-you-have);
51
51
=endcode
52
52
@@ -110,14 +110,14 @@ But in our big string we get, we don't know what order these regex matches will
110
110
111
111
You could actually use this to extract your data from the URI for basic CRUD that has all 3 parameters included:
112
112
113
-
=begincode
113
+
=begincode :skip-test
114
114
my $match = REST.parse('/product/update/7/notify');
115
115
say $match;
116
116
117
-
「/product/update/7/notify」
118
-
subject => 「product」
119
-
command => 「update」
120
-
data => 「7/notify」
117
+
# 「/product/update/7/notify」
118
+
# subject => 「product」
119
+
# command => 「update」
120
+
# data => 「7/notify」
121
121
=endcode
122
122
123
123
Of course, the data can be accessed directly by using $match<subject> or $match<command> or $match<data> to return the values parsed. They each contain match objects you can work further with, or coerce into a string ( $match<command>.Str )
@@ -155,14 +155,13 @@ Let's imagine, for the sake of demonstration, that we might want to allow these
155
155
my $m = REST.parse('/ product / update /7 /notify');
156
156
say $m;
157
157
158
-
###
159
-
「/ product / update /7 /notify」
160
-
slash => 「/ 」
161
-
subject => 「product」
162
-
slash => 「 / 」
163
-
command => 「update」
164
-
slash => 「 /」
165
-
data => 「7 /notify」
158
+
# 「/ product / update /7 /notify」
159
+
# slash => 「/ 」
160
+
# subject => 「product」
161
+
# slash => 「 / 」
162
+
# command => 「update」
163
+
# slash => 「 /」
164
+
# data => 「7 /notify」
166
165
=endcode
167
166
168
167
We're getting some extra junk in our match object now, with those slashes, but there are some very nice ways to make a tidy return value that we'll get to.
@@ -173,10 +172,10 @@ We want our RESTful grammar to allow for CRUD operations only. Anything else we
173
172
174
173
There are several ways to accomplish this. For example, you could change the command method:
@@ -220,7 +219,7 @@ This is what we've come up for processing our RESTful URIs so far:
220
219
221
220
Let's look at various URI's and how they behave being passed through our grammar.
222
221
223
-
=begincode
222
+
=begincode :skip-test
224
223
my @uris = ['/product/update/7/notify',
225
224
'/product/create',
226
225
'/item/delete/4'];
@@ -230,10 +229,9 @@ Let's look at various URI's and how they behave being passed through our grammar
230
229
say "Sub: $m<subject> Cmd: $m<command> Dat: $m<data>";
231
230
}
232
231
233
-
###
234
-
Sub: product Cmd: update Dat: 7/notify
235
-
Sub: product Cmd: create Dat:
236
-
Sub: item Cmd: delete Dat: 4
232
+
# Sub: product Cmd: update Dat: 7/notify
233
+
# Sub: product Cmd: create Dat:
234
+
# Sub: item Cmd: delete Dat: 4
237
235
=endcode
238
236
239
237
So with just this part of a grammar, we're getting almost everything we need. Our URI's get efficiently parsed and we're given a nice little data structure for the variables we need to work with.
@@ -250,10 +248,10 @@ You can think of grammar actions as a kind of plug-in expansion module for gramm
250
248
251
249
You do this when you first create your grammar. In addition to passing in the actual string you want to parse, you can pass in a named parameter called "actions" which should contain an instance of your actions class. From our example above, if our actions class were called REST-actions we would parse our grammar string like this
252
250
253
-
=begincode
251
+
=begincode :skip-test
254
252
my $matchObj = REST.parse($uri, actions => REST-actions.new);
255
253
256
-
...or if you prefer...
254
+
#…or if you prefer…
257
255
258
256
my $matchObj = REST.parse($uri, :actions(REST-actions.new));
259
257
=endcode
@@ -321,7 +319,7 @@ When we add "make" to our match split (which returns a list), our action will re
321
319
322
320
Now if we want to access just our id of 7 from that long URL, we can access the first element of the list returned from the "data" action we "made" with "make":
323
321
324
-
=begincode
322
+
=begincode :skip-test
325
323
my $uri = '/product/update/7/notify';
326
324
327
325
my $match = REST.parse($uri, actions => REST-actions.new);
@@ -355,7 +353,7 @@ But what we want to be certain to do, is to use the "made" method on our $<data>
355
353
356
354
After we "make" something in the TOP method of a grammar action, we can then access all of our custom-made stuff by calling the "made" method on our grammar result object. From our earlier example, it now becomes
357
355
358
-
=begincode
356
+
=begincode :skip-test
359
357
my $uri = '/product/update/7/notify';
360
358
361
359
my $match = REST.parse($uri, actions => REST-actions.new);
@@ -368,7 +366,7 @@ After we "make" something in the TOP method of a grammar action, we can then acc
368
366
369
367
Of course you could also shorten it if you knew you wouldn't need the full grammar return match object, and only wanted your custom-made data from your action's TOP.
370
368
371
-
=begincode
369
+
=begincode :skip-test
372
370
my $uri = '/product/update/7/notify';
373
371
374
372
my $rest = REST.parse($uri, actions => REST-actions.new).made;
@@ -396,7 +394,7 @@ Oh, did we forget to get rid of that ugly array element number? Hmm. Let's make
396
394
397
395
Now we can do this instead
398
396
399
-
=begincode
397
+
=begincode :skip-test
400
398
my $uri = '/product/update/7/notify';
401
399
402
400
my $rest = REST.parse($uri, actions => REST-actions.new).made;
0 commit comments