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

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

Thread Previous | Thread Next
From:
Yuki Kimoto
Date:
July 1, 2021 23:50
Subject:
Re: Pre-RFC: Width-aware (s)printf flag
Message ID:
CAExogxMYGD9ug=0ous1wRC8g=VOBix=fwLr1LKg+Gg1-QWWMDw@mail.gmail.com
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);

Thread Previous | Thread Next


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