Using quotation operators or quotation marks in perl printf

Reading perl sources I have seen the following construction many times:

printf qq[%s\n], getsomestring( $_ ); 

But usually it is written as

 printf "%s\n", getsomestring( $_ ); 

Question:

  • is there any β€œgood practice” that is the right way and if so
  • when it is recommended to use longer qq[...] and "..."
  • or is it just pure TIMTOWTDI?

perlop says nothing about this.

+4
source share
2 answers

You can use qq() as an alternative double quote method, for example, when you have double quotes in a string. For instance:

 "\"foo bar\"" 

Looks better when written

 qq("foo bar") 

When in a windows cmd command shell that uses double quotes, I often use qq() when I need interpolation. For instance:

 perl -lwe "print qq($foo\n)" 

The qq() operator - like many other perl operators such as s/// , qx() - is also convenient, as you demonstrate, because it can use only any character as a separator:

 qq[this works] qq|as does this| qq#or this# 

This is useful when you have many different delimiters per line. For instance:

 qq!This is (not) "hard" to quote! 

As for best practice, I would say that I use what is more readable.

+6
source

I always use qq[...] when there are quotation marks in strings, for example:

 qq["here you are", he said] 

If not, the use of "" more readable to me ""

+2
source

Source: https://habr.com/ru/post/1479106/


All Articles