|
| 1 | +=begin pod |
| 2 | +
|
| 3 | +=TITLE FAQ |
| 4 | +
|
| 5 | +=SUBTITLE Frequently Asked Questions about Perl 6 |
| 6 | +
|
| 7 | +=head1 Language Features |
| 8 | +
|
| 9 | +=head2 What is C<so>? |
| 10 | +
|
| 11 | +C<so> is a loose precedence operator that coerces to L<Bool|/type/Bool>. |
| 12 | +
|
| 13 | +It has the same semantics as the C<?> prefix operator, just like |
| 14 | +C<and> is the low-precedence version of C<&&>. |
| 15 | +
|
| 16 | +Example usage: |
| 17 | +
|
| 18 | + say so 1|2 == 2; # Bool::True |
| 19 | +
|
| 20 | +In this example, the result of the comparison (which is a |
| 21 | +L<Junction|/type/Junction>), is |
| 22 | +converted to Bool before being printed. |
| 23 | +
|
| 24 | +
|
| 25 | +=head2 How can I extract the values from a Junction? |
| 26 | +
|
| 27 | +If you want to extract the values (eigenstates) from a |
| 28 | +L<Junction|/type/Junction>, you are probably doing something wrong, and |
| 29 | +should be using a L<Set|/type/Set> instead. |
| 30 | +
|
| 31 | +Junctions are meant as matchers, not for doing algebra with them. |
| 32 | +
|
| 33 | +If you want to do it anyway, you can abuse autothreading for that: |
| 34 | +
|
| 35 | + sub eigenstates(Mu $j) { |
| 36 | + my @states; |
| 37 | + -> Any $s { @states.push: $s }.($j); |
| 38 | + @states; |
| 39 | + } |
| 40 | +
|
| 41 | + say eigenstates(1|2|3).join(', '); |
| 42 | + # prints 1, 2, 3 or a permutation thereof |
| 43 | +
|
| 44 | +=head2 If Str is immutable, how does C<s///> work? if Int is immutable, how does C<$i++> work? |
| 45 | +
|
| 46 | +In Perl 6, many basic types are immutable, but the variables holding them are |
| 47 | +not. The C<s///> operator works on a variable, into which it puts a newly |
| 48 | +creates string object. Likewise C<$i++> works on the C<$i> variable, not |
| 49 | +just on the value in it. |
| 50 | +
|
| 51 | +See the documentation on L<containers|/language/containers> for more |
| 52 | +information. |
| 53 | +
|
| 54 | +=head2 What's up with array references and automatic derferencing? Do I still need the C<@> sigil? |
| 55 | +
|
| 56 | +In Perl 6, nearly everything is a reference, so talking about taking |
| 57 | +references doesn't make much sense. Unlike Perl 5, scalar variables |
| 58 | +can also contain arrays directly: |
| 59 | +
|
| 60 | + my @a = 1, 2, 3; |
| 61 | + say @a; # "1 2 3\n" |
| 62 | + say @a.WHAT; # (Array) |
| 63 | +
|
| 64 | + my $scalar = @a; |
| 65 | + say $scalar; # "1 2 3\n" |
| 66 | + say $scalar.WHAT; # (Array) |
| 67 | +
|
| 68 | +The big difference is that arrays inside a scalar act as one value in list |
| 69 | +context, whereas arrays will be happily iterated over. |
| 70 | +
|
| 71 | + my @a = 1, 2, 3; |
| 72 | + my $s = @a; |
| 73 | +
|
| 74 | + for @a { ... } # loop body executed 3 times |
| 75 | + for $s { ... } # loop body executed only once |
| 76 | +
|
| 77 | + my @flat = flat @a, @a; |
| 78 | + say @flat.elems; # 6 |
| 79 | +
|
| 80 | + my @nested = flat $s, $s; |
| 81 | + say @nested.elems; # 2 |
| 82 | +
|
| 83 | +You can force flattening with C<@( ... )> or by calling the C<.list> method |
| 84 | +on an expression, and item context (not flattening) with C<$( ... )> |
| 85 | +or by calling the C<.item> method on an expression. |
| 86 | +
|
| 87 | +
|
| 88 | +=head2 Why sigils? Couldn't you do without them? |
| 89 | +
|
| 90 | +There are several reasons: |
| 91 | +
|
| 92 | +=item they make it easy to interpolate variables into strings |
| 93 | +
|
| 94 | +=item they form micro-namespaces for different variables, thus avoiding name clashes |
| 95 | +
|
| 96 | +=item they allow easy single/plural distinction |
| 97 | +
|
| 98 | +=item many natural languages use mandatory noun markers, so our brains are built to handle it |
| 99 | +
|
| 100 | +
|
| 101 | +=head2 Does Perl 6 have coroutines? What about C<yield>? |
| 102 | +
|
| 103 | +Perl 6 has no C<yield> statement like python does, but it does offer similar |
| 104 | +functionality through lazy lists. There are two popular ways to write |
| 105 | +routines that return lazy lists: |
| 106 | +
|
| 107 | + # first method, gather/take |
| 108 | + my @values = gather while have_data() { |
| 109 | + # do some computations |
| 110 | + take some_data(); |
| 111 | + # do more computations |
| 112 | + } |
| 113 | +
|
| 114 | + # second method, use .map or similar method |
| 115 | + # on a lazy list |
| 116 | + my @squares = (1..*).map(-> $x { $x * $x }); |
| 117 | +
|
| 118 | +=head2 Why can't I initialize private attributes from the new method, and how can I fix this? |
| 119 | +
|
| 120 | +A code like |
| 121 | +
|
| 122 | + class A { |
| 123 | + has $!x; |
| 124 | + method show-x { |
| 125 | + say $!x; |
| 126 | + } |
| 127 | + } |
| 128 | + A.new(x => 5).show-x; |
| 129 | +
|
| 130 | +does not print 5. Private attributes are I<private>, which means invisible to |
| 131 | +the outside. If the default constructor could initialize them, they would leak |
| 132 | +into the public API. |
| 133 | +
|
| 134 | +If you still want it to work, you can add a C<submethod BUILD> that |
| 135 | +initializes them: |
| 136 | +
|
| 137 | + class B { |
| 138 | + has $!x; |
| 139 | + submethod BUILD(:$!x) { } |
| 140 | + method show-x { |
| 141 | + say $!x; |
| 142 | + } |
| 143 | + } |
| 144 | + A.new(x => 5).show-x; |
| 145 | +
|
| 146 | +C<BUILD> is called by the default constructor (indirectly, see |
| 147 | +L<Object Construction|/language/objects#Object_Construction> |
| 148 | +for more details) with all the named arguments that the user passes to the |
| 149 | +constructor. C<:$!x> is a named parameter with name C<x>, and when called |
| 150 | +with a named argument of name C<x>, its value is bound to the attribute C<$!x>. |
| 151 | +
|
| 152 | +=head2 How and why do C<say> and C<print> differ? |
| 153 | +
|
| 154 | +The most obvious difference is that C<say> appends a newline at the |
| 155 | +end of the output, and C<print> does not. |
| 156 | +
|
| 157 | +But there is another difference: C<print> converts its arguments to |
| 158 | +a string by calling the C<Str> method on each item passed to, C<say> |
| 159 | +uses the C<gist> method instead. The former is meant for computers, |
| 160 | +the latter for human interpretation. |
| 161 | +
|
| 162 | +Or phrased differently, C<$obj.Str> gives a string representation, |
| 163 | +>$obj.gist> a short summary of that object suitable for fast recognition |
| 164 | +by the programmer, and C<$obj.perl> gives a Perlish representation. |
| 165 | +
|
| 166 | +For example type objects, also known as "undefined values", stringify |
| 167 | +to an empty string and warn, whereas the C<gist> method returns the name |
| 168 | +of the type, followed by an empty pair of parenthesis (to indicate there's |
| 169 | +nothing in that value except the type). |
| 170 | +
|
| 171 | + my Date $x; # $x now contains the Date type object |
| 172 | + print $x; # empty string plus warning |
| 173 | + say $x; # (Date)\n |
| 174 | +
|
| 175 | +So C<say> is optimized for debugging and display to people, C<print> |
| 176 | +is more suitable for producing output for other programs to consume. |
| 177 | +
|
| 178 | +=head2 What's the difference between C<token> and C<rule> ? |
| 179 | +
|
| 180 | +C<regex>, C<token> and C<rule> all three introduce regexes, but with |
| 181 | +slightly different semantics. |
| 182 | +
|
| 183 | +C<token> implies the C<:ratchet> or C<:r> modifier, which prevents the |
| 184 | +rule from backtracking. |
| 185 | +
|
| 186 | +C<rule> implies both the C<:ratchet> and C<:sigspace> (short C<:s>) |
| 187 | +modifer, which means a rule doesn't backtrace, and it treats |
| 188 | +whitespace in the text of the regex as C«<.ws>» calls (ie |
| 189 | +matches whitespace, which is optional except between two word |
| 190 | +characters). Whitespace at the start of the regex and at the start |
| 191 | +of each branch of an alternation is ignored. |
| 192 | +
|
| 193 | +C<regex> declares a plain regex without any implied modifiers. |
| 194 | +
|
| 195 | +=head2 What's the difference between C<die> and C<fail>? |
| 196 | +
|
| 197 | +C<die> throws an exception. |
| 198 | +
|
| 199 | +If C<use fatal;> (which is dynamically scoped) is in scope, C<fail> also |
| 200 | +throws an exception. Otherwise it returns a C<Failure> from the routine |
| 201 | +it is called from. |
| 202 | +
|
| 203 | +A C<Failure> is an "unthrown" or "soft" exception. It is an object that |
| 204 | +contains the exception, and throws the exception when the Failure is used |
| 205 | +as an ordinary object. |
| 206 | +
|
| 207 | +A Failure returns False from a C<defined> check, and you can exctract |
| 208 | +the exception with the C<exception> method. |
| 209 | +
|
| 210 | +=head2 Why is C<wantarray> or C<want> gone? Can I return different things in different contexts? |
| 211 | +
|
| 212 | +Perl has the C<wantarray> function that tells you whether it is called in |
| 213 | +void, scalar or list context. Perl 6 has no equivalent construct, |
| 214 | +because context does not flow inwards, i.e. a routine cannot know which |
| 215 | +context it is called in. |
| 216 | +
|
| 217 | +One reason is that Perl 6 has multi dispatch, and in a code example like |
| 218 | +
|
| 219 | + multi w(Int $x) { say 'Int' } |
| 220 | + multi w(Str $x) { say 'Str' } |
| 221 | + w(f()); |
| 222 | +
|
| 223 | +there is no way to determine if the caller of sub C<f> wants a string or |
| 224 | +an integer, because it is not yet known what the caller is. In general |
| 225 | +this requires solving the halting problem, which even Perl 6 compiler |
| 226 | +writers have trouble with. |
| 227 | +
|
| 228 | +The way to achieve context sensitivity in Perl 6 is to return an object |
| 229 | +that knows how to respond to method calls that are typical for a context. |
| 230 | +
|
| 231 | +For example regex matches return L<Match objects that know how to respond |
| 232 | +to list indexing, hash indexing, and that can turn into the matched |
| 233 | +string|/type/Match>. |
| 234 | +
|
| 235 | +=head1 Meta Questions and Advocacy |
| 236 | +
|
| 237 | +=head2 When will Perl 6 be ready? Is it ready now? |
| 238 | +
|
| 239 | +Readiness of programming languages and their compilers is not a binary |
| 240 | +decision. As they (both the language and the implementations) evolve, they |
| 241 | +grow steadily more usable. Depending on your demands on a programming |
| 242 | +language, Perl 6 and its compilers might or might not be ready for you. |
| 243 | +
|
| 244 | +Please see the L<feature comparison |
| 245 | +matrix|http://perl6.org/compilers/features> for an overview of implemented |
| 246 | +features. |
| 247 | +
|
| 248 | +
|
| 249 | +=head2 Why should I learn Perl 6? What's so great about it? |
| 250 | +
|
| 251 | +Perl 6 unifies many great ideas that aren't usually found in other programming |
| 252 | +languages. While several other languages offer some of these features, none of |
| 253 | +them offer all. |
| 254 | +
|
| 255 | +Unlike most languages, it offers |
| 256 | +
|
| 257 | +=item cleaned up regular expressions |
| 258 | +
|
| 259 | +=item [PEG](http://en.wikipedia.org/wiki/Parsing_expression_grammar) like grammars for parsing |
| 260 | +
|
| 261 | +=item lazy lists |
| 262 | +
|
| 263 | +=item a powerful meta object system |
| 264 | +
|
| 265 | +=item junctions of values |
| 266 | +
|
| 267 | +=item easy access to higher-order functional features like partial application and currying |
| 268 | +
|
| 269 | +=item separate mechanism for subtyping (inheritance) and code reuse (role application) |
| 270 | +
|
| 271 | +=item optional type annotations |
| 272 | +
|
| 273 | +=item powerful run-time multi dispatch for both subroutines and methods based on |
| 274 | + arity, types and additional code constraints |
| 275 | +
|
| 276 | +=item lexical imports |
| 277 | +
|
| 278 | +It also offers |
| 279 | +
|
| 280 | +=item closures |
| 281 | +
|
| 282 | +=item anonymous types |
| 283 | +
|
| 284 | +=item roles and traits |
| 285 | +
|
| 286 | +=item named arguments |
| 287 | +
|
| 288 | +=item nested signatures |
| 289 | +
|
| 290 | +=item object unpacking in signatures |
| 291 | +
|
| 292 | +=item intuitive, nice syntax (unlike Lisp) |
| 293 | +
|
| 294 | +=item easy to understand, explicit scoping rules (unlike Python) |
| 295 | +
|
| 296 | +=item a strong meta object system that does not rely on eval (unlike Ruby) |
| 297 | +
|
| 298 | +=item expressive routine signatures (unlike Perl 5) |
| 299 | +
|
| 300 | +=item state variables |
| 301 | +
|
| 302 | +=item named regexes for easy reuse |
| 303 | +
|
| 304 | +=item unlike many dynamic languages, calls to missing subroutines are caught |
| 305 | + at compile time, and in some cases even signature mismatches can be |
| 306 | + caught at compile time. |
| 307 | +
|
| 308 | +Please see the L<feature comparison |
| 309 | +matrix|http://perl6.org/compilers/features> for an overview of implemented |
| 310 | +features. |
| 311 | +
|
| 312 | +=head2 Is there a CPAN for Perl 6? Or will Perl 6 use the Perl 5 CPAN? |
| 313 | +
|
| 314 | +There isn't yet a module repository for Perl 6 as sophisticated as CPAN. |
| 315 | +But L<modules.perl6.org|http://modules.perl6.org/> has a ist of known |
| 316 | +Perl 6 modules, and L<panda|https://github.com/tadzik/panda/> can install |
| 317 | +those that work with L<rakudo|http://rakudo.org/>. |
| 318 | +
|
| 319 | +Suppport for installing Perl 6 modules from the Perl 5 CPAN is on its way. |
| 320 | +
|
| 321 | +=end pod |
0 commit comments