Re: Why SprintF?
by japhy (Canon) on Jan 27, 2006 at 17:42 UTC
|
There are times when you don't WANT to print the formatted string, but store it somewhere. That's why sprintf() exists.
| [reply] |
Re: Why SprintF?
by wazzuteke (Hermit) on Jan 27, 2006 at 17:49 UTC
|
The first comment is really the basis of the issue. sprintf will let you store off the results into a variable for later use, while printf will simply wrap the results and send them to STDOUT (or the CGI page).
For example:
# Using printf
printf( "%100s\n", 'This will just print to STDOUT; will not return ot
+herwise.' );
# Using sprintf
my $var = sprintf( "%100s\n", 'This will be stored off to $var, where
+I will use it later' );
print_var( $var );
sub print_var {
my ( $data ) = @_;
print $data;
}
This specific example will really, effectively, do the same thing. Although it can sometimes be nice to use the formatted result of sprintf in other areas of a given application.
Hope this helps, and good luck!
---hA||ta----
print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );
| [reply] [d/l] [select] |
Re: Why SprintF?
by donarb (Beadle) on Jan 27, 2006 at 18:17 UTC
|
You should know that perl is a general purpose language. So you'll see many functions in perl that have nothing to do with creating web pages. But because perl excels at processing text, it excels at creating web pages.
In addition, you should know that using printf may not be the most efficient way of creating web pages. From the context of your question, it sounds like you are spitting out little pieces of HTML.
More advanced systems collect all of those little pieces of HTML, format and then print the page all at once (this is where sprintf can help). This has benefits when you start querying databases to build your page. If you start printing out HTML before all of your page processing completes, what happens when you run into a serious error further down the page? You can't pull back the parts of the page you've already printed. | [reply] |
Re: Why SprintF?
by blokhead (Monsignor) on Jan 27, 2006 at 21:03 UTC
|
My favorite way to generate SQL statements is with sprintf. So instead of using just concatenation:
## silly example without DBI quoting
my $sql = "insert into mytable ("
. join(",", @columns) . ") values ("
. join(",", @values) . ")";
.. (which I happen to think is very ugly) I can use sprintf:
my $sql = sprintf "insert into mytable (%s) values (%s)",
join(",", @columns),
join(",", @values);
So in this case, I really like the mini string-templating possiblities of s/printf, but of course I don't want to print the SQL statement to the screen, I want to send it to the database. Hence, sprintf.
This is nothing special about SQL statements, only that I often want to include the result of nontrivial expressions (i.e, not just $string) in the string. I can't interpolate a join statement (easily) inside a double-quoted string, and stringing together concatenation operations is ugly, so I use sprintf, which is easier to read (for me at least).
| [reply] [d/l] [select] |
|
|
If you are feeding those SQL statements to DBI/|DBD::*, placeholders instead of the join on @values can save a lot of problems..
my $sql = sprintf "insert into mytable (%s) values (%s)",
join(",", @columns),
join(",", map {'?'} @values); # couple different ways to
+ do this part
$dbh->do($sql, {}, @values);
Or for this example, SQL::Abstract can be very useful as well:
use SQL::Abstract;
my $sql = SQL::Abstract->new;
my %data;
@data{@columns} = @values; #hash slice to get column=>value pairs
my($sql, @bind) = $sql->insert($table, \%data);
$dbh->do($sql, {}, @bind);
| [reply] [d/l] [select] |
Re: Why SprintF?
by bart (Canon) on Jan 27, 2006 at 23:32 UTC
|
I'd turn that around: why have printf, when it does almost(*) exactly the same thing as print sprintf combined? After all, two separate, independently usable commands are more interesting to have around, than just have the combined command.
(*) I say almost, as there is one thing printf does differently from print: print appends $\ to everything it prints, while printf doesn't. | [reply] [d/l] |
Re: Why SprintF?
by traveler (Parson) on Jan 27, 2006 at 19:05 UTC
|
Here is one way I use it:
use strict;
use Data::Dumper;
my %dat;
$dat{a} = sprintf("%5.2f", 3.141);
$dat{b} = sprintf("%20s","hello");
print Dumper %dat;
That is, I like to store formatted data sometimes. In fact, I have written code like this:
use strict;
use Data::Dumper;
my $format_a = "%5.2f";
my $format_b = "%20s";
my %dat;
$dat{a} = sprintf($format_a, 3.141);
$dat{b} = sprintf($format_b,"hello");
$dat{c} = sprintf($format_a, 12.34567);
print Dumper %dat;
This way formats can easily be controlled in a central place.
--traveler
| [reply] [d/l] [select] |
Re: Why SprintF?
by ikegami (Patriarch) on Jan 27, 2006 at 18:40 UTC
|
While the results of sprintf usually end up being printed, sometimes it's easier to build the string first, then print if afterwards. For an example of this, check out a post I made yesteday, where I modify an existing string before printing it. | [reply] [d/l] |