LoginSignup
3
3

More than 5 years have passed since last update.

Ruby から Perl を 実行(run) する方法 ; gem 経由

Last updated at Posted at 2014-01-25

前置き

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?)

@1

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 情報元

zephiroworks/ruby-perl

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3