SPUG: double quotes vs printf

Bill Campbell bill at celestial.com
Thu Jun 21 21:47:46 PDT 2007


On Thu, Jun 21, 2007, Michael R. Wolf wrote:
>When I've got to print a long string with variable interpolation, I often
>rewrite
>
>print " Lorem ipsum dolor sit $var1, consectetuer adipiscing $var2. In
>felis";
>
>with
>
>  printf 'Lorem ipsum dolor sit %s, consectetuer adipiscing %s. In felis',
>          $var1, $var2;
>
>or even
>
>  my $fmt = 'Lorem ipsum dolor sit %s, consectetuer adipiscing %s. In felis'
>  printf $fmt, $var1, $var2;
>
>I think it separates the presentation from the content.  I can save the
>older/stupider (i.e. me, later) reader from having to parse and interpolate
>the format string by factoring it out of the print, and signaling the same
>by surrounding it with single (non-interpolating) quotes.

Personally I prefer using the variable expansion in perl as I
find it easier to see what's being done than to have to match the
format string with the variables in the argument list.

If one wants to use more detailled format specs (e.g. %8.2f),
variable expansion doesn't work unless the variable contains the
formatted string.

Bill
--
INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

``Our Foreign dealings are an Open Book, generally a Check Book.''
    Will Rogers


More information about the spug-list mailing list