perlfaq4.html

来自「perl教程」· HTML 代码 · 共 867 行 · 第 1/5 页

HTML
867 行
字号
Julian day)</p>
<pre>
  $ perl -MDateTime -le'print DateTime-&gt;today-&gt;doy'
  31</pre>
<p>
</p>
<h2><a name="how_do_i_find_yesterday_s_date">How do I find yesterday's date?</a></h2>
<p>(contributed by brian d foy)</p>
<p>Use one of the Date modules. The <code>DateTime</code> module makes it simple, and
give you the same time of day, only the day before.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">DateTime</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">$yesterday</span> <span class="operator">=</span> <span class="variable">DateTime</span><span class="operator">-&gt;</span><span class="variable">now</span><span class="operator">-&gt;</span><span class="variable">subtract</span><span class="operator">(</span> <span class="string">days</span> <span class="operator">=&gt;</span> <span class="number">1</span> <span class="operator">);</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="string">"Yesterday was $yesterday\n"</span><span class="operator">;</span>
</pre>
<p>You can also use the <code>Date::Calc</code> module using its Today_and_Now
function.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Date::Calc</span> <span class="string">qw( Today_and_Now Add_Delta_DHMS )</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">@date_time</span> <span class="operator">=</span> <span class="variable">Add_Delta_DHMS</span><span class="operator">(</span> <span class="variable">Today_and_Now</span><span class="operator">(),</span> <span class="operator">-</span><span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span> <span class="operator">);</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="string">"@date\n"</span><span class="operator">;</span>
</pre>
<p>Most people try to use the time rather than the calendar to figure out
dates, but that assumes that days are twenty-four hours each.  For
most people, there are two days a year when they aren't: the switch to
and from summer time throws this off. Let the modules do the work.</p>
<p>
</p>
<h2><a name="does_perl_have_a_year_2000_problem_is_perl_y2k_compliant">Does Perl have a Year 2000 problem?  Is Perl Y2K compliant?</a></h2>
<p>Short answer: No, Perl does not have a Year 2000 problem.  Yes, Perl is
Y2K compliant (whatever that means).  The programmers you've hired to
use it, however, probably are not.</p>
<p>Long answer: The question belies a true understanding of the issue.
Perl is just as Y2K compliant as your pencil--no more, and no less.
Can you use your pencil to write a non-Y2K-compliant memo?  Of course
you can.  Is that the pencil's fault?  Of course it isn't.</p>
<p>The date and time functions supplied with Perl (gmtime and localtime)
supply adequate information to determine the year well beyond 2000
(2038 is when trouble strikes for 32-bit machines).  The year returned
by these functions when used in a list context is the year minus 1900.
For years between 1910 and 1999 this <em>happens</em> to be a 2-digit decimal
number. To avoid the year 2000 problem simply do not treat the year as
a 2-digit number.  It isn't.</p>
<p>When <a href="../../lib/Pod/perlfunc.html#item_gmtime"><code>gmtime()</code></a> and <a href="../../lib/Pod/perlfunc.html#item_localtime"><code>localtime()</code></a> are used in scalar context they return
a timestamp string that contains a fully-expanded year.  For example,
<a href="../../lib/Pod/perlfunc.html#item_gmtime"><code>$timestamp = gmtime(1005613200)</code></a> sets $timestamp to &quot;Tue Nov 13 01:00:00
2001&quot;.  There's no year 2000 problem here.</p>
<p>That doesn't mean that Perl can't be used to create non-Y2K compliant
programs.  It can.  But so can your pencil.  It's the fault of the user,
not the language.  At the risk of inflaming the NRA: &quot;Perl doesn't
break Y2K, people do.&quot;  See <a href="http://www.perl.org/about/y2k.html">http://www.perl.org/about/y2k.html</a> for
a longer exposition.</p>
<p>
</p>
<hr />
<h1><a name="data__strings">Data: Strings</a></h1>
<p>
</p>
<h2><a name="how_do_i_validate_input">How do I validate input?</a></h2>
<p>(contributed by brian d foy)</p>
<p>There are many ways to ensure that values are what you expect or
want to accept. Besides the specific examples that we cover in the
perlfaq, you can also look at the modules with &quot;Assert&quot; and &quot;Validate&quot;
in their names, along with other modules such as <code>Regexp::Common</code>.</p>
<p>Some modules have validation for particular types of input, such
as <code>Business::ISBN</code>, <code>Business::CreditCard</code>, <code>Email::Valid</code>,
and <code>Data::Validate::IP</code>.</p>
<p>
</p>
<h2><a name="how_do_i_unescape_a_string">How do I unescape a string?</a></h2>
<p>It depends just what you mean by &quot;escape&quot;.  URL escapes are dealt
with in <a href="../../lib/Pod/perlfaq9.html">the perlfaq9 manpage</a>.  Shell escapes with the backslash (<code>\</code>)
character are removed with</p>
<pre>
    <span class="regex">s/\\(.)/$1/g</span><span class="operator">;</span>
