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

Dear fellow monks,
I am trying to pull in 4 arguments from the command line, put them into variables and then use them as part of a system() call. Here is some of my code:

$rpt_name = $ARGV[0]; $desname = $ARGV[1]; $btch = $ARGV[2]; $record = $ARGV[3]; @args = qw(/where/the/program/is/rwcli60 report=$rpt_name userid=us +er_id@db server=my_server destype=printer desname=$desname btch=$btch + record=$record subbtch=0); $retval = system("@args"); die "system failed: $?" unless $retval == 0;

I have made the connection in the past, so it is not a path problem or anything. My personal opinion is that it has to do with the way I am putting the variables into the @args array. Do I need single quotes around the variables??????? Any help would be appreciated.

Prince99

Too Much is never enough...

Replies are listed 'Best First'.
Re: help with arrays
by arturo (Vicar) on Apr 20, 2001 at 20:23 UTC

    Here's a place where assigning in a list context and using array slices is neato. You could try this:

    my ($rpt_name, $desname, $btch, $record) = @ARGV[0..3];

    which is a compact way of doing what your code already does.

    Now, as to functional matters, the first thing I noticed is that the qw() does not interpolate, so all you'll ever get are the names of your arguments in there. So that qw()won't do.

    Here's a good rule to live by: when you're generating strings and then passing them to system calls, print those strings out when you're not getting the behavior you desire, at least during development. If you'd done that, you'd notice that the command you're attempting to execute isn't going to work.

    As for fixing that, you're better off with something like:

    # following the above my @args= ("path/to/prog", "report=$rpt_name" , ...);

    for creating the @args array.

    However, when you put the double-quotes around that array, you're defeating the purpose of passing an array to system, because quoting turns the array into a string. So, if you follow the above advice, you should be making the call like so:

    #get rid of $retval, too, unless you need it system(@args) and die "System failed: $?\n";

    HTH!

      Thanks to everyone for their ideas. They took care of the problem. Unfortunately, another problem has arisen. Again I would appreciate any insight into the problem. Here is where I am so far:

      $ENV{ORACLE_HOME} = '/opt/OraHome9i/6iserver'; $ENV{REPORTS60_PATH} = '$ORACLE_HOME/repordmin/templates:$ORACLE_HOME/ +tools/devd em60/demo/reports'; if(&ReadParse(\%input)){ @args= ("/opt/OraHome9i/6iserver/bin/rwcli60", "report=$input{rpt}" , + "userid=rep_user/sinclair\@pmi", "server=rep60_kodiac", "destype=pri +nter", "desname=$input{desname}", "btch=$input{btch}", "subbtch=0", " +record=$input{record}"); &ProcessForm;} else{ die;} } sub ProcessForm { $retval = system("@args"); die "system failed: $?" unless $retval == 0;

      The problem is thus: If I call this script "test2.pl" from the command line and pass it parameters it works fine. EX.test2.pl rpt=report.rdf desname=printername btch=50 record=0
      Now if I call it from an html page as a url. EX.http://blah/blah/test2.pl?rpt=report.rdf&desname=printername&btch=50&record=0 it gives me an error and $retval=256. I don't know why it works from the command line and prints the report, but from the html page as url it doesn't. Please help a confused novice.

      Prince99

      Too Much is never enough...
        It is not a user issue, is it? A lot of systems run as user nobody or apache, and maye they don't have access to the program?
        Just a shot
                        - Ant
Re: help with arrays
by lachoy (Parson) on Apr 20, 2001 at 20:10 UTC

    Don't use qw, changes below:

    ... my @args = ( '/where/the/program/is/rwcli60', "report=$rpt_name", 'userid=user_id@db', 'server=my_server', 'destype=printer', "desname=$desname", "btch=$btch", "record=$record", 'subbtch=0' ); my $retval = system( @args ); die "system failed: $?" unless $retval == 0;

    Note where I used single vs. double quotes -- double quotes must be used if you want to interpolate a variable within a string. Single quotes will not do any interpolation.

    Chris
    M-x auto-bs-mode

Re: help with arrays
by Sprad (Hermit) on Apr 20, 2001 at 20:17 UTC
    qw doesn't interpolate variables. If you say qw($blah), you'll get the literal text "$blah", not the value of $blah. Instead, just do @args = "/where/the...", but change "user_id@db" to "user_id\@db" so Perl doesn't think it's an array. Then everything will work fine.

    Actually, I'm not even sure why you're putting it in @args and not $args. I think a scalar would be more than happy to hold your string for you.

    ---
    I'm too sexy for my .sig.