Jump to content

Perl Programming/Keywords/quotemeta

From Wikibooks, open books for an open world
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Previous: qr Keywords Next: qw

The quotemeta keyword

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

 quotemeta EXPRESSION 

Examples

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