【问题标题】:What other languages have features and/or libraries similar to Perl's format?还有哪些其他语言具有类似于 Perl 格式的功能和/或库?
【发布时间】:2008-10-25 16:15:52
【问题描述】:

我在这里可能是少数,但我非常喜欢Perl's formats。我特别喜欢能够在一列中包含一段长文本(“~~ ^

【问题讨论】:

  • 链接(实际上)已损坏。

标签: ruby perl format


【解决方案1】:

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

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

form 函数替换了 Perl6 中的 format。 "Perl Best Practices" 中的 Damian Conway 建议将 Perl6::Form 与 Perl5 一起使用,引用 format 的以下问题....

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

这是 Robert Gamble 对 Ruby 示例的 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;

【讨论】:

    【解决方案2】:

    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
    

    【讨论】:

      【解决方案3】:

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

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

      (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"
      

      【讨论】:

        猜你喜欢
        • 2010-10-20
        • 2010-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 2010-10-27
        • 2011-06-02
        • 2010-10-07
        相关资源
        最近更新 更多