0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

標準出力を整形するもう1つの選択肢、perl5 の format

Last updated at Posted at 2022-08-17

背景

大くの言語にprintf のようなものが、整形のために用意されている。
より強力な表現性を持つ Lisp の format もある。
しかし、コードを実行するまでそれで表現される出力の審美性を捉えることは難しい。
sprintf "%-4s %02s %13s" , $d->year, $d->mon, $d->fullmonth;
そこで perl の format だ

概要

What you see is what you get
そんなコードが扱うのが簡単である
format はコードと出力の解離が少なく書けるので
選択肢の1つに入れておくと保守性の高いコードを期待できる

使用例

formatingPower.png
簡潔で判りやすい命名ができると、もっと見やすくなります。
また右に2つ3つとテーブル表示する時には、
printf では保守しにくいものになりますが、format はヘッチャラです。

注意

日本語のような2文字の幅を持つ Wide character は上手く扱えない。
また、見えない文字列としてdye()のような端末ハイライトの文字列も
format は扱い難い。一応パイプで接続して着色なら崩れない。
英語や数字のみを扱う時には機能する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?