![]() |
Practical mod_perl / HTML Book / | ![]() |
||
![]() |
||||
![]() |
![]() |
|||
![]() |
![]() |
|||
![]() |
||||
|
|
||||
![]() |
![]() |
|||
![]() |
6.4.7. Formats |
![]() |
||
![]() |
||||
![]() |
![]() |
![]() |
||
![]() |
||||
The interface to file handles that are linked to variables with Perl's tie( ) function is not yet complete. The format( ) and write( ) functions are missing. If you configure Perl with sfio, write( ) and format( )should work just fine.
Instead of format( ), you can use printf( ). For example, the following formats are equivalent:
format printf --------------- ##.## %2.2f ####.## %4.2f
To print a string with fixed-length elements, use the printf( ) format %n.ms where n is the length of the field allocated for the string and m is the maximum number of characters to take from the string. For example:
printf "[%5.3s][%10.10s][%30.30s]\n",
12345, "John Doe", "1234 Abbey Road"
prints:
[ 123][ John Doe][ 1234 Abbey Road]
Notice that the first string was allocated five characters in the output, but only three were used because m=5 and n=3 (%5.3s). If you want to ensure that the text will always be correctly aligned without being truncated, n should always be greater than or equal to m.
You can change the alignment to the left by adding a minus sign (-) after the %. For example:
printf "[%-5.5s][%-10.10s][%-30.30s]\n",
123, "John Doe", "1234 Abbey Road"
prints:
[123 ][John Doe ][1234 Abbey Road ]
You can also use a plus sign (+) for the right-side alignment. For example:
printf "[%+5s][%+10s][%+30s]\n",
123, "John Doe", "1234 Abbey Road"
prints:
[ 123][ John Doe][ 1234 Abbey Road]
Another alternative to format( ) and printf( ) is to use the Text::Reform module from CPAN.
In the examples above we've printed the number 123 as a string (because we used the %s format specifier), but numbers can also be printed using numeric formats. See perldoc -f sprintf for full details.
 
Continue to:
mod_perl, modperl, Apache, perl, cgi, html, mod_perl, e-commerce, scalability, free, open source, OSS, apache, squid, high availability, modperl, linux, unix, Web, www, mod_perl, webserver, admin, apache, book, webmaster, tools, modperl, guide, docs, documentation, help, mod_perl, perl, information, apache, script, errata, eric cholet, perl, apache, mod-perl, stas bekman, mod_perl, cool, perl, Apache, performance, speed, choice
![]() |
Other projects to check out: meta-religion.com is for those interested in Religious, Spiritual and Esoteric Phenomena. i-want-a-better.com is a community of people discussing what they would like to be improved in their lives and things they use and interact with. You may also want to find a healer in your area or read articles on variety of topics.