6

我在这里可能是少数,但我非常喜欢Perl 的格式。我特别喜欢能够在一列中包含一段长文本(“~~ ^<<<<<<<<<<<<<<<<”类型的东西)。是否有任何其他具有类似功能的编程语言或实现类似功能的库?我对任何为 Ruby 实现类似功能的库特别感兴趣,但我也对任何其他选项感到好奇。

4

3 回答 3

13

当我多年前使用 Fortran 时,我似乎想起了类似的东西(但是,它很可能是一个第三方库)。

至于 Perl 中的其他选项,请查看Perl6::Form.

form函数format在 Perl6 中替换。Damian Conway 在“ Perl 最佳实践”中建议使用Perl6::FormPerl5,并引用以下问题format......

  • 静态定义
  • 依赖全局变量进行配置,依赖 pkg 变量来格式化数据
  • 使用命名文件句柄(仅)
  • 不是递归的或可重入的

这是Perl6::FormRobert Gamble 对 Ruby 示例的一个变体......

use Perl6::Form;

my ( $month, $day, $year ) = qw'Sep 18 2001';
my ( $num, $numb, $location, $toe_size );

for ( "Market", "Home", "Eating Roast Beef", "Having None", "On the way home" ) {
    push @$numb,     ++$num;
    push @$location, $_;
    push @$toe_size, $num * 3.5;
}

print form
    '   Piggy Locations for {>>>}{>>}, {<<<<}',
                          $month, $day, $year ,
    "",
    '  Number: location              toe size',
    '  --------------------------------------',
    '{]})      {[[[[[[[[[[[[[[[}       {].0} ',
     $numb,    $location,              $toe_size;
于 2008-10-25T21:23:19.610 回答
7

FormatR为 Ruby 提供了类似 Perl 的格式。

这是文档中的一个示例:

require "formatr"
include FormatR

top_ex = <<DOT
   Piggy Locations for @<< @#, @###
                     month, day, year

Number: location              toe size
-------------------------------------------
DOT

ex = <<TOD
@)      @<<<<<<<<<<<<<<<<       @#.##
num,    location,             toe_size
TOD

body_fmt = Format.new (top_ex, ex)

body_fmt.setPageLength(10)
num = 1

month = "Sep"
day = 18
year = 2001
["Market", "Home", "Eating Roast Beef", "Having None", "On the way home"].each {|location|
    toe_size = (num * 3.5)
    body_fmt.printFormat(binding)
    num += 1
}

产生:

   Piggy Locations for Sep 18, 2001

Number: location              toe size
-------------------------------------------
1)      Market                   3.50
2)      Home                     7.00
3)      Eating Roast Beef       10.50
4)      Having None             14.00
5)      On the way home         17.50
于 2008-10-25T16:30:46.287 回答
2

Lisp(format ...)功能。它支持循环、条件和一大堆其他有趣的东西。

例如(从上面的链接复制):

(defparameter *english-list*
  "~{~#[~;~a~;~a and ~a~:;~@{~a~#[~;, and ~:;, ~]~}~]~}")

(format nil *english-list* '())       ;' ==> ""
(format nil *english-list* '(1))      ;' ==> "1"
(format nil *english-list* '(1 2))    ;' ==> "1 and 2"
(format nil *english-list* '(1 2 3))  ;' ==> "1, 2, and 3"
(format nil *english-list* '(1 2 3 4));' ==> "1, 2, 3, and 4"
于 2008-10-25T16:30:02.963 回答