[Edinburgh-pm] quotemeta

Stephen Quinney stephen at jadevine.org.uk
Mon May 26 01:17:14 PDT 2008


On Mon, May 26, 2008 at 12:56 AM,  <asmith9983 at gmail.com> wrote:
> I thought I'd be smart and use quotemeta in a Small piece of code, but the
> results weren't the same:-
>
> my $domain1=quotemeta "192.1.1.";
> my $domain2= "192\.1\.1\.";
>

The difference between these two statements is because you have used
double-quotes for the second so the backslashes get consumed
immediately. This shows the problem:

$ perl -e '$domain2 = "192\.1\.1\."; print $domain2 . "\n"'
192.1.1.

You can either escape them with a second backslash:

$ perl -e '$domain2 = "192\\.1\\.1\\."; print $domain2 . "\n"'
192\.1\.1\.

or use single-quotes, (or the quote-like operator):

perl -e '$domain2 = q{192\.1\.1\.}; print $domain2 . "\n"'
192\.1\.1\.

When putting values into a regular expression I often just do the
escaping of any metadata characters within the expression. For
instance:

m/\Q$domain2\E(\d{1,3})/;


I hope that helps.

Stephen


More information about the Edinburgh-pm mailing list