还有哪些其他语言具有与 Perl 格式类似的功能和/或库?
题
我可能是这里的少数派,但我非常喜欢 Perl 的格式. 。我特别喜欢能够在一列中包含一长段文本(“~~ ^<<<<<<<<<<<<<<<<”类型的东西)。是否有其他具有类似功能的编程语言或实现类似功能的库?我对任何为 Ruby 实现类似功能的库特别感兴趣,但我也对任何其他选项感到好奇。
解决方案
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
其他提示
我似乎记得很多年前我使用 Fortran 时有类似的东西(但它很可能是第 3 方库)。
至于 Perl 中的其他选项,请查看 Perl6::Form
.
这 form
函数替换 format
在 Perl6 中。达米安·康威在《Perl 最佳实践“建议使用 Perl6::Form
Perl5 引用了以下问题 format
....
- 静态定义的
- 依赖于配置的全局变量和用于格式化数据的 pkg 变量
- (仅)使用命名文件句柄
- 不可递归或可重入
这里有一个 Perl6::Form
Robert 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;
有 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"
不隶属于 StackOverflow