I am trying to run the following code:
$lines = "Enjoyable )) DAY";
$lines =~ lc $lines;
print $lines;
It fails on the second line where I get the error mentioned in the title. I understand the brackets are causing the trouble. I think I could use "quotemeta", but the thing is that my string contains info that I go on to process later, so I would like to keep the string intact as far as possible and not tamper with it too much.
You have two problems here.
1. =~
is used to execute a specific set of operations
The =~
operator is used to either match with //
, m//
, qr//
or a string; or to substitute with s///
or tr///
.
If all you want to do is lowercase the contents of $lines
then you should use =
not =~
.
$lines = "Enjoyable )) DAY";
$lines = lc $lines;
print $lines;
2. Regular expressions have special characters which must be escaped
If you want to match $lines
against a lower case version of $Lines
, which should return true if $lines
was already entirely lower case and false otherwise, then you need to escape the ")" characters.
#!/usr/bin/env perl
use strict;
use warnings;
my $lines = "enjoyable )) day";
if ($lines =~ lc quotemeta $lines) {
print "lines is lower case\n";
}
print $lines;
Note this is a toy example trying to find a reason for doing $lines =~ lc $lines
- It would be much better (faster, safer) to solve this with eq
as in $lines eq lc $lines
.
See perldoc -f quotemeta
or http://perldoc.perl.org/functions/quotemeta.html for more details on quotemeta.