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

Beginner here so I apologize if this is a dumb question.

Is there a simple way to send a unix command from inside my perl script? For exapmle the command we use on our computers (HP UNIX) to send a file to the printer I want is "ppc <filename>". How do I send that command? Thanx for your time!

-Kevin

Replies are listed 'Best First'.
Re: Unix command line
by janjan (Beadle) on Sep 28, 2003 at 22:20 UTC
    You've got a few options. exec() will call the command and exit; system() will call the command and then return control to your program; and backticks and qx// will capture the output of the command.
    my @who = `/usr/local/bin/who`;
    It sounds like you want to use exec() or system(), though. Read perldoc -f system and perldoc -f exec for more info.
Re: Unix command line
by Zaxo (Archbishop) on Sep 28, 2003 at 22:26 UTC

    system '/path/to/ppc', $filename; (I assume the angle brackets are some artifact)

    After Compline,
    Zaxo

Re: Unix command line
by tachyon (Chancellor) on Sep 29, 2003 at 04:41 UTC

    For completeness there is also the pipe open:

    open PIPE, "|$prog" or die $!

    which lets you fire up the external program and then pass a stream of data to it by printing to the pipe.....

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      You should also check for the exit status of the command when using pipe to invoke external commands.

      open will fail

      • if it was not able to fork a process
      • the command was not found.
      If there was any error in execting the command, it can be checked by closing the pipe and checking for the return value.

      The sequence will be

      # opne the pipe, check if fork passed open PIPE, "|$prog" or die $!; # do something with the command like print PIPE @files; # check if the command completed successfully close PIPE or die "Command failed : $! || $?";

      Hope this helps

      -T