in reply to printf x format ?

Perl is not C. Though I occasionally use sprintf in Perl, I rarely use printf, whose doco warns:

Don't fall into the trap of using a printf when a simple print would do. The print is more efficient and less error prone.

Hope the following test program clarifies:

use strict; use warnings; my $ca = 'A'; print "$ca\n"; # outputs A printf "%s\n", $ca; # outputs A printf "%c\n", ord($ca); # outputs A printf "%x\n", ord($ca); # outputs 41 printf "%d\n", ord($ca); # outputs 65

See also: ord, chr, hex, oct, sprintf

Replies are listed 'Best First'.
Re^2: printf x format ? -- solved
by pgmer6809 (Sexton) on Mar 03, 2022 at 03:30 UTC

    Thanks. That clears it up nicely. I read the bit about not using printf when print will do, but I did not know to look for ord. I was hoping that by specifying $ca with single quotes, instead of double quotes Perl would not treat $ca as a string. As the other reply hinted, I have been doing C recently, and Perl is not C. Thanks again. pgmer6809