Front page | perl.perl5.porters |
Postings from May 2003
[PATCH] perldata.pod revamp rev. 3
Thread Next
From:
Shlomi Fish
Date:
May 14, 2003 07:47
Subject:
[PATCH] perldata.pod revamp rev. 3
Message ID:
Pine.LNX.4.33L2.0305141744520.24207-200000@vipe.technion.ac.il
--- orig/perldata.pod Mon May 12 17:51:14 2003
+++ pod/perldata.pod Wed May 14 17:43:45 2003
@@ -7,10 +7,12 @@
=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<perlref>). 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 @@
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<awk>:
+"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<awk>:
if ($str == 0 && $str ne "0") {
warn "That doesn't look like a number";
@@ -309,8 +311,10 @@
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 @@
anything more complicated in the subscript will be interpreted as
an expression.
+=head3 Version Strings
+
A literal of the form C<v1.20.300.4000> 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 @@
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 @@
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 @@
end of the enclosing block. An inner block may countermand this
by saying C<no strict 'subs'>.
+=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 @@
mean that it comes out in that order. See L<perlfunc/sort> 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
Thread Next
-
[PATCH] perldata.pod revamp rev. 3
by Shlomi Fish