⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strings.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#ff7f50>print</FONT> expand<FONT =
color=3D#00ffff>(</FONT>$_<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Text::Tabs</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#ff7f50>print</FONT> =
unexpand<FONT color=3D#00ffff>(</FONT>$_<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN38>Expanding Variables in User =
Input</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
#You owe $debt to me.
#-----------------------------
</FONT>$text =3D~ <FONT color=3D#ff7f50>s</FONT>/\$<FONT =
color=3D#00ffff>(</FONT>\w+<FONT color=3D#00ffff>)</FONT>/$<FONT =
color=3D#00ffff>{</FONT>$<FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>}</FONT>/g<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$text =3D~ <FONT color=3D#ff7f50>s</FONT>/<FONT =
color=3D#00ffff>(</FONT>\$\w+<FONT color=3D#00ffff>)</FONT>/$<FONT =
color=3D#cdcd00>1</FONT>/gee<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>vars</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>$rows $cols<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>no</FONT> <FONT color=3D#b2dfee>strict</FONT> =
<FONT color=3D#00cd00>'refs'</FONT><FONT color=3D#00ffff>;</FONT>        =
           <FONT color=3D#bebebe># for ${$1}/g below
</FONT><FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$text</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#00ffff>(</FONT>$rows, $cols<FONT color=3D#00ffff>)</FONT> =
=3D <FONT color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>24</FONT>, <FONT =
color=3D#cdcd00>80</FONT><FONT color=3D#00ffff>);</FONT>
$text =3D <FONT color=3D#ff7f50>q</FONT><FONT color=3D#00ffff>(</FONT>I =
am $rows high <FONT color=3D#98fb98>and</FONT> $cols long<FONT =
color=3D#00ffff>);</FONT>  <FONT color=3D#bebebe># like single quotes!
</FONT>$text =3D~ <FONT color=3D#ff7f50>s</FONT>/\$<FONT =
color=3D#00ffff>(</FONT>\w+<FONT color=3D#00ffff>)</FONT>/$<FONT =
color=3D#00ffff>{</FONT>$<FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>}</FONT>/g<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> $text<FONT color=3D#00ffff>;</FONT>
I am <FONT color=3D#cdcd00>24</FONT> high <FONT =
color=3D#98fb98>and</FONT> <FONT color=3D#cdcd00>80</FONT> long
<FONT color=3D#bebebe>#-----------------------------
</FONT>$text =3D <FONT color=3D#00cd00>"I am 17 years old"</FONT><FONT =
color=3D#00ffff>;</FONT>
$text =3D~ <FONT color=3D#ff7f50>s</FONT>/<FONT =
color=3D#00ffff>(</FONT>\d+<FONT color=3D#00ffff>)</FONT>/<FONT =
color=3D#cdcd00>2</FONT> * $<FONT color=3D#cdcd00>1</FONT>/eg<FONT =
color=3D#00ffff>;</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdcd00>2</FONT> * <FONT color=3D#cdcd00>17</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$text =3D <FONT color=3D#00cd00>'I am $AGE years old'</FONT><FONT =
color=3D#00ffff>;</FONT>      <FONT color=3D#bebebe># note single quotes
</FONT>$text =3D~ <FONT color=3D#ff7f50>s</FONT>/<FONT =
color=3D#00ffff>(</FONT>\$\w+<FONT color=3D#00ffff>)</FONT>/$<FONT =
color=3D#cdcd00>1</FONT>/eg<FONT color=3D#00ffff>;</FONT>           =
<FONT color=3D#bebebe># WRONG
#-----------------------------
</FONT><FONT color=3D#00cd00>'$AGE'</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$text =3D~ <FONT color=3D#ff7f50>s</FONT>/<FONT =
color=3D#00ffff>(</FONT>\$\w+<FONT color=3D#00ffff>)</FONT>/$<FONT =
color=3D#cdcd00>1</FONT>/eeg<FONT color=3D#00ffff>;</FONT>          =
<FONT color=3D#bebebe># finds my() variables
#-----------------------------
# expand variables in $text, but put an error message in
# if the variable isn't defined
</FONT>$text =3D~ <FONT color=3D#ff7f50>s</FONT><FONT =
color=3D#00ffff>{</FONT>
     \$                         <FONT color=3D#bebebe># find a literal =
