http://qs321.pair.com?node_id=910796


in reply to Re^6: The $_ Special Variable
in thread The $_ Special Variable

What do the strict and warnings commands do?

use strict requires that all variables be declared with either my or our. This is the number one thing you can do to ensure you're code is well made and without errors. use warnings is essentially an extension that monitors your code for unintended errors.

And what is the purpose of my before the objects? What does it do?

First of all, those aren't objects, they're simple variables. my is used to declare your variables and is useful for limiting their scope. If you were to mistype a variable or use the wrong type, use strict would alert you to the error

Also, what is the purpose of my $string = shift;?

If you read the documentation for shift, you'll see that in this instance it's equivalent to:

my $string = shift @ARGV;

Previously, you hardcoded $ARGV[0] in multiple places. It's much better to assign your script's parameters to variables as a way of documenting your code.

I don't understand the if (s/\Q$string\E/($string)/g) { conditional either.

\Q...\E is a shortcut syntax for quotemeta. This escaped any regex special characters in your $string, so that it would be treated as a literal value.