Perl Function: quotemeta

Escape all non-word characters with a '\' character.

quotemeta.pl

my $s = '';

# Build a string holding characters 32 to 126
for( 32..126 )
{
    $s .= chr;
}

# Print the string and then show it again with all non-"word" characters backslashed.
print "$s\n";
printf( "%s\n", quotemeta $s ); # $_ would be used if $s was ommited.

# Now try and do the same as above with a pattern match and substitute.
print "$s\n";
$s =~ s/([^A-Za-z0-9_])/\\$1/g;
print "$s\n";
quotemeta.out

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
\ \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/0123456789\:\;\<\=\>\?\@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]\^_\`abcdefghijklmnopqrstuvwxyz\{\|\}\~
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
\ \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/0123456789\:\;\<\=\>\?\@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]\^_\`abcdefghijklmnopqrstuvwxyz\{\|\}\~

See Also: perldoc


Roger
Roger Hall
COMPUSPEC
Google
 
Web Compuspec.net
Valid XHTML 1.0!
Valid CSS!