Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Cleanup, mostly trailing whitespace

  • Loading branch information...
commit d1be5ad071d789d4331edc8d6e8fa3a1109576f0 1 parent 86ca520
Jan Ingvoldstad authored

Showing 1 changed file with 74 additions and 59 deletions. Show diff stats Hide diff stats

  1. 133  cookbook/01strings/01-00introduction.pl
133  cookbook/01strings/01-00introduction.pl
@@ -22,7 +22,7 @@ =head1 Description
22 22
 of zero or more characters, including letters, spaces, numbers, and
23 23
 other characters.
24 24
 
25  
-=head1 Declaring and assigning Strings 
  25
+=head1 Declaring and assigning Strings
26 26
 
27 27
 A string can be created explictly, by declaring a variable using the
28 28
 Str keyword, and assigning a string value to it.
@@ -47,32 +47,35 @@ =head1 Displaying Strings
47 47
 We display strings using print or say:
48 48
 
49 49
     print ""        ; # output is an empty string
50  
-    print "Hello\n" ; # output is Hello followed by a new line  
  50
+    print "Hello\n" ; # output is Hello followed by a new line
51 51
     say   "Hello"   ; # same
52  
-    say   'Hello'   ; # same 
  52
+    say   'Hello'   ; # same
53 53
 
54 54
 =end pod
55 55
 
56 56
 print ""        ; # output is an empty string
57  
-print "Hello\n" ; # output string is Hello followed by a new line  
  57
+print "Hello\n" ; # output string is Hello followed by a new line
58 58
 say   "Hello"   ; # same
59  
-say   'Hello'   ; # same 
  59
+say   'Hello'   ; # same
60 60
 
61 61
 =begin pod
62 62
 
63  
-Both print and say accept a list of things to display:
  63
+Both print and say also accept a list of things to display, and will
  64
+attempt to join all the things into a string:
64 65
 
65  
-    say "Hello", "World", "!";
  66
+    say "Hello", "World", "!"; # Hello World!
66 67
 
67 68
 =end pod
68 69
 
69  
-say "Hello", "World", "!";
  70
+say "Hello", "World", "!"; # Hello World!
70 71
 
71 72
 =begin pod
72 73
 
73 74
 Strings can be appended to one another, using the concatenation
74 75
 operator, ~
75 76
 
  77
+Beware when using print and say; concatenation is slower than joining.
  78
+
76 79
 Here, three strings are concatenated into a single string.  Output is
77 80
 Hello World! followed by a newline.
78 81
 
@@ -91,71 +94,81 @@ =head1 Displaying Strings
91 94
 =head1 Introspection
92 95
 
93 96
 Perl 6 has extensive support for introspection, that is, to see the
94  
-internals of how things are.  It is therefore possible to find out if
95  
-something is a string and act upon that information.
  97
+internals of types and objects during runtime.  It is therefore
  98
+possible to find out if a variable is a string and act upon that
  99
+information.
  100
+
  101
+We know we get a String when we declare something explicitly as Str:
96 102
 
97 103
     my Str $string = 'This is $string: a scalar holding a String';
98 104
     say $string;
99  
-    say '$string is ', $string.^name;                 # Str 
  105
+    say '$string is ', $string.^name;                 # Str
  106
+
  107
+We can also easily show that a general variable containing a String,
  108
+is in fact just that:
100 109
 
101 110
     my $scalar = 'This is $scalar holding a String';
102  
-    say $scalar; 
103  
-    say '$scalar is ', $scalar.^name;                 # Str 
  111
+    say $scalar;
  112
+    say '$scalar is ', $scalar.^name;                 # Str
104 113
 
105 114
 =end pod
106 115
 
107 116
 my Str $string = 'This is $string: a scalar holding a String';
108 117
 say $string;
109  
-say '$string is ', $string.^name;                 # Str 
  118
+say '$string is ', $string.^name;                 # Str
110 119
 
111 120
 my $scalar = 'This is $scalar holding a String';
