Changeset 8235

Show
Ignore:
Timestamp:
12/14/05 02:07:30 (3 years ago)
Author:
skud
Message:

Errr, lots of new stuff. Intro embiggened. Differences likewise. Moved p5 stuff into a
subdir.

Location:
docs/p6doc
Files:
8 added
1 removed
3 modified

Legend:

Unmodified
Added
Removed
  • docs/p6doc/Intro.pod

    r8200 r8235  
    101101=head2 Perl variable types 
    102102 
     103Perl has three main variable types: scalars, arrays, and hashes. 
     104 
     105=over 4 
     106 
     107=item Scalars 
     108 
     109A scalar represents a single value: 
     110 
     111    my $animal = "camel"; 
     112    my $answer = 42; 
     113 
     114Scalar values can be strings, integers or floating point numbers, and Perl 
     115will automatically convert between them as required.  There is no need 
     116to pre-declare your variable types (though you can if you want -- see L<XXX>). 
     117 
     118Scalar values can be used in various ways: 
     119 
     120    say $animal; 
     121    say "The animal is $animal"; 
     122    say "The square of $answer is ", $answer * $answer; 
     123 
     124There are a number of "magic" scalars with names that look like 
     125punctuation or line noise.  These special variables are used for all 
     126kinds of purposes, and are documented in L<XXX>.  The only one you 
     127need to know about for now is C<$_> which is the "default variable". 
     128It's used as the default argument to a number of functions in Perl, and 
     129it's set implicitly by certain looping constructs. 
     130 
     131    say;          # prints contents of $_ by default 
     132 
     133=item Arrays 
     134 
     135An array represents a list of values: 
     136 
     137    my @animals = ("camel", "llama", "owl"); 
     138    my @numbers = (23, 42, 69); 
     139    my @mixed   = ("camel", 42, 1.23); 
     140 
     141Arrays are zero-indexed.  Here's how you get at elements in an array: 
     142 
     143    say @animals[0];              # prints "camel" 
     144    say @animals[1];              # prints "llama" 
     145 
     146The numeric index of the last element of an array can by found with C<@array.end>: 
     147 
     148    say @animals[@animals.end];   # prints "owl" 
     149 
     150To find the number of elements in an array, use the C<elems> method: 
     151 
     152    say @mixed.elems;       # last element, prints 1.23 
     153 
     154To get multiple values from an array: 
     155 
     156    @animals[0,1];                  # gives ("camel", "llama"); 
     157    @animals[0..2];                 # gives ("camel", "llama", "owl"); 
     158    @animals[1..@animals.end];      # gives all except the first element 
     159 
     160This is called an "array slice". 
     161 
     162You can do various useful things to lists: 
     163 
     164    my @sorted    = @animals.sort; 
     165    my @backwards = @numbers.reverse; 
     166 
     167=begin maintainer_notes 
     168 
     169I have no idea what the following is in p6... I'll deal with them when I get there. 
     170 
     171 
     172There are a couple of special arrays too, such as C<@ARGV> (the command 
     173line arguments to your script) and C<@_> (the arguments passed to a 
     174subroutine).  These are documented in L<perlvar>. 
     175 
     176=end maintainer_notes 
     177 
     178=item Hashes 
     179 
     180A hash represents a set of key/value pairs: 
     181 
     182    my %fruit_color = ("apple", "red", "banana", "yellow"); 
     183 
     184You can use whitespace and the C<< => >> operator to lay them out more 
     185nicely: 
     186 
     187    my %fruit_color = ( 
     188        apple  => "red", 
     189        banana => "yellow", 
     190    ); 
     191 
     192To get at hash elements: 
     193 
     194    %fruit_color{"apple"};           # gives "red" 
     195 
     196You can get at lists of keys and values with C<keys()> and 
     197C<values()>. 
     198 
     199    my @fruits = %fruit_colors.keys; 
     200    my @colors = %fruit_colors.values; 
     201 
     202Hashes have no particular internal order, though you can sort the keys 
     203and loop through them. 
     204 
     205=begin maintainer_notes 
     206 
     207Again, no idea here... will get to it eventually. 
     208 
     209Just like special scalars and arrays, there are also special hashes. 
     210The most well known of these is C<%ENV> which contains environment 
     211variables.  Read all about it (and other special variables) in 
     212L<perlvar>. 
     213 
     214=end maintainer_notes 
     215 
     216=back 
     217 
     218Scalars, arrays and hashes are documented more fully in L<perldata>. 
     219 
     220More complex data types can be constructed using references, which allow 
     221you to build lists and hashes within lists and hashes. 
     222 
     223A reference is a scalar value and can refer to any other Perl data 
     224type. So by storing a reference as the value of an array or hash 
     225element, you can easily create lists and hashes within lists and 
     226hashes. The following example shows a 2 level hash of hash 
     227structure using anonymous hash references. 
     228 
     229    my $variables = { 
     230        scalar  =>  { 
     231                     description => "single item", 
     232                     sigil => '$', 
     233                    }, 
     234        array   =>  { 
     235                     description => "ordered list of items", 
     236                     sigil => '@', 
     237                    }, 
     238        hash    =>  { 
     239                     description => "key/value pairs", 
     240                     sigil => '%', 
     241                    }, 
     242    }; 
     243 
     244    print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n"; 
     245 
     246Exhaustive information on the topic of references can be found in 
     247L<perlreftut>, L<perllol>, L<perlref> and L<perldsc>. 
     248 
     249 
     250 
    103251=head2 Variable scoping 
    104252 
  • docs/p6doc/README

    r8198 r8235  
    2323We'll need a tool to bundle up the contents of this dir under Perl6::Doc 
    2424and push them out to CPAN. 
     25 
     26THE PLAN 
     27 
     28Here's my plan of attack. 
     29 
     301. Go through perlintro (from perl 5) and rewrite it for Perl 6 
     31 
     322. As I go along, take notes for perl5-to-6 transition docs 
     33 
     343. Go through the general language reference perl 5 docs (eg perlsyn, 
     35perlop, perlfunc, perldata), rewriting for perl 6.   
     36 
     374. Deal with stuff like tutorials and FAQs last, unless they happen to 
     38come up as I go along. 
  • docs/p6doc/TOC.pod

    r8200 r8235  
    1212    TOC                This table of contents 
    1313    Intro              A brief introduction for new Perl users 
    14     Perl5Differences   Quick overview for Perl 5 programmers 
     14 
     15=head2 Perl 5 topics 
     16 
     17    Perl5::Docfinder        Table of equivalences between Perl 5 and 6 docs 
     18    Perl5::Differences      Differences between Perl 5 and 6 
    1519 
    1620=head2 Language reference 
     21 
     22    Reference::Syntax       General syntax 
     23    Reference::Functions    Built-in functions 
     24    Reference::Operators    Built-in operators 
    1725 
    1826=head2 Tutorials