前置き
Raw な Ruby からは Perl スクリプトを run するのは非常にメンドイらしい
gem を使って楽に run する
開発者曰く「バグを見つけたら issue report ください」とし「Rspec で 充分なテストしてるから(大丈夫)」とも仰っている。
環境導入
gem install ruby-perl
コード諸々
- 'heello, Perl from Ruby'
hello.rb
require 'perl'
Perl.run %Q{print 'hello, Perl from Ruby\n'}
$ ruby hello.rb
hello, Perl from Ruby
- foreach 処理
foreach.rb
require 'perl'
Perl do
run <<-EOF
my @stone = ("Perl", "Ruby");
foreach my $gem (@stone){
print "$gem\n";
}
EOF
end
$ ruby foreach.rb
Perl
Ruby
異常
- Perl の文法ミス
- Perl のエラーコードが出る
- Perl のエラーが起きても、Ruby のエラーは起きない@1
syntaxErrorPerl.rb
require 'perl'
Perl.run %Q{ pInt 'hello, world\n' }
$ ruby syntaxErrorPerl.rb
String found where operator expected at (eval 1) line 2, near "pInt 'hello, world'"
(Might be a runaway multi-line '' string starting on line 1)
(Do you need to predeclare pInt?)
Perl 文法ミスによる Perl.run の例外処理を試行したが、error を出力していなかった
begin
Perl.run %Q{ pInt 'hello, world\n' } #=> no exception
rescue => e
p e.class # 以下 2 行が実行されず
p e.message #
end
gem 情報元