Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello dear monks how it could be possible to include bash commands in my perl program? i tended to write bash scripts and use my perl programs there, is it possible to write my perl program and use some bash commands there in line with my perl codes??

Replies are listed 'Best First'.
Re: using bash commands in perl program
by kyle (Abbot) on Jan 05, 2009 at 16:44 UTC

    See system, backticks/qx in perlop under "Quote and Quote-like Operators", and the special pipe open case in open. You might also be interested in IPC::Run.

Re: using bash commands in perl program
by toolic (Bishop) on Jan 05, 2009 at 16:58 UTC
    In addition to the advice already given, keep in mind that Perl has some built-in commands that are equivalent to some Unix commands. See UNIX 'command' equivalents in Perl.
      I know those commands but since some of my programs run under linux and i need to get output in bash shell, i needed to run them in my perl code.
Re: using bash commands in perl program
by zentara (Cardinal) on Jan 05, 2009 at 16:54 UTC
    You can run bash thru IPC::Open3. Here is a super simple example. You can add alot more complexity by doing a Select on the output filehandles.
    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; $|=1; #my $pid=open3(\*IN,\*OUT,\*ERR,'/bin/bash'); my $pid=open3(\*IN,\*OUT,0,'/bin/bash'); # set \*ERR to 0 to send STDERR to STDOUT my $cmd = 'date'; #send cmd to bash print IN "$cmd\n"; my $result = <OUT>; print $result;

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      thanks, this tip was nice :)