dollar sign
</FONT>    <FONT color=3D#00ffff>(</FONT>\w+<FONT =
color=3D#00ffff>)</FONT>                       <FONT color=3D#bebebe># =
find a "word" and store it in $1
</FONT><FONT color=3D#00ffff>}{</FONT>
    <FONT color=3D#ff7f50>no</FONT> <FONT color=3D#b2dfee>strict</FONT> =
<FONT color=3D#00cd00>'refs'</FONT><FONT color=3D#00ffff>;</FONT>        =
   <FONT color=3D#bebebe># for $$1 below
</FONT>    <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>defined</FONT> $<FONT =
color=3D#00ffff>{</FONT>$<FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>})</FONT> <FONT color=3D#00ffff>{</FONT>
        $<FONT color=3D#00ffff>{</FONT>$<FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>};</FONT>                  =
<FONT color=3D#bebebe># expand global variables only
</FONT>    <FONT color=3D#00ffff>}</FONT> <FONT =
color=3D#ffa500>else</FONT> <FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#00cd00>"[NO VARIABLE: \$$1]"</FONT><FONT =
color=3D#00ffff>;</FONT>  <FONT color=3D#bebebe># error msg
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>egx<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN41>Controlling Case</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>locale</FONT><FONT color=3D#00ffff>;</FONT>              =
       <FONT color=3D#bebebe># needed in 5.004 or above
</FONT>
$big =3D <FONT color=3D#98fb98>uc</FONT><FONT =
color=3D#00ffff>(</FONT>$little<FONT color=3D#00ffff>);</FONT>           =
  <FONT color=3D#bebebe># "bo peep" -&gt; "BO PEEP"
</FONT>$little =3D <FONT color=3D#98fb98>lc</FONT><FONT =
color=3D#00ffff>(</FONT>$big<FONT color=3D#00ffff>);</FONT>             =
<FONT color=3D#bebebe># "JOHN"    -&gt; "john"
</FONT>$big =3D <FONT color=3D#00cd00>"\U$little"</FONT><FONT =
color=3D#00ffff>;</FONT>             <FONT color=3D#bebebe># "bo peep" =
-&gt; "BO PEEP"
</FONT>$little =3D <FONT color=3D#00cd00>"\L$big"</FONT><FONT =
color=3D#00ffff>;</FONT>             <FONT color=3D#bebebe># "JOHN"    =
-&gt; "john"
#-----------------------------
</FONT>$big =3D <FONT color=3D#00cd00>"\u$little"</FONT><FONT =
color=3D#00ffff>;</FONT>             <FONT color=3D#bebebe># "bo"      =
-&gt; "Bo"
</FONT>$little =3D <FONT color=3D#00cd00>"\l$big"</FONT><FONT =
color=3D#00ffff>;</FONT>             <FONT color=3D#bebebe># "BoPeep"    =
-&gt; "boPeep"</FONT><FONT color=3D#43cd80> </FONT><FONT =
color=3D#bebebe>
#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>locale</FONT><FONT color=3D#00ffff>;</FONT>              =
       <FONT color=3D#bebebe># needed in 5.004 or above
</FONT>
$beast   =3D <FONT color=3D#00cd00>"dromedary"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># capitalize various parts of $beast
</FONT>$capit   =3D <FONT color=3D#98fb98>ucfirst</FONT><FONT =
color=3D#00ffff>(</FONT>$beast<FONT color=3D#00ffff>);</FONT>         =
<FONT color=3D#bebebe># Dromedary
</FONT>$capit   =3D <FONT color=3D#00cd00>"\u\L$beast"</FONT><FONT =
color=3D#00ffff>;</FONT>            <FONT color=3D#bebebe># (same)
</FONT>$capall  =3D <FONT color=3D#98fb98>uc</FONT><FONT =
color=3D#00ffff>(</FONT>$beast<FONT color=3D#00ffff>);</FONT>            =
  <FONT color=3D#bebebe># DROMEDARY
