Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

loop inside system command

by DS (Acolyte)
on Jul 17, 2002 at 13:58 UTC ( #182428=perlquestion: print w/replies, xml ) Need Help??

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

Hello , I am using the system command to excute a command inside my script , however I am trying to use an array inside that system command ,,, forexmaple , the command line is :
my $firstbuild = hi.bld my @otherbuilds = q( low.bld hello.bld cow.bld ); system(qq(C:/execu $firstbuild @otherbuilds file.c));
knowing that the array @otherbuilds could have more or less builds than three. so I am trying to find a way to do somthing like this
system(qq(C:/execu $firstbuild low.bld hello.bld cow.bld file.c));
somehow read the array index and list them in an order .. I tried a loop within that command but system didn't like it . what do yall think .. thanks for hints

Replies are listed 'Best First'.
Re: loop inside system command
by stephen (Priest) on Jul 17, 2002 at 14:14 UTC
    I'd recommend using the multi-argument variant of system().
    system("C:\\execu", $firstbuild, @otherbuilds, 'file.c') and die "System command failed";

    stephen

Re: loop inside system command
by abaxaba (Hermit) on Jul 17, 2002 at 14:18 UTC
    If I understand your question correctly, you'll need to execute system singularly, and build your loop around it, as opposed to in it
    foreach ($firstbuild, @otherbuilds) { system (qq(C:/execu $_ file.c)); }
    One thing you may consider, esp. if @otherbuilds can grow to be sufficiently large: system is a double fork, in terms of processes spawned. One proc. for the system call, one for the actual command to run. If you're going to be calling system on more than a couple of items, you may consider doing something like so:
    open (OUT, ">cmd.txt") || die "$!\n"; foreach ($firstbuild, @otherbuilds) { print OUT "C:/execu $_ file.c\n"; } close OUT; system ("cmd.txt");
    The reason? Instead of spawning 2N processes, you'll spawn only N+1. 3 or 4 system calls probably doesn't warrant the extra work. If you got 100 or so...well, you get the picture.

    ÅßÅ×ÅßÅ
    "It is a very mixed blessing to be brought back from the dead." -- Kurt Vonnegut
      thanks to you all :)
Re: loop inside system command
by cidaris (Friar) on Jul 17, 2002 at 14:18 UTC
    I got this to work:
    my $command_line = ""; my $first_build = "hi.bld"; my @other_builds = qw( low.bld hello.bld cow.bld ); $command_line = "c:\\execu $first_build"; $command_line .= " " . shift @other_builds while (@other_builds); $command_line .= " file.c"; print "$command_line\n"; system "$command_line\n";


    Hope it helps.
    cidaris
      Interesting, cidaris. But have you ever used the join command?

      It would save you your loop on "while(@other_builds)".

      $command_line = "c:\\execu $first_build "; # note trailing space $command_line .= join " ", @other_builds; $command_line .= " file.c";
      The only downside here is that you wind up with 2 spaces between $first_build and file.c if there are no other_builds.

      The problem goes away if you do everything in a join:

      $command_line .= join " ", ("c:\\execu $first_build",@other_builds,"fi +le.c");

      --
      Mike

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://182428]
Approved by claree0
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2020-07-16 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?