LoginSignup
22
26

More than 5 years have passed since last update.

perlで外部コマンドを実行する方法

Posted at

perlでシステム系コマンドの取り方
バックグランドで動かすforkやexecはほぼ却下系?
http://blog.livedoor.jp/sasata299/archives/51069647.html
http://d.hatena.ne.jp/mas-inagaki/20100107/1262847507

普通に実行するときは以下。(標準出力などは出てしまう)

perl
system($command);

プログラムの中で出力をコネコネしたい時は以下。(実行したプログラムの標準出力には出ない)

perl
open my $rs, "$command 2>&1 |";
my @rlist = <$rs>;
close $rs;
my $result = join '', @rlist;

以下の形だと実行した標準出力に出てしまうので、あんまりいけてない。
というか値(コマンド結果の標準出力)が取れないことがある。

perl
my $result = `$command 2>&1`;
22
26
2

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
22
26