develooper Front page | perl.perl5.porters | Postings from July 2021

Re: Pre-RFC: Width-aware (s)printf flag

Thread Previous
From:
Dan Book
Date:
July 2, 2021 00:13
Subject:
Re: Pre-RFC: Width-aware (s)printf flag
Message ID:
CABMkAVX99G-er3nin2SqS_2bN5YtXomvF2VEtLDAg=bLE_kkug@mail.gmail.com
On Thu, Jul 1, 2021 at 7:50 PM Yuki Kimoto <kimoto.yuki@gmail.com> wrote:

>
> 2021-7-2 1:52 Dan Book <grinnz@gmail.com> wrote:
>
>> On Thu, Jul 1, 2021 at 12:46 PM Paul "LeoNerd" Evans <
>> leonerd@leonerd.org.uk> wrote:
>>
>>> Consider
>>>
>>>   printf "%-40s : %s\n", $_->@* for @rows;
>>>
>>> There is one potential problem here: you normally need to encode
>> characters to bytes in order to print them. The grapheme determination
>> would need to happen before encoding. This would work out if you're
>> printing to a handle with an encoding layer, but probably cause confusion
>> in the usual case.
>>
>> -Dan
>>
>
> Does the following code work well?
>
>   use utf8;
>   use Encode 'encode';
>
>   my $ouptput = '';
>
>   for my $row (@rows) {
>     $ouptput .= sprintf "あいうえお %-|40s : %s\n", $row->@*;
>   }
>
>   print encode('UTF-8', $output);
>
>
Yes, encoding after this operation is done with sprintf would work fine.
The documentation could explain this.

-Dan

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About