std.html
来自「perl教程」· HTML 代码 · 共 103 行
HTML
103 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>getopts - Process single-character switches with switch clustering</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>getopts - Process single-character switches with switch clustering</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#help_and_version"><code>-help</code> and <code>-version</code></a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>getopt, getopts - Process single-character switches with switch clustering</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="variable">Getopt::Std</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">getopt</span><span class="operator">(</span><span class="string">'oDI'</span><span class="operator">);</span> <span class="comment"># -o, -D & -I take arg. Sets $opt_* as a side effect.</span>
<span class="variable">getopt</span><span class="operator">(</span><span class="string">'oDI'</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">%opts</span><span class="operator">);</span> <span class="comment"># -o, -D & -I take arg. Values in %opts</span>
<span class="variable">getopts</span><span class="operator">(</span><span class="string">'oif:'</span><span class="operator">);</span> <span class="comment"># -o & -i are boolean flags, -f takes an argument</span>
<span class="comment"># Sets $opt_* as a side effect.</span>
<span class="variable">getopts</span><span class="operator">(</span><span class="string">'oif:'</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">%opts</span><span class="operator">);</span> <span class="comment"># options as above. Values in %opts</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The <code>getopt()</code> function processes single-character switches with switch
clustering. Pass one argument which is a string containing all switches
that take an argument. For each switch found, sets $opt_x (where x is the
switch name) to the value of the argument if an argument is expected,
or 1 otherwise. Switches which take an argument don't care whether
there is a space between the switch and the argument.</p>
<p>The <code>getopts()</code> function is similar, but you should pass to it the list of all
switches to be recognized. If unspecified switches are found on the
command-line, the user will be warned that an unknown option was given.</p>
<p>Note that, if your code is running under the recommended <code>use strict
'vars'</code> pragma, you will need to declare these package variables
with "our":</p>
<pre>
<span class="keyword">our</span><span class="operator">(</span><span class="variable">$opt_x</span><span class="operator">,</span> <span class="variable">$opt_y</span><span class="operator">);</span>
</pre>
<p>For those of you who don't like additional global variables being created, <code>getopt()</code>
and <code>getopts()</code> will also accept a hash reference as an optional second argument.
Hash keys will be x (where x is the switch name) with key values the value of
the argument or 1 if no argument is specified.</p>
<p>To allow programs to process arguments that look like switches, but aren't,
both functions will stop processing switches when they see the argument
<code>--</code>. The <code>--</code> will be removed from @ARGV.</p>
<p>
</p>
<hr />
<h1><a name="help_and_version"><code>--help</code> and <code>--version</code></a></h1>
<p>If <code>-</code> is not a recognized switch letter, <code>getopts()</code> supports arguments
<code>--help</code> and <code>--version</code>. If <code>main::HELP_MESSAGE()</code> and/or
<code>main::VERSION_MESSAGE()</code> are defined, they are called; the arguments are
the output file handle, the name of option-processing package, its version,
and the switches string. If the subroutines are not defined, an attempt is
made to generate intelligent messages; for best results, define $main::VERSION.</p>
<p>If embedded documentation (in pod format, see <a href="../../lib/Pod/perlpod.html">the perlpod manpage</a>) is detected
in the script, <code>--help</code> will also show how to access the documentation.</p>
<p>Note that due to excessive paranoia, if $Getopt::Std::STANDARD_HELP_VERSION
isn't true (the default is false), then the messages are printed on STDERR,
and the processing continues after the messages are printed. This being
the opposite of the standard-conforming behaviour, it is strongly recommended
to set $Getopt::Std::STANDARD_HELP_VERSION to true.</p>
<p>One can change the output file handle of the messages by setting
$Getopt::Std::OUTPUT_HELP_VERSION. One can print the messages of <code>--help</code>
(without the <code>Usage:</code> line) and <code>--version</code> by calling functions <code>help_mess()</code>
and <code>version_mess()</code> with the switches string as an argument.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?