Skip to content

Commit

Permalink
Update Language/faq.pod6
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun committed Jan 19, 2018
1 parent d82dc3c commit ec806e6
Showing 1 changed file with 86 additions and 74 deletions.
160 changes: 86 additions & 74 deletions doc/Language/faq.pod6
Expand Up @@ -96,7 +96,7 @@ logs|https://www.google.com/search?q=site:irclog.perlgeek.de+inurl:perl6>.
=head2 What is the Perl 6 specification?
The specification refers to the official test suite for Perl 6. It's called
roast and is L<hosted on github|https://github.com/perl6/roast>. Any compiler
L<roast|https://github.com/perl6/roast> and is hosted on github. Any compiler
that passes the tests is deemed to implement that version of the Perl 6
specification.
Expand All @@ -116,33 +116,36 @@ Yes, see L<glossary|/language/glossary>.
=head2 I'm a Perl 5 programmer. Where is a list of differences between Perl 5 and Perl 6?
There are several C<5to6-> guides in the L<Language section of the
There are several B<5to6-> guides in the L<Language section of the
documentation|https://docs.perl6.org/language.html>, most notable of which
is the L<5to6-nutshell guide|https://docs.perl6.org/language/5to6-nutshell>.
is the L<5to6-nutshell|https://docs.perl6.org/language/5to6-nutshell> guide.
=head2 I'm a Ruby programmer looking for quickstart type docs?
X<|Ruby (FAQ)>
See the 'rb-nutshell' pod under L<https://docs.perl6.org/language/rb-nutshell>
See the L<rb-nutshell|https://docs.perl6.org/language/rb-nutshell> guide.
=head1 Modules
=head2 Is there a CPAN (repository of third party library modules) for Perl 6?
X<|CPAN (FAQ)>X<|ecosystem>
Yes, it's the same L<CPAN|http://cpan.org/> as for Perl 5! The only difference is
when using L<PAUSE|http://pause.perl.org/> to upload the module, you'd select
Yes, it's the same L<CPAN|http://cpan.org/> as for Perl 5! The only difference
is when using L<PAUSE|http://pause.perl.org/> to upload the module, you'd select
C<Perl 6> as the target directory. The
L«C<App::Mi6> tool|https://modules.perl6.org/l/App::Mi6» can simplify the uploading
process. Latest versions of L«C<zef> module installer|https://modules.perl6.org/l/zef»
L«C<App::Mi6> tool|https://modules.perl6.org/l/App::Mi6» can simplify the
uploading process. Latest versions of
L«C<zef> module installer|https://modules.perl6.org/l/zef»
automatically check for latest versions of a module on CPAN as well as our
L<GitHub-based ecosystem|https://github.com/perl6/ecosystem/>.
Viewing of modules available CPAN will be implemented soon on
L<modules.perl6.org website|https://modules.perl6.org/> (temporarily, it's
available as L<a raw JSON file|https://github.com/ugexe/Perl6-ecosystems/blob/master/cpan.json>
available as a
L<raw JSON file|https://github.com/ugexe/Perl6-ecosystems/blob/master/cpan.json>
=head2 Is there a perldoc (command line documentation viewer) for Perl 6?
X<|p6doc (FAQ)>
Yes, it's called p6doc and is present in the ecosystem under that name. It's
present in Rakudo Star but you will need to install it using the monthly
Expand All @@ -157,12 +160,13 @@ well with most Perl 5 modules. It can even run Perl 5 Catalyst and DBI.
=head2 Can I use C and C++ from Perl 6?
X<|C and C++ (FAQ)>
L<Nativecall|https://docs.perl6.org/language/nativecall> makes this particularly easy.
L<Nativecall|https://docs.perl6.org/language/nativecall> makes this
particularly easy.
=head2 Nativecall can't find libfoo.so and I only have libfoo.so.1.2!
This is commonly seen on Debian-like systems. You need to install C<libfoo-dev> package,
to set a sym link for the missing file.
This is commonly seen on Debian-like systems. You need to install C<libfoo-dev>
package, to set a sym link for the missing file.
=head2 Where have all the traditional UNIX library functions gone?
X<|UNIX library functions (FAQ)>
Expand All @@ -183,16 +187,17 @@ basic modules|https://docs.perl6.org/language/modules-core>.
Many more modules can be found in the L<ecosystem|http://modules.perl6.org/>.
=head2 Is there something like B::Deparse/How can I get hold of the AST?
=head2 Is there something like C<B::Deparse>/How can I get hold of the AST?
Use C<--target=optimize> command line option to view the AST of your program; e.g.:
C<perl6 --target=optimize -e 'say "hi"'>
Use C<--target=optimize> command line option to view the AST of your program,
e.g., C<perl6 --target=optimize -e 'say "hi"'>
The C<optimize> target gives the AST after the static optimizer did its job, while
target C<ast> gives the AST before that step. To get the full list of available targets,
run C<perl6 --stagestats -e "">
The target C<optimize> gives the AST after the static optimizer did its job,
while target C<ast> gives the AST before that step. To get the full list of
available targets, run C<perl6 --stagestats -e "">
=head2 What is Precompilation?
X<|Precompile (FAQ)>
When you load a module for the first time, Rakudo compiles it into bytecode,
and both stores it on disk, and uses the compiled bytecode. On subsequent
Expand All @@ -201,17 +206,18 @@ significantly faster.
=head1 Language Features
=head2 How can I dump Perl 6 data structures (like Perl 5 Data::Dumper and similar)?
=head2 How can I dump Perl 6 data structures
(like Perl 5 Data::Dumper and similar)?
X<|Data::Dumper (FAQ)>
Typical options are to use L<say> routine that uses L<gist> method that
gives the "gist" of the object being dumped. More detailed output can be
obtained by calling L<perl> method that typically returns representation
in L<EVAL|/routine/EVAL>-able code.
If you're using the L<rakudo implementation|http://rakudo.org>, you can use
the L«non-standard C<dd> routine|/programs/01-debugging#Dumper_function_dd»
for dumping, whose output is similar to L<perl>, but with more information.
If you're using the L<rakudo|http://rakudo.org> implementation, you can use
the L«rakudo-specific C<dd> routine|/programs/01-debugging#Dumper_function_dd»
for dumping, whose output is similar to L<.perl>, but with more information.
Examples:
Expand Down Expand Up @@ -255,11 +261,12 @@ Example:
=head2 What is C<(Any)>?
L<Any|/type/Any> is a top level class most objects inherit from. The L<Any> type object is L<the
default value|/type/Attribute#index-entry-trait_is_default_%28Attribute%29-Trait_is_default>
on variables and parameters without an explicit type constraint, which means you'll
likely see C<(Any)> printed when you output a L<gist> of a variable without any value,
such as using L«C<say> routine|/routine/say»:
L«C<Any>|/type/Any» is a top level class most objects inherit from.
The C<Any> type object is
L<the default value|/type/Attribute.html#Trait_is_default> on variables and
parameters without an explicit type constraint, which means you'll
likely see C<(Any)> printed when you output a L<gist|/routine/gist> of
a variable without any value, such as using L«C<say> routine|/routine/say»:
=begin code
my $foo;
Expand All @@ -272,11 +279,12 @@ my $bar = 70;
say $bar; # OUTPUT: «70␤»
=end code
To test whether a variable has any defined values, see L<DEFINITE> and L<defined>
To test whether a variable has any defined values, see
L<DEFINITE|/classtut#index-entry-DEFINITE> and L<defined>
routines. Several other constructs exist that test for definiteness, such as
L«C<with>, C<orwith>, and C<without>|/syntax/with%20orwith%20without» statements,
L«C<//>|/routine/$SOLIDUS$SOLIDUS», L<andthen>, L<notandthen>, and L<orelse> operators,
as well as L<type constraint smileys|/type/Signature#Constraining_Defined_and_Undefined_Values>.
L«C<with>, C<orwith>, and C<without>|/syntax/with%20orwith%20without»
statements, L«C<//>|/routine/$SOLIDUS$SOLIDUS», L<andthen>, L<notandthen>, and
L<orelse> operators, as well as L<type constraint smileys|/type/Signature#Constraining_Defined_and_Undefined_Values>.
=head2 What is C<so>?
Expand All @@ -290,8 +298,7 @@ Example usage:
say so 1|2 == 2; # OUTPUT: «True␤»
In this example, the result of the comparison (which is a
L<Junction|/type/Junction>), is
converted to Bool before being printed.
L<Junction|/type/Junction>), is converted to Bool before being printed.
=head2 What are those C<:D> and C<:U> things in signatures?
Expand All @@ -305,38 +312,39 @@ For example, if you declare a variable
then not only can you assign integers (that is, instances of class Int) to it,
but the C<Int> type object itself:
=for code :preamble<my Int $x>
$x = Int
=begin code :preamble<my Int $x>
$x = Int
=end code
If you want to exclude type objects, you can append the C<:D> type smiley,
which stands for "definite":
=begin code
my Int:D $x = 42;
$x = Int; # dies with:
# Type check failed in assignment to $x;
# expected Int:D but got Int
$x = Int;
# dies with:
# Type check failed in assignment to $x;
# expected Int:D but got Int
=end code
Likewise, C<:U> constrains to undefined values, that is, type objects.
To explicitly allow either type objects or instances, you can use C<:_>.
=head2 What is the C<< --> >> thing in the signature?
=head2 What is the C«-->» thing in the signature?
L«-->|/type/Signature#Constraining_Return_Types» is a return constraint, either
a type or a definite value.
Example of a type constraint:
=begin code
sub divide-to-int( Int $a, Int $b --> Int ) {
return ($a / $b).narrow;
}
divide-to-int(3, 2)
# Type check failed for return value; expected Int but got Rat
=end code
Example of a definite return value:
Expand Down Expand Up @@ -366,24 +374,29 @@ If you want to do it anyway, you can abuse autothreading for that:
say eigenstates(1|2|3).join(', ');
# prints 1, 2, 3 or a permutation thereof
=head2 If Str is immutable, how does C<s///> work? If Int is immutable, how does C<$i++> work?
=head2 If Str is immutable, how does C<s///> work? If Int is immutable,
how does C<$i++> work?
In Perl 6, values of many basic types are immutable, but the variables holding them are
not. The C<s///> operator works on a variable, into which it puts a newly
created string object. Likewise, C<$i++> works on the C<$i> variable, not
In Perl 6, values of many basic types are immutable, but the variables holding
them are not. The C<s///> operator works on a variable, into which it puts a
newly created string object. Likewise, C<$i++> works on the C<$i> variable, not
just on the value in it.
Knowing this, you would not try to change a literal string (e.g. like
C<'hello' ~~ s/h/H/;>), but you might accidentally do
something equivalent using `map`:
C<'hello' ~~ s/h/H/;>), but you might accidentally do something equivalent
using C<map> as follows.
my @foo = <hello world>.map: { s/h/H/ };
my @foo = <hello world>.map: { s/h/H/ }; # dies with
# Cannot modify an immutable Str (hello)
# dies with
# Cannot modify an immutable Str (hello)
my @bar = <hello world>».subst-mutate: 'h', 'H'; # dies with
# Cannot resolve caller subst-mutate(Str: Str, Str);
# the following candidates match the type but require
# mutable arguments: ...
my @bar = <hello world>».subst-mutate: 'h', 'H';
# dies with
# Cannot resolve caller subst-mutate(Str: Str, Str);
# the following candidates match the type but require
# mutable arguments: ...
Instead of modifying the original value in place, use a routine or operator
that returns a new value:
Expand All @@ -394,7 +407,8 @@ that returns a new value:
See the documentation on L<containers|/language/containers> for more
information.
=head2 What's up with array references and automatic dereferencing? Do I need the C<@> sigil?
=head2 What's up with array references and automatic dereferencing?
Do I need the C<@> sigil?
In Perl 6, nearly everything is a reference, so talking about taking
references doesn't make much sense. Scalar variables can also contain
Expand All @@ -411,7 +425,6 @@ arrays directly:
The big difference is that arrays inside a scalar act as one value in list
context, whereas arrays will be happily iterated over.
=begin code
my @a = 1, 2, 3;
my $s = @a;
Expand All @@ -423,7 +436,6 @@ context, whereas arrays will be happily iterated over.
my @nested = flat $s, $s;
say @nested.elems; # OUTPUT: «2␤»
=end code
You can force flattening with C<@( ... )> or by calling the C<.list> method
on an expression, and item context (not flattening) with C<$( ... )>
Expand All @@ -449,10 +461,8 @@ There are several reasons:
You likely tried to mix string interpolation and key characters, like HTML tags:
=begin code
my $foo = "abc";
say "$foo<html-tag>";
=end code
Perl 6 thinks C<$foo> to be a Hash and C«<html-tag>» to be a string literal
hash key. Use a closure to help it to understand you.
Expand Down Expand Up @@ -480,7 +490,8 @@ routines that return lazy lists:
my @squares = (1..*).map(-> \x { x² });
=end code
=head2 Why can't I initialize private attributes from the new method, and how can I fix this?
=head2 Why can't I initialize private attributes from the new method,
and how can I fix this?
Code like
Expand Down Expand Up @@ -541,9 +552,9 @@ nothing in that value except the type).
print $x; # empty string plus warning
say $x; # OUTPUT: «(Date)␤»
So, C<say> is optimized for debugging; display is optimized for people; and C<print>
and C<put> are most suitable for producing output for other programs
to consume.
So, C<say> is optimized for debugging; display is optimized for people;
and C<print> and C<put> are most suitable for producing output for other
programs to consume.
C<put> is thus sort of a hybrid between C<print> and C<say>;
like C<print>, its output is suitable for other programs,
Expand Down Expand Up @@ -573,20 +584,21 @@ C<die> throws an exception.
C<fail> returns a C<Failure> object. (If the caller has declared C<use fatal;>
in the calling lexical scope, C<fail> throws an exception instead of returning.)
A C<Failure> is an "unthrown" or "lazy" exception. It's an object that
contains the exception, and throws the exception if you try to use the C<Failure>
A C<Failure> is an "unthrown" or "lazy" exception. It's an object that contains
the exception, and throws the exception if you try to use the C<Failure>
as an ordinary object, or ignore it in sink context.
A C<Failure> returns C<False> from a C<defined> check, and you can extract
the exception with the C<exception> method.
=head2 Why is C<wantarray> or C<want> gone? Can I return different things in different contexts?
=head2 Why is C<wantarray> or C<want> gone?
Can I return different things in different contexts?
Perl 5 has the L<C<wantarray>|/language/5to6-perlfunc#wantarray> function that
tells you whether it's called in void, scalar or list context. Perl 6 has no
equivalent construct because context does not flow inwards; i.e., a routine would need
time travel to know which context it's called in because context is lazy (known
only when the results are used later).
equivalent construct because context does not flow inwards; i.e.,
a routine would need time travel to know which context it's called in because
context is lazy (known only when the results are used later).
For example, Perl 6 has multiple dispatch. So, in a code example like
Expand Down Expand Up @@ -765,17 +777,17 @@ them offer all.
=item Perl 6's mottos remain the same as they have been for Perl all along: “Perl is different. In a nutshell, Perl is designed to make the easy jobs easy, without making the hard jobs impossible.” and “There Is More Than One Way To Do It”. Now with even more -Ofun added.
Please see the L<feature comparison
matrix|https://perl6.org/compilers/features> for an overview of implemented
features.
Please see the
L<feature comparison matrix|https://perl6.org/compilers/features>
for an overview of implemented features.
=head2 Is Perl 6 fast enough for me?
That depends on what you are doing. Rakudo has been developed with the
philosophy of "make it work right then make it work fast". It's fast for some
things already but needs work for others.
Since Perl 6 provides lots of clues to the JIT that other dynamic languages don't, we think
we'll have a lot of headroom for performance improvements.
things already but needs work for others. Since Perl 6 provides lots of clues
to the JIT that other dynamic languages don't, we think we'll have a lot of
headroom for performance improvements.
The following crude benchmarks, with all the usual caveats about such things,
show that Perl 6 can be faster than Perl 5 for similar tasks if
Expand Down

0 comments on commit ec806e6

Please sign in to comment.