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

📄 subroutines.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
color=3D#9ac0cd>$variable</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>mysub</FONT> =
<FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#bebebe># ... accessing $variable
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>BEGIN</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$variable</FONT> =3D <FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>;</FONT>                       <FONT color=3D#bebebe># =
initial value
</FONT>    <FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>othersub</FONT> <FONT color=3D#00ffff>{</FONT>           =
               <FONT color=3D#bebebe># ... accessing $variable
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$counter</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#ffa500>next</FONT>_counter <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#ffa500>return</FONT> ++$counter <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>BEGIN</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$counter</FONT> =3D <FONT color=3D#cdcd00>42</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#ffa500>next</FONT>_counter <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#ffa500>return</FONT> ++$counter <FONT color=3D#00ffff>}</FONT>
    <FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>prev_counter</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#ffa500>return</FONT> --$counter <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=3DAEN548>Determining Current Function =
Name</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>$this_function =3D <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>caller</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>))[</FONT><FONT =
color=3D#cdcd00>3</FONT><FONT color=3D#00ffff>];</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#00ffff>(</FONT>$package, $filename, $line, $subr, =
$has_args, $wantarray <FONT color=3D#00ffff>)</FONT>=3D <FONT =
color=3D#98fb98>caller</FONT><FONT color=3D#00ffff>(</FONT>$i<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#   0         1         2       3       4          =
5
#-----------------------------
</FONT>$me  =3D whoami<FONT color=3D#00ffff>();</FONT>
$him =3D whowasi<FONT color=3D#00ffff>();</FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>whoami</FONT>  =
<FONT color=3D#00ffff>{</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>caller</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>))[</FONT><FONT =
color=3D#cdcd00>3</FONT><FONT color=3D#00ffff>]</FONT> <FONT =
color=3D#00ffff>}</FONT>
<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>whowasi</FONT> =
<FONT color=3D#00ffff>{</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>caller</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>2</FONT><FONT color=3D#00ffff>))[</FONT><FONT =
color=3D#cdcd00>3</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=3DAEN551>Passing Arrays and Hashes by =
Reference</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>array_diff<FONT color=3D#00ffff>(</FONT> \<FONT =
color=3D#cdad00>@array1</FONT>, \<FONT color=3D#cdad00>@array2</FONT> =
<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>@a</FONT> =3D <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>1</FONT>, <FONT =
color=3D#cdcd00>2</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#cdad00>@b</FONT> =3D <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>5</FONT>, <FONT color=3D#cdcd00>8</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#cdad00>@c</FONT> =3D add_vecpair<FONT =
color=3D#00ffff>(</FONT> \<FONT color=3D#cdad00>@a</FONT>, \<FONT =
color=3D#cdad00>@b</FONT> <FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"</FONT><FONT =
color=3D#cdad00>@c</FONT><FONT color=3D#00cd00>\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#cdcd00>6</FONT> <FONT color=3D#cdcd00>10</FONT>
<FONT color=3D#43cd80> </FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>add_vecpair</FONT> <FONT color=3D#00ffff>{</FONT>       =
<FONT color=3D#bebebe># assumes both vectors the same length
</FONT>    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#9ac0cd>$x</FONT>, <FONT =
color=3D#9ac0cd>$y</FONT><FONT color=3D#00ffff>)</FONT> =3D <FONT =
color=3D#cdad00>@_</FONT><FONT color=3D#00ffff>;</FONT>   <FONT =
color=3D#bebebe># copy in the array references
</FONT>    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#cdad00>@result</FONT><FONT color=3D#00ffff>;</FONT>

    <FONT color=3D#ffa500>for</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#ffa500>my</FONT> $i=3D<FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>;</FONT> $i &lt; @$x<FONT color=3D#00ffff>;</FONT> =
$i++<FONT color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
      <FONT color=3D#cdad00>$result</FONT><FONT =
color=3D#00ffff>[</FONT>$i<FONT color=3D#00ffff>]</FONT> =3D =
$x-&gt;<FONT color=3D#00ffff>[</FONT>$i<FONT color=3D#00ffff>]</FONT> + =
$y-&gt;<FONT color=3D#00ffff>[</FONT>$i<FONT color=3D#00ffff>];</FONT>
    <FONT color=3D#00ffff>}</FONT>

    <FONT color=3D#ffa500>return</FONT> <FONT =
color=3D#cdad00>@result</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@_</FONT> =3D=3D <FONT =
color=3D#cdcd00>2</FONT> &amp;&amp; <FONT =
color=3D#98fb98>ref</FONT><FONT color=3D#00ffff>(</FONT>$x<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>eq</FONT> <FONT =
color=3D#00cd00>'ARRAY'</FONT> &amp;&amp; <FONT =
color=3D#98fb98>ref</FONT><FONT color=3D#00ffff>(</FONT>$y<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>eq</FONT> <FONT =
color=3D#00cd00>'ARRAY'</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"usage: =
add_vecpair ARRAYREF1 ARRAYREF2"</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=3DAEN554>Detecting Return Context</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>if</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>wantarray</FONT><FONT =
color=3D#00ffff>())</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># list context
</FONT><FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#ffa500>elsif</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#ff7f50>defined</FONT> <FONT =
color=3D#98fb98>wantarray</FONT><FONT color=3D#00ffff>())</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># scalar context
</FONT><FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#ffa500>else</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># void context
</FONT><FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>wantarray</FONT><FONT =
color=3D#00ffff>())</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"In list =
context\n"</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> <FONT =
color=3D#cdad00>@many_things</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>elsif</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>defined</FONT> <FONT =
color=3D#98fb98>wantarray</FONT><FONT color=3D#00ffff>())</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"In scalar =
context\n"</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> $one_thing<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"In void =
context\n"</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT><FONT color=3D#00ffff>;</FONT>  =
<FONT color=3D#bebebe># nothing
</FONT><FONT color=3D#00ffff>}</FONT>

mysub<FONT color=3D#00ffff>();</FONT>                    <FONT =
color=3D#bebebe># void context
</FONT>
$a =3D mysub<FONT color=3D#00ffff>();</FONT>               <FONT =
color=3D#bebebe># scalar context
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>mysub<FONT color=3D#00ffff>())</FONT> <FONT =
color=3D#00ffff>{</FONT>  <FONT color=3D#00ffff>}</FONT>           <FONT =
color=3D#bebebe># scalar context
</FONT>
<FONT color=3D#cdad00>@a</FONT> =3D mysub<FONT =
color=3D#00ffff>();</FONT>               <FONT color=3D#bebebe># list =
context
</FONT><FONT color=3D#ff7f50>print</FONT> mysub<FONT =
color=3D#00ffff>();</FONT>              <FONT color=3D#bebebe># list =
context
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN557>Passing by Named Parameter</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>thefunc<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>INCREMENT</FONT> =3D&gt; <FONT =
color=3D#00cd00>"20s"</FONT>, <FONT color=3D#00cd00>START</FONT> =3D&gt; =
<FONT color=3D#00cd00>"+5m"</FONT>, <FONT color=3D#00cd00>FINISH</FONT> =
=3D&gt; <FONT color=3D#00cd00>"+30m"</FONT><FONT =
color=3D#00ffff>);</FONT>
thefunc<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>START</FONT> =
=3D&gt; <FONT color=3D#00cd00>"+5m"</FONT>, <FONT =
color=3D#00cd00>FINISH</FONT> =3D&gt; <FONT =
color=3D#00cd00>"+30m"</FONT><FONT color=3D#00ffff>);</FONT>
thefunc<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>FINISH</FONT> =
=3D&gt; <FONT color=3D#00cd00>"+30m"</FONT><FONT =
color=3D#00ffff>);</FONT>
thefunc<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>START</FONT> =
=3D&gt; <FONT color=3D#00cd00>"+5m"</FONT>, <FONT =
color=3D#00cd00>INCREMENT</FONT> =3D&gt; <FONT =
color=3D#00cd00>"15s"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>thefunc</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#cdcd00>%args</FONT> =
=3D <FONT color=3D#00ffff>(</FONT><FONT color=3D#43cd80> </FONT>
        <FONT color=3D#00cd00>INCREMENT</FONT>   =3D&gt; <FONT =
color=3D#00cd00>'10s'</FONT>,<FONT color=3D#43cd80> </FONT>
        <FONT color=3D#00cd00>FINISH</FONT>      =3D&gt; <FONT =
color=3D#cdcd00>0</FONT>,<FONT color=3D#43cd80> </FONT>
        <FONT color=3D#00cd00>START</FONT>       =3D&gt; <FONT =
color=3D#cdcd00>0</FONT>,<FONT color=3D#43cd80> </FONT>
        <FONT color=3D#cdad00>@_</FONT>,         <FONT color=3D#bebebe># =
argument pair list goes here
</FONT>    <FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>$args</FONT><FONT color=3D#00ffff>{</FONT><FONT =

⌨️ 快捷键说明

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