</FONT>$capall  =3D <FONT color=3D#00cd00>"\U$beast"</FONT><FONT =
color=3D#00ffff>;</FONT>              <FONT color=3D#bebebe># (same)
</FONT>$caprest =3D <FONT color=3D#98fb98>lcfirst</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>uc</FONT><FONT =
color=3D#00ffff>(</FONT>$beast<FONT color=3D#00ffff>));</FONT>     <FONT =
color=3D#bebebe># dROMEDARY
</FONT>$caprest =3D <FONT color=3D#00cd00>"\l\U$beast"</FONT><FONT =
color=3D#00ffff>;</FONT>            <FONT color=3D#bebebe># (same)
#-----------------------------
# capitalize each word's first character, downcase the rest
</FONT>$text =3D <FONT color=3D#00cd00>"thIS is a loNG liNE"</FONT><FONT =
color=3D#00ffff>;</FONT>
$text =3D~ <FONT color=3D#ff7f50>s</FONT>/<FONT =
color=3D#00ffff>(</FONT>\w+<FONT color=3D#00ffff>)</FONT>/\u\L$<FONT =
color=3D#cdcd00>1</FONT>/g<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> $text<FONT color=3D#00ffff>;</FONT>
This Is A Long Line
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>uc</FONT><FONT =
color=3D#00ffff>(</FONT>$a<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>eq</FONT> <FONT color=3D#98fb98>uc</FONT><FONT =
color=3D#00ffff>(</FONT>$b<FONT color=3D#00ffff>))</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"a and b =
are the same\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch01/randcap">download =
the following standalone program</A></FONT>
#!/usr/bin/perl -p
# randcap: filter to randomly capitalize 20% of the letters
# call to srand() is unnecessary in 5.004
</FONT><FONT color=3D#ffa500>BEGIN</FONT> <FONT color=3D#00ffff>{</FONT> =
<FONT color=3D#98fb98>srand</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>time</FONT><FONT color=3D#00ffff>()</FONT> ^ <FONT =
color=3D#00ffff>(</FONT>$$ + <FONT color=3D#00ffff>(</FONT>$$ &lt;&lt; =
<FONT color=3D#cdcd00>15</FONT><FONT color=3D#00ffff>)))</FONT> <FONT =
color=3D#00ffff>}</FONT>
<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>randcase</FONT> =
<FONT color=3D#00ffff>{</FONT> <FONT color=3D#98fb98>rand</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>100</FONT><FONT =
color=3D#00ffff>)</FONT> &lt; <FONT color=3D#cdcd00>20</FONT> ? <FONT =
color=3D#00cd00>"\u</FONT><FONT color=3D#cdad00>$_</FONT><FONT =
color=3D#00cd00>[0]"</FONT> : <FONT color=3D#00cd00>"\l</FONT><FONT =
color=3D#cdad00>$_</FONT><FONT color=3D#00cd00>[0]"</FONT> <FONT =
color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>s</FONT>/<FONT color=3D#00ffff>(</FONT>\w<FONT =
color=3D#00ffff>)</FONT>/randcase<FONT color=3D#00ffff>(</FONT>$<FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>)</FONT>/<FONT =
color=3D#98fb98>ge</FONT><FONT color=3D#00ffff>;</FONT>


<FONT color=3D#bebebe>#% randcap &lt; genesis | head -9
#boOk 01 genesis
#
#
#001:001 in the BEginning goD created the heaven and tHe earTh.
#
#</FONT><FONT color=3D#43cd80>    </FONT><FONT color=3D#bebebe>
#
#001:002 and the earth wAS without ForM, aND void; AnD darkneSS was
#
#	 upon The Face of the dEEp. and the spIrit of GOd movEd upOn
#
#	 tHe face of the Waters.
#
#
#001:003 and god Said, let there be ligHt: and therE wAs LigHt.
#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>randcase</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#98fb98>rand</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>100</FONT><FONT color=3D#00ffff>)</FONT> &lt; <FONT =
color=3D#cdcd00>20</FONT> ? <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"\040"</FONT> ^ <FONT color=3D#cdad00>$_</FONT><FONT =
color=3D#00ffff>[</FONT><FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>])</FONT> : <FONT color=3D#cdad00>$_</FONT><FONT =
color=3D#00ffff>[</FONT><FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>];</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$string &amp;=3D <FONT color=3D#00cd00>"\177"</FONT> <FONT =
color=3D#98fb98>x</FONT> <FONT color=3D#98fb98>length</FONT><FONT =
color=3D#00ffff>(</FONT>$string<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN44>Interpolating Functions and =
Expressions Within=20
Strings</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT>$answer =3D $var1 . func<FONT color=3D#00ffff>()</FONT> . =
$var2<FONT color=3D#00ffff>;</FONT>   <FONT color=3D#bebebe># scalar =
only
#-----------------------------
</FONT>$answer =3D <FONT color=3D#00cd00>"STRING @{[ LIST EXPR ]} MORE =
STRING"</FONT><FONT color=3D#00ffff>;</FONT>
$answer =3D <FONT color=3D#00cd00>"STRING ${\( SCALAR EXPR )} MORE =
STRING"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -