Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- formatting question
June 3, 2008, 5:58 pm
rate this thread
This is in Perl for Dummies, 4th ed, p160.
I'm trying to understand this ...
the first part, "%s%$Fmt%s", my understanding is the format part,
which specifies the formats for the second part, thelist part, ("%
$Fmt=|", $TestStr, "|"): %s for "%$Fmt=|", %$Fmt for $TestStr, and %s
for "|", respectively. Is this correct?
Then what is %$Fmt, it seems a % for format and then a variable $Fmt,
the book did not mention any format string like this ...
Anyone can shed some light? Thanks!
Re: formatting question
Double quoted strings are processed and the results of that processing
are then used in the rest of the expression. This applies everywhere in
Perl. Part of that processing is interpolation of variables. This is
part of string handling in general and not specifically part of the job
done by printf() - which is why string-interpolation isn't mentioned in
the documentation for printf.
If $Fmt contains 's' then "%s%$Fmt%s" is '%s%s%s'
If $TestStr contains 'teststring' the result of the original sprintf()
is then '%s=|teststring|'
Presumably the author uses this in some sort of loop to illustrate the
effects of various formatting characters.
Re: formatting question
A few more lines of code showing what $Fmt and $TestStr are set to would
be useful... I'm going to guess you were using something like
my $Fmt = 'f';
my $TestStr = '1.004';
in which case you would get the string '%f=|1.004000|' back from
Hmmm, I would suggest you find a new book. Quite apart from the fact
I've never heard anything good about any of the 'for Dummies' books,
the expression you gave is *not* a clear way to write what they wanted.
'Learning Perl', published by O'Reilly, is the standard recommendation;
for others, see perldoc -q book or http://books.perl.org .
Firstly, there is no 'first part' and 'second part', and the inner set
of parens are completely unnecessary. The important point is that
sprintf treats its first argument specially, and uses it to format the
What you are not understanding is that there are two levels of
intepretation going on here. (This is one of the reasons I would not
write it like that: it's confusing.)
First Perl expands the double-quoted strings, so given the values I was
assuming above sprintf gets passed the list
'%s%f%s', '%f=|', '1.004', '|'
which I've written with single-quotes to show they aren't going to be
expanded again by Perl. Then sprintf takes the first argument, and
formats the rest in the appropriate places, so
%s -> %f=|
%f -> 1.004000
%s -> |
%s%f%s -> %f=|1.004000|
I presume the aim here is to let you put in various values for $TestStr,
and various formats, and show you how sprintf would intepret them.
I would have writted it as
sprintf "%%$Fmt=|%$Fmt|", $TestStr
or perhaps even
"%$Fmt=|" . sprintf("%$Fmt", $TestStr) . '|'
which, although it still looks a little like line-noise, is probably
easier to understand.
Raise your hand if you're invulnerable.