112  
-say $scalar; 
113  
-say '$scalar is ', $scalar.^name;                 # Str 
  121
+say $scalar;
  122
+say '$scalar is ', $scalar.^name;                 # Str
114 123
 
115 124
 =begin pod
116 125
 
117 126
 =head1 Numbers as strings
118 127
 
119  
-A number might be interpreted as a string, depending on the context
  128
+A number may be interpreted as a string, depending on the context:
120 129
 
121 130
     say 1; # 1 is interpreted as a number
122  
-    say (1+1), " is a number interpreted as a string" ; # 2 is a number interpreted as a string. 
  131
+    say 2, " is a number interpreted as a string"; # 2 is a number interpreted as a string.
  132
+    say 1+2*3, " is a number interpreted as a string"; # 7 is a number interpreted as a string.
123 133
 
124  
-Note the parentheses around (1+1), which cause 1 + 1 to be evaluated
125  
-as a numeric expression, before the resulting "2" is evaluated as a
126  
-string. 
  134
+Note that Perl 6 ensures that the arithmetic expression before the
  135
+first comma is evaluated even without enclosing parentheses, and that
  136
+it is only afterwards that it is interpreted as a string.
127 137
 
128 138
 =end pod
129 139
 
130  
-say 1; # 1 is a number 
131  
-say (1+1), " is a number interpreted as a string" ; 
  140
+say 1; # 1 is a number
  141
+say 2, " is a number interpreted as a string"; # 2 is a number interpreted as a string.
  142
+say 1+2*3, " is a number interpreted as a string"; # 7 is a number interpreted as a string.
132 143
 
133 144
 =begin pod
134 145
 
  146
+=head1 Strings as numbers
  147
+
135 148
 Conversely, sometimes a string might be interpreted as a number:
136 149
 
137  
-    print  +""     ; # a num-ified empty string evaluates as 0 
138  
-    print  "1" + 1 ; # 2 
  150
+    print  +""     ; # a num-ified empty string evaluates as 0
  151
+    print  "1" + 1 ; # 2
139 152
 
140 153
 The string, "1" is treated as a number in this context, added to the
141  
-number 1 by the + operator, which returns the number, 2, as a 
142  
-string for output.  
  154
+number 1 by the + operator, which returns the number, 2, as a
  155
+string for output.
143 156
 
144 157
 =end pod
145 158
 
146  
-say   +""     ; # a num-ified empty string evaluates as 0 
147  
-say  "1" + 1  ; # 2 
  159
+say   +""     ; # a num-ified empty string evaluates as 0
  160
+say  "1" + 1  ; # 2
148 161
 
149 162
 =begin pod
150 163
 
151 164
 Context sensitivity is the essence of Perl.  Keeping this in mind, what
152  
-would you expect to be the output string, for the following?  
  165
+would you expect to be the output string, for the following?
153 166
 
154 167
     my $string = "1" ~ "1" + 10; # 12 or 21?
155 168
     say $string;
156 169
 
157 170
 But, "1+1", surrounded by quotation marks, either '' or "", stringifies
158  
-the expression, so that it is evaluated as a string. 
  171
+the expression, so that it is evaluated as a string.
159 172
 
160 173
     say "1 + 1"; # literally: 1 + 1
161 174
 
@@ -165,19 +178,21 @@ =head1 Numbers as strings
165 178
     say eval "1 + 1";    # 2
166 179
 
167 180
 On the command-line, you may pass a string to the perl 6 interpretor,
168  
-to have it evaluated as a program expression, by using the -e switch: 
  181
+to have it evaluated as a program expression, by using the -e switch:
169 182
 
170 183
     ./perl6 -e "say 1+1"; # 2
  184
+    ./perl6 -e 'say "1+1"'; # 1+1
171 185
 
172 186
 =end pod
173 187
 
174 188
 my $string = "1" ~ "1" + 10; # 12 or 21?
175 189
 say $string;
  190
