quotemeta - perldoc.perl.org

Perl Programming Documentation
Download Perl
Explore

Manual

- Overview
- Tutorials
- FAQs
- History / Changes
- License

Reference

- Language
- Functions
- Operators
- Special Variables
- Pragmas
- Utilities
- Internals
- Platform Specific

Modules

- A • B • C • D • E
- F • G • H • I • L
- M • N • O • P • S
- T • U • X

Tools

- Preferences

# quotemeta

Perl 5 version 12.2 documentation
Go to top
Show recent pages
Recently read

# quotemeta

Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage

- quotemeta EXPR

- quotemeta

  Returns the value of EXPR with all non-"word" characters backslashed. (That is, all characters not matching `/[A-Za-z_0-9]/` will be preceded by a backslash in the returned string, regardless of any locale settings.) This is the internal function implementing the `\Q` escape in double-quoted strings.

  If EXPR is omitted, uses `$_` .

  quotemeta (and `\Q` ... `\E` ) are useful when interpolating strings into regular expressions, because by default an interpolated variable will be considered a mini-regular expression. For example:

          my $sentence = 'The quick brown fox jumped over the lazy dog';    my $substring = 'quick.*?fox';    $sentence =~ s{$substring}{big bad wolf};

  Will cause `$sentence` to become `'The big bad wolf jumped over...'` .

  On the other hand:

          my $sentence = 'The quick brown fox jumped over the lazy dog';    my $substring = 'quick.*?fox';    $sentence =~ s{\Q$substring\E}{big bad wolf};

  Or:

          my $sentence = 'The quick brown fox jumped over the lazy dog';    my $substring = 'quick.*?fox';    my $quoted_substring = quotemeta($substring);    $sentence =~ s{$quoted_substring}{big bad wolf};

  Will both leave the sentence as is. Normally, when accepting string input from the user, quotemeta() or `\Q` must be used.