in reply to System command using array and pipe

I am trying to execute a system command. The problem is that 1) there is a space in the directory name for the directory path, and 2) there is a pipe and a redirection.

(2) means you are trying to execute a shell command, but you aren't running a shell. When using the single-argument form of system, Perl will invoke the command in a shell if it thinks necessary. But you aren't using the single-argument form of system.

system("mysqldump","--add-drop-table","-uroot","-ppassword", "mydataba +se","|","gzip","-9c",">","$backup_dir/mydatabase.sql.gz");

is the same as

mysqldump '-add-drop-table' '-uroot '-ppassword' 'mydatabase' '|' 'gzi +p' '-9c' '>' "$backup_dir/mydatabase.sql.gz"

at the prompt.

You have a couple of options.

You could surely simplify at the cost of overhead.

Replies are listed 'Best First'.
Re^2: System command using array and pipe
by civil777 (Initiate) on Jun 04, 2011 at 06:22 UTC

    Bravo! Thank you very much. Your first solution worked like a charm.

    I did have some trouble getting the second solution to work. Perl is complaining about "Use of uninitialized value $_ in pattern match (m//)" when the $backup_dir is passed to the sub text_to_shell_lit. I studied the text_to_shell_lit sub for a while and could not figure out what was wrong. (BTW, what is the underscore in the argument of the sub? ... Newbie question, I know.)

      Fixed.

      The underscore prototype makes $_ the default argument. Specifically, it makes map text_to_shell_lit, equivalent to map text_to_shell_lit($_),. I always forget I have to use $_[0] and not $_ when I use the "_" prototype.