mirod said:
Can't you just use any character (here the space) to delimit a regexp?
That's not happening here:
print "Good\n" if $test =~ ab;
There is no space to the right of the regex. If anything, it would be a word boundary as the delimeter (\b in a regex). This appears to be, as merlyn mentioned, a bareword being promoted to a string and then to a regex.

mirod said:

This code actually does not pass -w and use strict on my Solaris/Perl 5.6 combo.

Are you sure ab is not pre-compiled or some similar trickery?

Nope, it's not precompiled. I just got home (I'm home early due to being sick as a dog. Don't lick my posts, I wouldn't want you to get ill) and tried it on my Win98 box and again, it runs fine.

Naturally, I'm curious as to the performance aspects, so I decided to benchmark this.

#!/usr/bin/perl -w use strict; use Benchmark; my $test; #print "Good\n" if $test =~ ab; timethese(-15, { bare => '$test = "aba"; $test =~ ab', delimited => '$test = "aba"; $test =~ /ab/' });
Whups! All of a sudden, I get a whole slew of messages like the following:
Unquoted string "ab" may clash with future reserved word at (eval 1) l +ine 1.
Somehow, eval is catching the problem. By removing the -w switch, the code ran fine (but still works with strict, go figure).

Incidentally, the benchmark results showed no significant performance impact:

Benchmark: running bare, delimited, each for at least 15 CPU seconds.. +. bare: 13 wallclock secs (15.01 usr + 0.00 sys = 15.01 CPU) @ 45 +7834.84/s (n=6872101) delimited: 16 wallclock secs (15.00 usr + 0.00 sys = 15.00 CPU) @ 45 +8124.53/s (n=6871868)
I want to explore this more and see just how complicated of a regex I can make here, but I need to get some sleep.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just go the the link and check out our stats.


In reply to (Ovid - benchmarking a bare regex) RE: Odd... by Ovid
in thread Odd... by Ovid

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.