2008-10-25 59 views
6

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

+0

链接被(有效)打破。 – 2017-06-03 04:52:33

回答

7

FormatR提供的Perl样形式的Ruby。

下面是从文档的例子:

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 
2

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

例如(从上方连结复制):

(defparameter *english-list* 
    "~{~#[~;~a~;~a and ~a~:;[email protected]{~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" 
13

我似乎记得类似的东西在Fortran语言时,我很多年前用它(但它很可能已经是第三方库)。

至于在Perl中的其他选项看看Perl6::Form

form函数替换Perl6中的format。达米安康威“Perl Best Practices”建议使用Perl6::Form与Perl5中与format ....

  • 静态定义
  • 依靠配置& PKG瓦尔数据的全局变量,他们在
  • 使用格式化援引下列问题命名文件句柄(只)
  • 没有递归或可重入

这里是一个Perl6::Form由罗伯特宝博红宝石示例的变化....

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; 
相关问题