The standard advice is found in perlfaq5, on perlmonks, and elsewhere. Using Number::Format or a commify() sub works well, but isn't very convenient for modifying existing code that was written with printf. It seems unlikely that perl's printf will ever support %'d and friends (see printf(3), scroll to "Flag Characters").
However, almost any linux system has a /usr/bin/printf command with %' support. So, the simple definition
will let you usesub cprintf {system('/usr/bin/printf', @_)}
to print: There are at least 42,000,000 ways to do it!cprintf("There are at least %'d ways to do it!\n", 42e6)
For this to work, your LC_NUMERIC environment variable should be set to a locale that has a "thousands separator", such as en_US.UTF-8. It seems to work quite well, at least for simple cases. To modify existing code, just change your printf's to cprintf's and add apostrophe's to the formats as needed. Less work than wrapping each argument with a subroutine call and changing each format to %s. It also helps keep things clear and readable.
It's tempting to override the builtin printf, but that's not easy to do. The CORE documentation lists printf as a special keyword. (Playing with tied file-handles or other tricks might work, but doesn't seem worth it too me.)
On the other hand, if you want to auto-magically commify *all* your %d's, you could use:
And then cprintf("%d", 42e6) will print 42,000,000.sub cprintf { my($fmt)= shift; $fmt=~s/%(-?\d*)d/%'$1d/g; system('/usr/bin/printf', $fmt, @_) }
- Every cprintf execs a new process. You probably don't want to call cprintf() 42,000,000 times.
- You can't use perl's %v format, and perl can't check for missing or extra args to cprintf like it does for printf.
- Runtime errors from /usr/bin/printf may be hard to catch or diagnose.
- To print to a file, you have to re-direct stdout. Commified printing to a string (sprintf) in this way would be tricky. Backtics invoke the shell, which would be fraught with danger. You'd need IPC::System::Simple or something similar.
- /usr/bin/printf has some unique features (some might be useful). See the gnu coreutils docs, or try 'info coreutils printf' ('man printf' isn't very complete).
will printsystem('/usr/bin/printf', 'Bash uses backtics for cmd substitution, eg: %s', '`ls *`')
This works because perl puts two strings into **argv and then directly execs /usr/bin/printf. But,Bash uses backtics for cmd substitution, eg: `ls *`
will do something quite different, since perl will pass the single string to a shell, which will then execute ls. Replace 'ls' with 'rm' (or, horrors, 'rm -rf') and you've got a disaster on your hands!system( '/bin/printf "Bash uses backtics for cmd substitution, eg: %s" `ls *`' )
Happy Holidays, and best wishes for the new year!
-Jeff
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Printing large numbers with commas to show thousands groups.
by shmem (Chancellor) on Dec 27, 2019 at 13:48 UTC | |
by Anonymous Monk on Dec 27, 2019 at 23:47 UTC | |
by shmem (Chancellor) on Dec 28, 2019 at 00:46 UTC | |
by Anonymous Monk on Dec 30, 2019 at 18:54 UTC | |
Re: Printing large numbers with commas to show thousands groups. (updated)
by haukex (Archbishop) on Dec 28, 2019 at 22:10 UTC | |
Re: Printing large numbers with commas to show thousands groups.
by jnorden (Novice) on Dec 31, 2019 at 18:14 UTC |