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

Try following code
$file = "somefile.file";
system( "dos2unix $file $file" );
or
$file = "somefile.file";
system( "mv $file $file" );
The system command seems to pass second argument to shell
as command. How can I avoid this ?
I tried
@argv = ( "mv", $file, $file );
system(@argv);
I get same error. What am I missing ?

Replies are listed 'Best First'.
Re: system function
by chromatic (Archbishop) on Mar 19, 2000 at 11:04 UTC
    system uses the shell when you call it with one argument. It doesn't do that when you call it with a list. The Perl Cookbook suggests code like this:
    my $status = system($program, $arg1, $arg2); die "$program exited funny: $?" unless $status == 0;
    I would expect the array to flatten into a list in your last example, but I wouldn't expect a list to become a single item. Which error message are you receiving?
      This reply was the best. Thank you so much
Re: system function
by mortis (Pilgrim) on Mar 20, 2000 at 05:52 UTC
    Not that this is perl related, but if you're executing the move command mv with the same file as both arguments, mv should fail with an error message. Your first invocation of system() should execute the command just as if you had typed it into the shell. Which can be dangerous -- if you're using the -T switch for taint checking, the first version of system will cause your program to die if the value of $file is obtained from outsite the source code of your script (from a command line argument, or from a file including stdin). The second invocation of system() should be much safer, as the array invocation of system() executes the first argument, and any remaining arguments are passed directly to the program being executed. As already asked, what is the output of the program when you run it?
RE: system function
by Anonymous Monk on Mar 20, 2000 at 09:22 UTC
    system("mv \"$file1 $file1\"");
Re: system function
by Anonymous Monk on Mar 21, 2000 at 20:55 UTC
    Thanx chromatic. Your answer was really helpful.
    Mortis, sorry for confusion. I should have put mv $file1
    $file2.
RE: system function
by Anonymous Monk on Mar 19, 2000 at 23:05 UTC
    just use the backticks.
      do not just use backtics! backtics should only be used when it is absolutely crucial that you capture the output of a program! otherwise it is wasteful, because the output will be captured into a temporary variable (and then thrown away...)