[solved] perl - how do I run an external command?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
scsijon
Posts: 1596
Joined: Thu 24 May 2007, 03:59
Location: the australian mallee
Contact:

[solved] perl - how do I run an external command?

#1 Post by scsijon »

I need to run something like this:

Code: Select all

 $package_category = (./support/find_cat $package_name $package_description)
format I am trying to match is:

Code: Select all

output_variable = relative_file_path/filename_to_run $input_variable1 $input_variable2
it works ok via the console, but not in my opensuserpm2ppm script

it just errorrs out with

syntax error at ./opensuserpm2ppm line 149, near "(." and line149 is the above one of course.

I've tried using () {} and "" but it doesn't seem to work.

I've chased on the w5c training docs, but can't see anything I can use

any ideas please?

thanks
Last edited by scsijon on Wed 20 Feb 2013, 03:07, edited 1 time in total.


User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#3 Post by GustavoYz »

It depends if you need/want some results back.
Most cases, this is the best:

Code: Select all

my $stuff = `cat /this/file`;
Perl uses 'sh' by default, so avoid bashisms.

Also, if writing something complex to escape, something that requires back-ticks, for example, use 'qx/`shell code here`/.

The system function gives only the return status of the shell code executed.

scsijon
Posts: 1596
Joined: Thu 24 May 2007, 03:59
Location: the australian mallee
Contact:

#4 Post by scsijon »

thanks all and especially GustavoYz

eventual code I used is

Code: Select all

$package_category = `./support/find_cat $package_name $package_description` ;
with the ` from the key with ~ and not the " key.

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#5 Post by GustavoYz »

Exactly...
You can also save the whole command and its arguments into a single variable, and then backtick it. Something like this is perhaps more practical for re-use:

Code: Select all

if ($opts{f}){
  $comm .= ' ' . "$opts{f}";
} else {
  $comm .= ' ' . $default_file;
}
my $result = `$comm`;
using Getopt::Std (core module), so then you can specify different files using a '-f' switch, for example:

Code: Select all

./script.pl -f /path/to/file.sh

Post Reply