From: Shlomi Fish Date: Wed, 14 May 2003 17:46:05 +0000 (+0300) Subject: perldata.pod revamp rev. 3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=692ef166768038d07562d41a21f34b424912aa08;p=p5sagit%2Fp5-mst-13.2.git perldata.pod revamp rev. 3 Message-ID: p4raw-id: //depot/perl@19584 --- diff --git a/pod/perldata.pod b/pod/perldata.pod index c58d419..f60d016 100644 --- a/pod/perldata.pod +++ b/pod/perldata.pod @@ -7,10 +7,12 @@ perldata - Perl data types =head2 Variable names Perl has three built-in data types: scalars, arrays of scalars, and -associative arrays of scalars, known as "hashes". Normal arrays -are ordered lists of scalars indexed by number, starting with 0 and with -negative subscripts counting from the end. Hashes are unordered -collections of scalar values indexed by their associated string key. +associative arrays of scalars, known as "hashes". A scalar is a +single string (of any size, limited only by the available memory), +number, or a reference to something (which will be discussed +in L). Normal arrays are ordered lists of scalars indexed +by number, starting with 0. Hashes are unordered collections of scalar +values indexed by their associated string key. Values are usually referred to by name, or through a named reference. The first character of the name tells you to what sort of data @@ -187,8 +189,8 @@ operator to produce an undefined value. To find out whether a given string is a valid non-zero number, it's sometimes enough to test it against both numeric 0 and also lexical -"0" (although this will cause B<-w> noises). That's because strings -that aren't numbers count as 0, just as they do in B: +"0" (although this will cause noises if warnings are on). That's +because strings that aren't numbers count as 0, just as they do in B: if ($str == 0 && $str ne "0") { warn "That doesn't look like a number"; @@ -309,8 +311,10 @@ names beginning with $ or @, followed by an optional bracketed expression as a subscript.) The following code segment prints out "The price is $Z<>100." - $Price = '$100'; # not interpreted - print "The price is $Price.\n"; # interpreted + $Price = '$100'; # not interpolated + print "The price is $Price.\n"; # interpolated + +There is no double interpolation in Perl, so the C<$100> is left as is. As in some shells, you can enclose the variable name in braces to disambiguate it from following alphanumerics (and underscores). @@ -335,6 +339,8 @@ C<$days{Feb}> and the quotes will be assumed automatically. But anything more complicated in the subscript will be interpreted as an expression. +=head3 Version Strings + A literal of the form C is parsed as a string composed of characters with the specified ordinals. This form, known as v-strings, provides an alternative, more readable way to construct @@ -354,6 +360,8 @@ running Perl interpreter's version in this form. See L. Note that using the v-strings for IPv4 addresses is not portable unless you also use the inet_aton()/inet_ntoa() routines of the Socket package. +=head3 Special Literals + The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program. They may be used only as separate tokens; they @@ -381,6 +389,8 @@ filehandle in a BEGIN block: the BEGIN block is executed as soon as it is seen (during compilation), at which point the corresponding __DATA__ (or __END__) token has not yet been seen. +=head3 Barewords + A word that has no other interpretation in the grammar will be treated as if it were a quoted string. These are known as "barewords". As with filehandles and labels, a bareword that consists @@ -397,10 +407,12 @@ produces a compile-time error instead. The restriction lasts to the end of the enclosing block. An inner block may countermand this by saying C. +=head3 Array Joining Delimiter + Arrays and slices are interpolated into double-quoted strings by joining the elements with the delimiter specified in the C<$"> -variable (C<$LIST_SEPARATOR> in English), space by default. The -following are equivalent: +variable (C<$LIST_SEPARATOR> if "use English;" is specified), +space by default. The following are equivalent: $temp = join($", @ARGV); system "echo $temp"; @@ -606,6 +618,32 @@ Note that just because a hash is initialized in that order doesn't mean that it comes out in that order. See L for examples of how to arrange for an output ordering. +=head2 Subscripts + +An array is subscripted by specifying a dollary sign (C<$>), then the +name of the array (without the leading C<@>), then the subscript inside +square brackets. For example: + + @myarray = (5, 50, 500, 5000); + print "Element Number 2 is", $myarray[2], "\n"; + +The array indices start with 0. A negative subscript retrieves its +value from the end. In our example, C<$myarray[-1]> would have been +5000, and C<$myarray[-2]> would have been 500. + +Hash subscripts are similar, only instead of square brackets curly brackets +are used. For example: + + %scientists = + ( + "Newton" => "Isaac", + "Einstein" => "Albert", + "Darwin" => "Charles", + "Feynman" => "Richard", + ); + + print "Darwin's First Name is ", $scientists{"Darwin"}, "\n"; + =head2 Slices A common way to access an array or a hash is one scalar element at a