Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.97k stars 556 forks source link

Absorber printf conversion #17166

Open p5pRT opened 5 years ago

p5pRT commented 5 years ago

Migrated from rt.perl.org#134467 (status was 'open')

Searchable as RT134467$

p5pRT commented 5 years ago

From @jidanni

Created by jidanni@gmail.com

For cases like

$ perl -we '@​xyz = ( 1 .. 3 ); printf "X=%D Z=%d\n"\, @​xyz[ 0\, 2 ];' X=1 Z=3

how about offering a different way​: introducing the "absorber conversion"!

$ perl -we '@​xyz = ( 1 .. 3 ); printf "X=%D%q Z=%d\n"\, @​xyz;' X=1 Z=3

That's right\, "%q" has simply eaten its argument and done nothing!

Why would anybody want to do that? Well at least give users a choice.

(Just like the "unpack" function has an "x" template. See perlpacktut.)

Perl Info ``` Flags: category=core severity=wishlist Site configuration information for perl 5.28.1: Configured by Debian at Sun Mar 31 11:51:22 UTC 2019. Summary of my perl5 (revision 5 version 28 subversion 1) configuration: ... ```
p5pRT commented 5 years ago

From @jkeenan

On Wed\, 02 Oct 2019 19​:57​:25 GMT\, jidanni wrote​:

This is a bug report for perl from jidanni@​gmail.com\, generated with the help of perlbug 1.41 running under perl 5.28.1.

----------------------------------------------------------------- [Please describe your issue here]

For cases like

$ perl -we '@​xyz = ( 1 .. 3 ); printf "X=%D Z=%d\n"\, @​xyz[ 0\, 2 ];' X=1 Z=3

how about offering a different way​: introducing the "absorber conversion"!

$ perl -we '@​xyz = ( 1 .. 3 ); printf "X=%D%q Z=%d\n"\, @​xyz;' X=1 Z=3

That's right\, "%q" has simply eaten its argument and done nothing!

Why would anybody want to do that? Well at least give users a choice.

(Just like the "unpack" function has an "x" template. See perlpacktut.)

Do you have any prior art for this feature request?

Internet search for "absorber conversion" mainly turns up links for automotive shock absorbers?

Thank you very much.

-- James E Keenan (jkeenan@​cpan.org)

p5pRT commented 5 years ago

The RT System itself - Status changed from 'new' to 'open'

p5pRT commented 5 years ago

From @tonycoz

On Wed\, 02 Oct 2019 12​:57​:25 -0700\, jidanni wrote​:

For cases like

$ perl -we '@​xyz = ( 1 .. 3 ); printf "X=%D Z=%d\n"\, @​xyz[ 0\, 2 ];' X=1 Z=3

how about offering a different way​: introducing the "absorber conversion"!

$ perl -we '@​xyz = ( 1 .. 3 ); printf "X=%D%q Z=%d\n"\, @​xyz;' X=1 Z=3

That's right\, "%q" has simply eaten its argument and done nothing!

Why would anybody want to do that? Well at least give users a choice.

(Just like the "unpack" function has an "x" template. See perlpacktut.)

You can already use %.0s to do this​:

$ perl -le 'printf "%s%.0s%s\n"\, "a"\, "b"\, "c"' ac

though this will try to convert that second parameter to a string\, which may have side-effects (like a warning if it's undef.)

$ perl -Wle 'printf "%s%.0s%s\n"\, "a"\, undef\, "c"' Use of uninitialized value in printf at -e line 1. ac

You can already reference printf parameters by index​:

$ perl -le 'printf q(%1$s%3$s)\, "a"\, "b"\, "c\n"' ac

which avoids the side-effects​:

perl -le 'printf q(%1$s%3$s)\, "a"\, undef\, "c\n"' ac

I don't know that there's much value to this change.

Tony