# Copyright (C) 2003-2007, The Perl Foundation. =pod =head0 Basic Syntax Z Perl 6 is a work in progress, so the syntax is still changing, but not as fast as it used to be. The next 4 chapters are likely to be slightly outdated by the time you read them. Even so, they provide a good baseline. If you start here, you'll only have to catch up on a few months of changes (starting with the design documents after Apocalypse 12), instead of several years worth. Pretend for a moment that you don't know anything about Perl. You heard the language has some neat features, so you thought you might check it out. You go to the store and pick up a copy of I because you think this Larry Wall guy might know something about it. It's the latest version, put out for the 6.0.1 release of Perl. It's not a delta document describing the changes, it's an introduction, and you dive in with the curiosity of a kid who got a telescope for his birthday. These chapters are a glimpse down that telescope. There's plenty of time later to analyze each feature and decide which you like and which you don't. For now, take a step back and get a feel for the system as a whole, for what it'll be like to work in it. =head1 Variables Z X The most basic building blocks of a programming language are its nouns, the chunks of data that get sucked in, pushed around, altered in various ways, and spat out to some new location. The chunks of data are values: strings, numbers, etc., or composites of the simpler values. Variables are just named containers for those values. The three kinds of variables in Perl 6 are scalars, arrays, and hashes. Each has an identifying symbol (or sigil) as part of the name of the variable: C<$> for scalars, C<@> for arrays, and C<%> for hashes. The sigils provide a valuable visual distinction by making it immediately obvious what kinds of behavior a particular variable is likely to have. But, fundamentally, there's little difference between the three. Each variable is essentially a container for a value, whether that value is single or collective. (This statement is an oversimplification, as you'll soon see.) =head2 Scalars Z X<$ (dollar sign);for scalar variable names> X Scalars are all-purpose containers. They can hold strings, integers, floating-point numbers, and all kinds of objects and built-in types. For example: $string = "Zaphod's just this guy, you know?"; $int = 42; $float = 3.14159; $array = [ "Zaphod", "Ford", "Trillian" ]; $hash = { "Zaphod" => 362, "Ford" => 1574, "Trillian" => 28 }; $sub = sub { print $string }; $object = Android.new; A X filehandle is just an ordinary object in an ordinary scalar variable. For example: $filehandle = open $filename; Assignment to a scalar parses a single item after the C<=> and stops at the next unprotected comma. $foo = 1, $bar = 2; # two scalar assignments =head2 Arrays Z X<@ (at sign);for array names> X X Array variables hold simple ordered collections of scalar values. Individual values are retrieved from the array by numeric index. The C<0> index holds the first value. The C<@> sigil is part of the name of the variable and stays the same no matter how the variable is used: @crew = "Zaphod", "Ford", "Trillian"; $second_member = @crew[1]; # Ford Unlike scalar assignment, assignment to an array parses everything after the C<=> as a list, so parentheses are not needed around the list. X X To get the the number of elements in an array use the C<.elems> method. The C<.last> method returns the index of the last element in an array--that is, the highest index in an array. $count_elements = @crew.elems; $last_index = @crew.last; =head2 Pairs Z A C holds a single key and a single value. It doesn't have a unique sigil because pairs rarely appear alone, so they're stored in scalars, arrays, or hashes. The pair constructor C<< => >> forms a pair, with the key on the left and value on the right. $pair = 'key' => 'value'; The alternate I