📄 00000019.htm
字号:
<TD ALIGN="left">An individual value (number or string)</TD> <BR> </TR> <BR> <BR> <TR CLASS=row> <BR> <TD ALIGN="left">Array</TD> <BR> <BR> <TD ALIGN="left"><TT>@</TT></TD> <BR> <BR> <TD ALIGN="left"><TT>@large</TT></TD> <BR> <BR> <TD ALIGN="left">A list of values, keyed by number</TD> <BR> </TR> <BR> <BR> <TR CLASS=row> <BR> <TD ALIGN="left">Hash</TD> <BR> <BR> <TD ALIGN="left"><TT>%</TT></TD> <BR> <BR> <TD ALIGN="left"><TT>%interest</TT></TD> <BR> <BR> <TD ALIGN="left">A group of values, keyed by string</TD> <BR> </TR> <BR> <BR> <TR CLASS=row> <BR> <TD ALIGN="left">Subroutine</TD> <BR> <BR> <TD ALIGN="left"><TT>&amp;</TT></TD> <BR> <BR> <TD ALIGN="left"><TT>&amp;how</TT></TD> <BR> <BR> <TD ALIGN="left">A callable chunk of Perl code</TD> <BR> </TR> <BR> <BR> <TR CLASS=row> <BR> <TD ALIGN="left">Typeglob</TD> <BR> <BR> <TD ALIGN="left"><TT>*</TT></TD> <BR> <BR> <TD ALIGN="left"><TT>*struck</TT></TD> <BR> <BR> <TD ALIGN="left">Everything named <TT>struck</TT></TD> <BR> </TR> <BR> </TABLE> <BR> <BR> <H4><A NAME="PERL2-CH-1-SECT-2.1.1"></A>Singularities</H4> <BR> <BR> <P>From our example, you can see that scalars may be assigned a new value <BR> with the <TT>=</TT> operator, just as in many other computer languages. <BR> Scalar variables can be assigned any form of scalar value: integers, floating-point <BR> numbers, strings, and even esoteric things like references to other variables, <BR> or to objects. There are many ways of generating these values for assignment. <BR> </P> <BR> <BR> <P>As in the UNIX shell, you can use different quoting mechanisms to make <BR> different kinds of values. Double quotation marks (double quotes) do variable <BR> interpolation[7] and backslash interpretation,[8] while single quotes suppress <BR> both interpolation and interpretation. And backquotes (the ones leaning <BR> to the left) will execute an external program and return the output of <BR> the program, so you can capture it as a single string containing all the <BR> lines of output. </P> <BR> <BR> <BLOCKQUOTE class=footnote> <BR> <P>[7] Sometimes called &quot;substitution&quot; by shell programmers, <BR> but we prefer to reserve that word for something else in Perl. So please <BR> call it interpolation. We're using the term in the textual sense (&quot;this <BR> passage is a Gnostic interpolation&quot;) rather than in the mathematical <BR> sense (&quot;this point on the graph is an interpolation between two other <BR> points&quot;). </P> <BR> <BR> <P>[8] Such as turning <TT>\t</TT> into a tab, <TT>\n</TT> into a newline, <BR> <TT>\ 001</TT> into a CTRL-A, and so on, in the tradition of many UNIX <BR> programs. </P> <BR> </BLOCKQUOTE> <BR> <BR> <PRE>$answer = 42; # an integer <BR> $pi = 3.14159265; # a &quot;real&quot; number <BR> $avocados = 6.02e23; # scientific notation <BR> $pet = &quot;Camel&quot;; # string <BR> $sign = &quot;I love my $pet&quot;; # string with interpolation <BR> $cost = 'It costs $100'; # string without interpolation <BR> $thence = $whence; # another variable <BR> $x = $moles * $avocados; # an expression <BR> $cwd = `pwd`; # string output from a command <BR> $exit = system(&quot;vi $x&quot;); # numeric status of a command <BR> $fido = new Camel &quot;Fido&quot;; # an object <BR> </PRE> <BR> <BR> <P>Uninitialized variables automatically spring into existence as needed. <BR> Following the principle of least surprise, they are created with a null <BR> value, either <TT>&quot;&quot;</TT> or <TT>0</TT>. Depending on where you <BR> use them, variables will be interpreted automatically as strings, as numbers, <BR> or as &quot;true&quot; and &quot;false&quot; values (commonly called Boolean <BR> values). Various operators expect certain kinds of values as parameters, <BR> so we will speak of those operators as &quot;providing&quot; or &quot;supplying&quot; <BR> a scalar context to those parameters. Sometimes we'll be more specific, <BR> and say it supplies a numeric context, a string context, or a Boolean context <BR> to those parameters. (Later we'll also talk about list context, which is <BR> the opposite of scalar context.) Perl will automatically convert the data <BR> into the form required by the current context, within reason. For example, <BR> suppose you said this: </P> <BR> <BR> <PRE>$camels = '123'; <BR> print $camels + 1, &quot;\n&quot;; <BR> </PRE> <BR> <BR> <P>The original value of <TT>$camels</TT> is a string, but it is converted <BR> to a number to add <TT>1</TT> to it, and then converted back to a string <BR> to be printed out as <TT>124</TT>. The newline, represented by <TT>&quot;\n&quot;</TT>, <BR> is also in string context, but since it's already a string, no conversion <BR> is necessary. But notice that we had to use double quotes there--using <BR> single quotes to say <TT>'\n'</TT> would result in a two-character string <BR> consisting of a backslash followed by an &quot;<TT>n</TT>&quot;, which <BR> is not a newline by anybody's definition. </P> <BR> <BR> <P>So, in a sense, double quotes and single quotes are yet another way <BR> of specifying context. The interpretation of the innards of a quoted string <BR> depends on which quotes you use. Later we'll see some other operators that <BR> work like quotes syntactically, but use the string in some special way, <BR> such as for pattern matching or substitution. These all work like double-quoted <BR> strings too. The <I>double-quote</I> context is the &quot;interpolative&quot; <BR> context of Perl, and is supplied by many operators that don't happen to <BR> resemble double quotes. </P> <BR> <BR> <H4><A NAME="PERL2-CH-1-SECT-2.1.2"></A>Pluralities</H4> <BR> <BR> <P>Some kinds of variables hold multiple values that are logically tied <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -