Click to See Complete Forum and Search --> : Perl and executing system commands


James Bond
04-26-2001, 12:57 AM
Hi.

I have found 3 ways to execute system commands using perl as a cgi.

#1 qx{ "<command>" }
This works, but, I can't do something like "finger user" or "ls -l" I am limited to finger or ls. Is this true or am I doing something wrong

#2 system ("command", "arg", ...)

How do I correctly use this command, I can never get any output when used in a cgi. Also, do I need to store this in an array (@ar = system(...) ) or, can I use a string like variable ( $var = system(...) )

#3 exec

How do I use this command

Also, which of these 3 is generally considered the 'best' under the most situations.

Thanks

-JB

jemfinch
04-26-2001, 07:16 AM
They're all bad. CGI is a really dangerous environment in which to execute outside programs.

Anyway, qx// is the same thing as backticks, which captures the output (as an array of lines, or a string of the whole thing, depending on what you assign it to.)

System can be used two ways -- it can be used as simply "system($string)", in which case it spawns a shell to interpret the arguments if $string has spaces in it. Or it can be called as "system($progname, $argv1, ...)", which spawns $progname and calls it with $argv1 and all the other arguments you gave to it. If there are no spaces in $progname, it won't spawn a shell. This latter method is the way you should always call system in perl (if you need to at all -- it's still not a good idea.)

Exec replaces the current executing image (which is perl, interpreting your script) with whatever program+arguments you give it. You probably don't want that.

Jeremy

YaRness
04-26-2001, 08:19 AM
any way to just do a server side, or are you wanting to process information from some outside proggie?

i'd be interested in what information or proggie it is you are running and why, there must be another way to do it.