+say "1 + 1"; # literally: 1 + 1
  191
+say eval "1 + 1";    # 2
176 192
 
177  
-=begin pod 
  193
+=begin pod
178 194
 
179  
-Assignments of non-strings set the variable to the
180  
-appropriate type:
  195
+Assignments of non-strings set the variable to the appropriate type:
181 196
 
182 197
     my $scalar = 1234;
183 198
     say $scalar; # 1234
@@ -186,7 +201,7 @@ =head1 Numbers as strings
186 201
 An object can be stringified, by using the ~ operator immediately
187 202
 prior to the variable's sigil
188 203
 
189  
-    say '~$scalar is ', (~$scalar).^name; # ~$scalar is Str 
  204
+    say '~$scalar is ', (~$scalar).^name; # ~$scalar is Str
190 205
 
191 206
 =end pod
192 207
 
@@ -203,8 +218,8 @@ =head2 Single-quoted Strings
203 218
 
204 219
 Strings that are written with single quotes are almost
205 220
 verbatim.  However, backslashes are an escape character.
206  
-This is so that you can write literal single-quotes 
207  
-within a single-quoted string, and also be able to write 
  221
+This is so that you can write literal single-quotes
  222
+within a single-quoted string, and also be able to write
208 223
 a backslash at the end of a single-quote-enclosed string:
209 224
 
210 225
     say 'n\'     ; # Error: perl sees no closing '
@@ -212,40 +227,40 @@ =head2 Single-quoted Strings
212 227
     say 'n\''    ; # n'
213 228
     say 'n\n'    ; # n\n
214 229
     say 'n\\n'   ; # n\n
215  
-    say 'n\\\n'  ; # n\\n better spelled as: 
216  
-    say 'n\\\\n' ; # n\\n  
  230
+    say 'n\\\n'  ; # n\\n better spelled as:
  231
+    say 'n\\\\n' ; # n\\n
217 232
 
218 233
 
219 234
 A few other backslashy escapes work in single quotes too
220 235
 
221 236
 =head2 Double-quoted Strings
222 237
 
223  
-If you want to interpolate variables and other special characters 
  238
+If you want to interpolate variables and other special characters
224 239
 within a literal string, use double quotes around the value:
225 240
 
226 241
     my $var1 = 'dog' ;
227 242
     say "The quick brown fox jumps over the lazy $var1";
228  
-    
  243
+
229 244
 
230 245
 =head2 Interpolation
231 246
 
232 247
 Double-quoted strings interpolate the elements of an array or
233  
-a hash, closures, functions, backslashed control characters, and 
  248
+a hash, closures, functions, backslashed control characters, and
234 249
 other good stuff.  Single-quoted strings do not.
235 250
 
236  
-    # literal whitespace  
  251
+    # literal whitespace
237 252
     my $squot = '    The quick brown fox jumps over the lazy dog.
238 253
     	dog.';
239 254
     my $dquot = "    The quick brown fox jumps over the lazy
240 255
     	dog.";
241 256
     say $squot;
242 257
     say $dquot;
243  
-    
  258
+
244 259
     # Double-quotes interpolate special backslash values,
245 260
     # but single-quotes do not
246 261
     say 'The quick brown fox\n\tjumps over the lazy dog\n';
247 262
     say "The quick brown fox\n\tjumps over the lazy dog\n";
248  
-    
  263
+
249 264
     # interpolate array elements:
250 265
     my @animal = ("fox", "dog");
251 266
     say 'The quick brown @animal[0] jumps over the lazy @animal[1]';
@@ -255,14 +270,14 @@ =head2 Interpolation
255 270
     my %animal = (quick => 'fox', lazy => 'dog');
256 271
     say 'The quick brown %animal{\'quick\'} jumps over the lazy %animal{\'lazy\'}.';
257 272
     say "The quick brown %animal{'quick'} jumps over the lazy %animal{'lazy'}.";
258  
-    
  273
+
259 274
     # interpolate methods, closures, and functions:
