http://www.perlmonks.org?node_id=33195


in reply to Odd...

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.