IMHO TMTOWTDI
if (uc $error eq "ERROR"){ # . . . }
or
if (lc $error eq "error"){ # . . . }
Although, I would prefer Zaxo's first example because it seems more efficient (if for no other reason, but conservation of code :-)
Note that this is not strictly equivalent to Zaxo's code since /error/i checks (case insensitively) for the presence of the string 'error' and not for equality to it (up to case insentiveness).

More importantly: "more efficient"? Well, I premise that I'd probably use /error/i or /^error$/i (this really depends on what can there be in $_) myself in most reasonable cases, but running

#!/usr/bin/perl use strict; use warnings; use Benchmark qw/:all :hireswallclock/; my $test_str='error'; my @test=map { my @chr=("\0", " "); local $_=$test_str; s/./$& ^ $chr[rand 2]/ge} 1..100; no warnings 'void'; cmpthese -30, { regex => sub { /^\Q$test_str\E$/i for @test }, lc => sub { $test_str eq lc for @test} }, 'all'; __END__
I get
Benchmark: running lc, regex for at least 30 CPU seconds... lc: 34.068 wallclock secs (34.10 usr 0.00 sys + 0.00 cusr 0 +.00 csys = 34.10 CPU) @ 19917.24/s (n=679178) regex: 31.518 wallclock secs (31.53 usr 0.00 sys + 0.00 cusr 0 +.00 csys = 31.53 CPU) @ 13689.50/s (n=431630) Rate regex lc regex 13690/s -- -31% lc 19917/s 45% --
and
Benchmark: running lc, regex for at least 30 CPU seconds... lc: 31.4462 wallclock secs (31.45 usr 0.00 sys + 0.00 cusr +0.00 csys = 31.45 CPU) @ 26138.57/s (n=822058) regex: 31.8579 wallclock secs (31.86 usr 0.00 sys + 0.00 cusr +0.00 csys = 31.86 CPU) @ 14326.49/s (n=456442) Rate regex lc regex 14326/s -- -45% lc 26139/s 82% --
under Windows 98 and Linux (kernel 2.6.10) respectively. Perl 5.8.6 in both cases...

In reply to Re^2: Match upper and lower case by blazar
in thread Match upper and lower case by benlaw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.