in reply to Re: Make a variable safe for a regex.
in thread Make a variable safe for a regex.

Well, they appear to do the same, at least in the one-byte character range:
my $str = join '', map chr, 0 .. 255; my $qm = quotemeta( $str); ( my $kyle = $str) =~ s/(\W)/\\$1/g; my $quoted_qm = join '', $qm =~ /\\(.)/g; my $quoted_kyle = join '', $kyle =~ /\\(.)/g; printf "quotemeta: %d, kyle: %d (%s)\n", length $quoted_qm, length $quoted_kyle, $quoted_qm eq $quoted_kyle ? "same" : "differernt";
That prints
quotemeta: 192, kyle: 192 (same)
Anno

Replies are listed 'Best First'.
Re^3: Make a variable safe for a regex.
by diotalevi (Canon) on Feb 26, 2007 at 00:47 UTC

    \Q and quotemeta() are the same function.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      True, but kyle's alternative (which I compared with quotemeta) isn't based on \Q.

      Anno