260 275
     say '@animal.elems() {@animal.elems} &elems(@animal)';
261 276
     say "@animal.elems() {@animal.elems} &elems(@animal)";
262  
-    
  277
+
263 278
 =end pod
264 279
 
265  
-# literal whitespace  
  280
+# literal whitespace
266 281
 my $squot = '    The quick brown fox jumps over the lazy dog.
267 282
 	dog.';
268 283
 my $dquot = "    The quick brown fox jumps over the lazy
@@ -293,13 +308,13 @@ =head2 Interpolation
293 308
 # interpolate methods, closures, and functions:
294 309
 say '@animal.elems() {@animal.elems} &elems(@animal)';
295 310
 say "@animal.elems() {@animal.elems} &elems(@animal)";
296  
- 
  311
+
297 312
 =begin pod
298 313
 
299 314
 =head2 Perl's Quote-like Operators
300 315
 
301 316
 It's often useful to use something other than single or double quotes
302  
-when declaring strings. To do so use the q// and qq// quote operators, 
  317
+when declaring strings. To do so use the q// and qq// quote operators,
303 318
 which provide advanced interpolation control:
304 319
 
305 320
     # Single quoted strings
@@ -311,7 +326,7 @@ =head2 Perl's Quote-like Operators
311 326
     say q/This string allows "double quotes" seamlessly/;
312 327
 
313 328
 The slashes in q// and qq// can be replaced with most of the
314  
-delimiters that worked in Perl 5. All of Unicode above Latin-1 is reserved 
  329
+delimiters that worked in Perl 5. All of Unicode above Latin-1 is reserved
315 330
 for user-defined quotes.
316 331
 
317 332
     # Single quoted strings
@@ -322,7 +337,7 @@ =head2 Perl's Quote-like Operators
322 337
     say q<Many delimiters are available for quoting>;
323 338
     say q{Many delimiters are available for quoting};
324 339
     say q?Many delimiters are available for quoting?;
325  
-    
  340
+
326 341
     # But not the colon B<:>
327 342
     q:illegal_perl6:; #legal perl 5
328 343
 
@@ -412,10 +427,10 @@ =head2 Special adverbs and synonyms
412 427
 
413 428
 The <> synonym for q:w has many uses
414 429
 
415  
-    @animals           = <fox dog monkey>; 
  430
+    @animals           = <fox dog monkey>;
416 431
     say @animals[0]    ; # fox
417 432
     %animal            = <brown fox lazy dog>;
418  
-    say %animal<lazy>  ; # dog 
  433
+    say %animal<lazy>  ; # dog
419 434
 
420 435
 =item :ww Split on Quoted Words
421 436
 
@@ -423,20 +438,20 @@ =head2 Special adverbs and synonyms
423 438
     # Quoted words and variable interpolation
424 439
     @animals           = qq:ww/"brown $fox" "lazy %animal{'lazy'}"/;
425 440
 
426  
-The «» synonym for qq:ww has many uses, also spelled <<>> 
  441
+The «» synonym for qq:ww has many uses, also spelled <<>>
427 442
 
428 443
     ($fox,$dog)          = «brown lazy»;
429 444
     ($fox,$dog)          = <<brown lazy>>; # same
430 445
     %animal              = « $fox fox lazy "lazy dog" »;
431  
-    say %animal« $dog »  ; # lazy dog 
432  
-    say %animal<<$dog>>    ; # lazy dog 
  446
+    say %animal« $dog »  ; # lazy dog
  447
+    say %animal<<$dog>>    ; # lazy dog
433 448
 
434 449
 =item :x Execute
435 450
 
436 451
 TODO
437 452
 
438 453
 =back
439  
-   
  454
+
440 455
 =item :t Defining Multiline Strings (Here Documents)
441 456
 
442 457
 Multiline strings (here documents) can be defined using the q// and qq//

0 notes on commit d1be5ad

Please sign in to comment.
Something went wrong with that request. Please try again.