shenme has asked for the wisdom of the Perl Monks concerning the following question:
In some routines I needed to replace printf's with something to capture output into an array. So I substituted a routine that used sprintf and then saved the output string away. And started to see output strings of '1', '2', '5', etc. After flailing about with increasing paranoia I finally decided to read the docs absolutely literally, and changed the routine from
to something likesub xprintf { my( @args ) = @_; push @aglobal, sprintf @args; }
and boggled that that worked.sub xprintf { my( $fmt, @args ) = @_; push @aglobal, sprintf $fmt, @args; }
sprintf usage is documented as "sprintf FORMAT, LIST" as opposed to, say, "sprintf LIST". The same form is documented for printf, "printf FORMAT, LIST". I had supposed this was merely for explication, to make clear that the first value is the format string. And if I code something like
it works just fine. However, using the same form with sprintf the following produces the string '2',@args = ( "We see '%s'\n", 'wowza' ); printf @args;
Very strange that sprintf should be so much more pedantic than printf, eh?@arghs = ( "We see '%s'\n", 'wowza' ); $s = sprintf @arghs;
[ One of the CB'ots thought there was a discussion of this same oddity somewhere hereabouts, but searching on "sprintf printf format", "sprintf array", "sprintf format", and so on didn't find it. ]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sprintf format parameter required
by davido (Cardinal) on Oct 31, 2003 at 05:31 UTC | |
by shenme (Priest) on Oct 31, 2003 at 05:36 UTC | |
|
Re: sprintf format parameter required
by demerphq (Chancellor) on Oct 31, 2003 at 09:33 UTC | |
by Anonymous Monk on Oct 31, 2003 at 13:58 UTC | |
by demerphq (Chancellor) on Oct 31, 2003 at 16:09 UTC | |
|
Re: sprintf format parameter required
by Abigail-II (Bishop) on Oct 31, 2003 at 10:05 UTC | |
|
Re: sprintf format parameter required
by Roger (Parson) on Oct 31, 2003 at 06:22 UTC | |
|
Re: sprintf format parameter required
by BrowserUk (Patriarch) on Oct 31, 2003 at 06:14 UTC |