I am trying to execute a MongoDB query as below :
$Collection1->update({"_id":\$id}, { \$set: {"Title":$title} }, false );
But I get below error-
Global symbol "$set" requires explicit package name at file.pl line xx.
As per MongoDB documentation, \$set is supposed to work just fine. What could be wrong here?
You can use the simple single quotes, which in Perl will NOT interpolate the value inside. Then your ‘$’ will be passed as just part of the string instead of trying to interpolate a value for a variable.
Note in the example above I also changed your double quotes around the field names to be single quotes also. It’s good practice in Perl to use single quotes when you mean a string literal, and double quotes when you want to replace with the value of a variable.
Also, you don’t want (or need) to escape the ‘$’ in front of the ‘$id’ as, you do want the id variable to be replaced in the update call.
the “qw()” operator Paul showed does the same thing. See “Quote and Quote‐like Operators” in the perldoc perlop section.
edited answer to reflect other comments about the improper hash notations.