</pre>
<p>This won't expand <code>&quot;\n&quot;</code> or <code>&quot;\t&quot;</code> or any other special escapes.</p>
<p>
</p>
<h2><a name="how_do_i_remove_consecutive_pairs_of_characters">How do I remove consecutive pairs of characters?</a></h2>
<p>(contributed by brian d foy)</p>
<p>You can use the substitution operator to find pairs of characters (or
runs of characters) and replace them with a single instance. In this
substitution, we find a character in <code>(.)</code>. The memory parentheses
store the matched character in the back-reference <code>\1</code> and we use
that to require that the same thing immediately follow it. We replace
that part of the string with the character in <code>$1</code>.</p>
<pre>
    <span class="regex">s/(.)\1/$1/g</span><span class="operator">;</span>
</pre>
<p>We can also use the transliteration operator, <a href="../../lib/Pod/perlfunc.html#item_tr_"><code>tr///</code></a>. In this
example, the search list side of our <a href="../../lib/Pod/perlfunc.html#item_tr_"><code>tr///</code></a> contains nothing, but
the <code>c</code> option complements that so it contains everything. The
replacement list also contains nothing, so the transliteration is
almost a no-op since it won't do any replacements (or more exactly,
replace the character with itself). However, the <a href="../../lib/Pod/perlguts.html#item_s"><code>s</code></a> option squashes
duplicated and consecutive characters in the string so a character
does not show up next to itself</p>
<pre>
        <span class="keyword">my</span> <span class="variable">$str</span> <span class="operator">=</span> <span class="string">'Haarlem'</span><span class="operator">;</span>   <span class="comment"># in the Netherlands</span>
            <span class="variable">$str</span> <span class="operator">=~</span> <span class="regex">tr///cs</span><span class="operator">;</span>       <span class="comment"># Now Harlem, like in New York</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_expand_function_calls_in_a_string">How do I expand function calls in a string?</a></h2>
<p>(contributed by brian d foy)</p>
<p>This is documented in <a href="../../lib/Pod/perlref.html">the perlref manpage</a>, and although it's not the easiest
thing to read, it does work. In each of these examples, we call the
function inside the braces used to dereference a reference. If we
have a more than one return value, we can construct and dereference an
anonymous array. In this case, we call the function in list context.</p>
<pre>
        <span class="keyword">print</span> <span class="string">"The time values are @{ [localtime] }.\n"</span><span class="operator">;</span>
</pre>
<p>If we want to call the function in scalar context, we have to do a bit
more work. We can really have any code we like inside the braces, so
we simply have to end with the scalar reference, although how you do
that is up to you, and you can use code inside the braces.</p>
<pre>
        <span class="keyword">print</span> <span class="string">"The time is ${\(scalar localtime)}.\n"</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="string">"The time is ${ my $x = localtime; \$x }.\n"</span><span class="operator">;</span>
</pre>
<p>If your function already returns a reference, you don't need to create
the reference yourself.</p>
<pre>
        <span class="keyword">sub</span><span class="variable"> timestamp </span><span class="operator">{</span> <span class="keyword">my</span> <span class="variable">$t</span> <span class="operator">=</span> <span class="keyword">localtime</span><span class="operator">;</span> <span class="operator">\</span><span class="variable">$t</span> <span class="operator">}</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="string">"The time is ${ timestamp() }.\n"</span><span class="operator">;</span>
</pre>
<p>The <code>Interpolation</code> module can also do a lot of magic for you. You can
specify a variable name, in this case <a href="../../lib/Pod/perlguts.html#item_e"><code>E</code></a>, to set up a tied hash that
does the interpolation for you. It has several other methods to do this
as well.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Interpolation</span> <span class="string">E</span> <span class="operator">=&gt;</span> <span class="string">'eval'</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="string">"The time values are $E{localtime()}.\n"</span><span class="operator">;</span>
</pre>
<p>In most cases, it is probably easier to simply use string concatenation,
which also forces scalar context.</p>
<pre>
        <span class="keyword">print</span> <span class="string">"The time is "</span> <span class="operator">.</span> <span class="keyword">localtime</span> <span class="operator">.</span> <span class="string">".\n"</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_find_matching_nesting_anything">How do I find matching/nesting anything?</a></h2>
<p>This isn't something that can be done in one regular expression, no
matter how complicated.  To find something between two single
characters, a pattern like <a href="../../lib/Pod/perlguts.html#item_x"><code>/x([^x]*)x/</code></a> will get the intervening
bits in $1. For multiple ones, then something more like
<code>/alpha(.*?)omega/</code> would be needed.  But none of these deals with
nested patterns.  For balanced expressions using <code>(</code>, <code>{</code>, <code>[</code> or
<code>&lt;</code> as delimiters, use the CPAN module Regexp::Common, or see
<a href="../../lib/Pod/perlre.html#___code___">(??{ code }) in the perlre manpage</a>.  For other cases, you'll have to write a
parser.</p>
<p>If you are serious about writing a parser, there are a number of
modules or oddities that will make your life a lot easier.  There are
the CPAN modules Parse::RecDescent, Parse::Yapp, and Text::Balanced;
and the byacc program.   Starting from perl 5.8 the Text::Balanced is
part of the standard distribution.</p>
<p>One simple destructive, inside-out approach

⌨️ 快捷键说明

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