📄 ch2.htm
字号:
<BLOCKQUOTE>
<PRE>
"The Dirty" . (2*6)
</PRE>
</BLOCKQUOTE>
<P>
which will give you this string result:
<BLOCKQUOTE>
<PRE>
"The Dirty12"
</PRE>
</BLOCKQUOTE>
<P>
Remember, before Perl processes the operand outside the parentheses,
it processes the operand inside the parentheses.
<H4>Numeric Operators</H4>
<P>
Operators for numbers are pretty much what you might expect. One
odd one is from the C programming language and it's called a modulus.
It is the "%" symbol and it divides two numbers by their
integer value, not their actual value, and then takes the remainder
as the new value. For example,
<BLOCKQUOTE>
<PRE>
25.3 % 4.4378742
</PRE>
</BLOCKQUOTE>
<P>
is first converted to
<BLOCKQUOTE>
<PRE>
25 % 4
</PRE>
</BLOCKQUOTE>
<P>
and then the new value is 1, which is what remains after 4 divides
into 25. A full listing of numeric operators appears in Table
2.2.<BR>
<P>
<CENTER><B>Table 2.2 Numeric Operators</B></CENTER>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=60% CELLPADDING=3>
<TR VALIGN=TOP><TD><CENTER><B>Operator</B></CENTER></TD><TD><B>Action</B>
</TD><TD><B>Example</B></TD></TR>
<TR VALIGN=TOP><TD><CENTER>+</CENTER></TD><TD>Addition
</TD><TD>1+2, or 3</TD></TR>
<TR VALIGN=TOP><TD><CENTER>-</CENTER></TD><TD>Subtraction
</TD><TD>1-2, or -1</TD></TR>
<TR VALIGN=TOP><TD><CENTER>*</CENTER></TD><TD>Multiplication
</TD><TD>2*2, or 4</TD></TR>
<TR VALIGN=TOP><TD><CENTER>/</CENTER></TD><TD>Division
</TD><TD>2/2, or 1</TD></TR>
<TR VALIGN=TOP><TD><CENTER>**</CENTER></TD><TD>Exponentiation
</TD><TD>2**3, or 8</TD></TR>
<TR VALIGN=TOP><TD><CENTER>%</CENTER></TD><TD>Modulus
</TD><TD>2.3%3.2, or 0</TD></TR>
<TR VALIGN=TOP><TD><CENTER><</CENTER></TD><TD>Less than
</TD><TD>2<3</TD></TR>
<TR VALIGN=TOP><TD><CENTER><=</CENTER></TD><TD>Less than or equal to
</TD><TD>2<=3</TD></TR>
<TR VALIGN=TOP><TD><CENTER>==</CENTER></TD><TD>Equal to
</TD><TD>2==4/2</TD></TR>
<TR VALIGN=TOP><TD><CENTER>>=</CENTER></TD><TD>Greater than or equal to
</TD><TD>3>=2</TD></TR>
<TR VALIGN=TOP><TD><CENTER>></CENTER></TD><TD>Greater than
</TD><TD>5>1</TD></TR>
<TR VALIGN=TOP><TD><CENTER>!=</CENTER></TD><TD>Not equal to
</TD><TD>5!=1</TD></TR>
</TABLE></CENTER>
<P>
<P>
When these operators are used, Perl makes a comparison and then
returns a true or false value. Operators for strings are a little
different.
<H4>String Operators</H4>
<P>
These operators work the same way as the numeric ones, and are
represented by characters. The concatenate operator used previously
can have these different results:
<UL>
<LI>"Hey," . "now!" produces "Hey, now!"
<LI>"Hey, now!" . "\n" produces "Hey,
now!\n"
<LI>"Hey," . " " . "now!" produces
"Hey, now!"
</UL>
<P>
Other string operators are listed in Table 2.3.<BR>
<P>
<CENTER><B>Table 2.3 String Operators</B></CENTER>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=60% CELLPADDING=3>
<TR VALIGN=TOP><TD><CENTER><B>Operator</B></CENTER></TD><TD><B>Action</B>
</TD><TD><B>Example</B></TD></TR>
<TR VALIGN=TOP><TD><CENTER> </CENTER></TD><TD>Concatenate
</TD><TD>"bi" . "g", or "big"
</TD></TR>
<TR VALIGN=TOP><TD><CENTER>eq</CENTER></TD><TD>Equal
</TD><TD>"small" eq "small"</TD>
</TR>
<TR VALIGN=TOP><TD><CENTER>ne</CENTER></TD><TD>Not equal
</TD><TD>"small" ne "tiny"</TD>
</TR>
<TR VALIGN=TOP><TD><CENTER>lt</CENTER></TD><TD>Less than
</TD><TD>"30" lt "7"</TD></TR>
<TR VALIGN=TOP><TD><CENTER>gt</CENTER></TD><TD>Greater than
</TD><TD>"50" gt "300"</TD></TR>
<TR VALIGN=TOP><TD><CENTER>le</CENTER></TD><TD>Less than or equal to
</TD><TD>"ten" le "ten"</TD></TR>
<TR VALIGN=TOP><TD><CENTER>ge</CENTER></TD><TD>Greater than or equal to
</TD><TD>"eleven" ge "eleven"</TD>
</TR>
<TR VALIGN=TOP><TD><CENTER>x</CENTER></TD><TD>String repetition
</TD><TD>"more" x 2, or "moremore"
</TD></TR>
</TABLE></CENTER>
<P>
<P>
You might have noticed something odd in Table 2.3 with the less
than and greater than examples. They seem confusing, but remember,
we're dealing with strings now, not numbers, so Perl takes the
ASCII values of the number characters because it sees them as
strings with these operators. This makes 30 less than 7 because
3 is less than 7 in ASCII.
<P>
It should be noted that, just as Perl will process what's inside
parentheses first, it also gives special precedence and associativity
(paying attention to which side of the operator a value is on)
to each of its operators. For a full list of these relationships
check Appendix A.
<H2><A NAME="ScalarVariables"><FONT SIZE=5 COLOR=#FF0000>
Scalar Variables</FONT></A></H2>
<P>
While a scalar value may change during the run of a program, the
scalar variable remains the same. The variable is merely the jug
into which the sweet wine of value is poured. A scalar variable
can contain only one value at a time, but that value can change
as the program demands. A scalar variable has this format:
<BLOCKQUOTE>
<PRE>
$the_variable's_name $another_variable
</PRE>
</BLOCKQUOTE>
<P>
where you see that white space ends the variable name. Variables
are case-sensitive, so $JAZZ is different from $jazz, which is
different again from $Jazz. When choosing a variable name it is
highly recommended to pick something that reflects the value or
task that the variable is fufilling in your script to make it
easier for anyone (including yourself) to read the script.
<P>
Variables work with operators to give them their values. The most
common use you will encounter is something like this:
<BLOCKQUOTE>
<PRE>
$IQ = 13Ø
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
$Weight = $IQ + 2Ø
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
$page = $weight - 17
</PRE>
</BLOCKQUOTE>
<P>
where the different scalar variables are assigned values based
on integers, or other scalar variables. You can also use scalar
variables on both sides of an operator, like so:
<BLOCKQUOTE>
<PRE>
$income = $income - $food
</PRE>
</BLOCKQUOTE>
<P>
where the value of $income is assigned a new value of $income
minus $food. This type of relationship is so common in Perl, as
in other languages, that Perl has developed a shortcut to do this
called a<I> binary assignment operator. </I>Almost all of Perl's
binary assignment operators are made of some modified "="
symbol. For example, a binary assignment that could be used in
place of an ordinary operator might take $income = $income-$rent
and change it to a binary assignment like so:
<BLOCKQUOTE>
<PRE>
$income -= $rent
</PRE>
</BLOCKQUOTE>
<P>
where the operator "-" is made into a binary assignment
when added to the front of the "=" operator. Binary
is a clever name for these new assignments, considering that two
operators are used to make them. Almost any operator can be used
as a binary assignment in this way.
<P>
Beyond the binary assignment are the autoincrement and autodecrement
operators. These can be used to automatically add or subtract
one to a variable. Autoincrement is signified with the "++"
symbols. It could be used like this:
<BLOCKQUOTE>
<PRE>
$debt += 1, is the same as
++$debt
</PRE>
</BLOCKQUOTE>
<P>
with the value of $debt going up one with every pass. Autodecrement
is very similar, with the "--" symbols being placed
in front of a variable to lower its value by one with each pass.
<P>
You can only use the autoincrement and autodecrement operators
to move your variable up or down one step.
<H3><A NAME="TheChopOperator">
The Chop Operator</A></H3>
<P>
This operator, as has been described before, takes a scalar variable
and chops the last character from it. And what use does this have?
<P>
It all has to do with Perl's unending search for truth. The real
power behind Perl can be found in the way it compares variables,
finds them "true" or "false," records the
result, and then moves on. One of the catches is that Perl should
treat a blank line like a null sting, which would give it a false
value. Always one step ahead, Perl was made to see a blank line
as a new line, which is given the value "\n," and this
has a true value.
<P>
However, you might not want that new line there, so you can use
the chop operator to get rid of it. The default value of chop()
is to chop off the last character. You will see chop used, especially
in relation to the "\n" value, in several of the scripts
in this book.
<H3><A NAME="InterpolationofScalarsintoStrings">
Interpolation of Scalars into Strings</A></H3>
<P>
Interpolation is related to double-quoted strings. When a string
literal, like "Hey, now!" is double-quoted, Perl knows
to look through the string for any scalar variables. When Perl
finds one of these variables, its literal value is put in its
place before it is output. For example:
<BLOCKQUOTE>
<PRE>
$uid = "user_id";
$new = "new $uid"; # $new has the value
# "new user_id"
$x = "$new and $data"; # $x only has
# the value "new user_id and" because
# $data is an undefined variable
</PRE>
</BLOCKQUOTE>
<P>
Interpolation, a fancy word that means "putting a literal
value into a string" in Perl, works on the first pass through
a line only. You can see this in the following example:
<BLOCKQUOTE>
<PRE>
$min = "$time";
$clock = "$min left to go";
</PRE>
</BLOCKQUOTE>
<P>
The value of $clock is "$time left to go." There is
no double substitution.
<P>
If you want to avoid the substitution of a variable with its value,
then you have to either add single quotes to that area of the
string, or put a backslash in front of the "$" symbol
to turn off its special significance. This might look like:
<BLOCKQUOTE>
<PRE>
$x = "The variable used for the user id is ".'$user';
$y = "The variable used for the user id is \$user";
</PRE>
</BLOCKQUOTE>
<P>
where both of the outputs will be "The variable used for
the user id is $user."
<P>
You can use interpolation to avoid tripping up the Perl interpreter
with your variables. Say you want to have some text that remains
constant after a variable. With Perl, the variable name is the
longest possible name it can find, so that causes trouble with
finding your variable. This example might help clear up what I
mean. In this script we are trying to unite a changing variable
with a constant piece of text:
<BLOCKQUOTE>
<PRE>
$old = "name_one";
$new = "name_two";
$previous = "You are $old user.";
$current = "You are {$new} user.";
</PRE>
</BLOCKQUOTE>
<P>
If we print $previous we get "You are $old user." because
Perl holds $olduser as a new variable. But if we add curly braces
to the line, we get a more satisfactory result. When $current
is printed we get "You are name_two user." We avoided
the confusion by enclosing the name of the variable in curly braces.
<P>
Curly braces, also referred to as curly brackets, perform a number
of duties in Perl, from delimiting compound statements, as above,
to marking the beginning and end of loops and subroutines. They
are even involved with regular expressions (a part of Perl explored
in the next chapter). Generally they act as high-end markers,
or delimiters, but there are no hard and fast rules for using
curly braces, so close attention must be paid to when and where
they are used.
<P>
You can also change the case of your variables and values using
the case-shifting backslash operators mentioned earlier. The following
are all ways of changing case:
<BLOCKQUOTE>
<PRE>
$user = "\LNAME"; # changes value
# from 'NAME' to 'name'
$new = "NAME"; $user = "\L$new";
# gives us 'name' for the value of
# $user
</PRE>
</BLOCKQUOTE>
<P>
or the operators can be used together:
<BLOCKQUOTE>
<PRE>
$user = "\LNAME"; # $user is 'name'
$biguser = "\u\LNAME"; # $biguser is 'Name'
$biguser = "\u\L$user"; # $biguser is 'Name'
</PRE>
</BLOCKQUOTE>
<P>
This method works because Perl will remember case-shifting operators
that are in a string until they are used, so you can put \L between
\u and $user, and \u will see the first letter in the value of
$user as the letter is should modify.
<P>
With variable interpolation, only double-quoted strings can make
use of these functions. That may be why another name for it is
double-quote interpolation.
<H3><A NAME="StandardInputltSTDINgt">
Standard Input <STDIN></A></H3>
<P>
In Perl there is one special variable called Standard Input, or
<STDIN>. Actually, <STDIN> is much more than a scalar
variable, it is a filehandle. Other common filehandles you might
use in Perl are standard output, or <STDOUT>, and standard
error, or <STDERR>. A filehandle is a way for Perl to make
an input and output, or I/O, connection between a running script,
or process, and a user. A user can be a human, or another process.
<BR>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=576>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -