Perl Programming/Keywords/quotemeta

Previous: qr Keywords Next: qw

The quotemeta keyword

edit

The quotemeta command returns the value of EXPRESSION such that all the ASCII non-"word" characters (that do not match with [A-Za-z_0-9]) are backslashed. This is the internal function implementing the \Q escape in double-quoted strings. If EXPRESSION is not passed, the function uses $_ instead.

Syntax

edit
  quotemeta EXPRESSION

Examples

edit
  The code
my $sentence = 'The quick brown fox jumpes over and over again.';
my $substring = 'quick.*?fox';
my $quoted_substring = quotemeta($substring);

$sentence =~ s{$quoted_substring}{big bad wolf};

print '"', $sentence, "\"\n\"", $substring, "\"\n\"", $quoted_substring, "\"\n";
returns the following:
"The quick brown fox jumpes over and over again."
"quick.*?fox"
"quick\.\*\?fox"


Previous: qr Keywords Next: qw