Reading perl sources I saw many times the next construction:
printf qq[%s\n], getsomestring( $_ );
But usually it is written as
printf "%s\n", getsomestring( $_ );
The question:
qq[...]
vs the "..."
The perlop doesn't mention anything about this.
You can use qq()
as an alternative double quote method, such as when you have double quotes in the string. For example:
"\"foo bar\""
Looks better when written
qq("foo bar")
When in the windows cmd shell, which uses double quotes, I often use qq()
when I need interpolation. For example:
perl -lwe "print qq($foo\n)"
The qq()
operator -- like many other perl operators such as s///
, qx()
-- is also handy as you demonstrate because it can use just about any character as its delimiter:
qq[this works]
qq|as does this|
qq#or this#
This is handy for when you have many different delimiters in the string. For example:
qq!This is (not) "hard" to quote!
As for best practice, I would say use whatever is more readable.
I always use qq[...]
when there are quotes in the strings, example:
qq["here you are", he said]
If not, for me is more readable the use of ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With