Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using quote-like-operators or quotes in the perl's printf

Tags:

perl

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:

  • is here any "good practice" what is the correct way, and if yes
  • when is recommended to use the longer qq[...] vs the "..."
  • or it is only pure TIMTOWTDI?

The perlop doesn't mention anything about this.

like image 813
jm666 Avatar asked Feb 17 '23 15:02

jm666


2 Answers

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.

like image 107
TLP Avatar answered Feb 24 '23 23:02

TLP


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 ""

like image 20
Miguel Prz Avatar answered Feb 25 '23 00:02

Miguel Prz