The Printf and Sprintf Functions
#!/usr/bin/perl -w
###################### Printf/Sprintf Demo ########################
$ruler_line = "1234567890"; $ruler_line x= 5;
print "$ruler_line\n","-" x 50,"\n";
$var = "hello";
printf "%10s\n%-10s\n", $var, $var;
$var = "64";
printf "%05d\n%5d\n%-5d\n%5o\n%5x\n%5.2f\n",
$var, $var, $var, $var, $var, $var;
print "\n$ruler_line\n","-" x 50, "\n";
$var = 987.12345;
$width = 13;
$right = 4;
printf("%12.3f\n%-12.3f\n%12.5e\n%*.*f\n", #### * not "official" perl.
$var, $var, $var, $width, $right, $var);
$var = "abracadabra";
printf "%.5s\n", $var;
printf "%10.5s\n", $var;
printf "%5s\n", $var; ###### Prints the whole string anyway!
$var = "123456789012345";
printf "%d\n", $var; ###### Integer too big for %d or %ld!!
printf "%ld\n",$var;
print "\n$ruler_line\n","-" x 50,"\n";
$var1 = "123";
$var2 = "511";
$formatted = sprintf "%07d Howdy! %6o", $var1, $var2;
print "$formatted\n";
######################### Program Output Below ##########################
12345678901234567890123456789012345678901234567890
--------------------------------------------------
hello ####### %10s
hello ####### %-10s
00064 ####### %05d
64 ####### %5d
64 ####### %-5d
100 ####### %5o
40 ####### %5x
64.00 ####### %5.2f
12345678901234567890123456789012345678901234567890
--------------------------------------------------
987.123 ####### %12.3f
987.123 ####### %-12.3f
9.87123e+02 ####### %12.5e
987.1235 ####### %13.4f using variable format specifiers.
abrac ####### %.5s of abracadabra
abrac ####### %10.5s of abracadabra
abracadabra ####### %5s of abracadabra
-2045911175 ####### %d of 123456789012345
-2045911175 ####### %ld of the same number
12345678901234567890123456789012345678901234567890
--------------------------------------------------
0000123 Howdy! 777 ###### sprintf formatted string.