in reply to How to capture error messages...

You could combine STDERR and STDOUT. Also, you forgot to convert the arguments into literals.
my $resultLdapSearch = `ldapmodify -h \Q$ldap_server\E -D "cn=Director +y Manager" -w \Q$dir_mgr_pwd\E -f \Q$ldap_modify_file\E 2>&1`;

You could make it much more readable, though.

my $cmd = join ' ', map quotemeta, ( 'ldapmodify', -h => $ldap_server, -D => 'cn=Directory Manager', -w => $dir_mgr_pwd, -f => $ldap_modify_file, ); my $resultLdapSearch = `$cmd 2>&1`;

Replies are listed 'Best First'.
Re^2: How to capture error messages...
by ambrus (Abbot) on Jan 16, 2008 at 10:18 UTC

    I think that using quotemeta for quoting arguments for the shell is a bad practice. For bash-like shells, it misquotes newline characters. Quotemeta should be used for perl regular expressions and nothing else.

      Quotemeta should be used for perl regular expressions and nothing else.

      eh?

      For bash-like shells, it misquotes newline characters.

      Indeed.

      $ perl -e'$msg = "abc\ndef"; print `echo \Q$msg\E`;' abcdef

      system and back-ticks use the bourne shell. What would be a proper escape function for bourne shell args?

        In most cases, it's simpler to just avoid the shell and exec the program directly. In this case, this can be done by using the multi-argument pipe-open (unless you have a very ancient perl). It would come out something like this (untested):

        my $resultLdapSearch; { open my $P, "-|", "ldapmodify", "-h", $ldap_server, "-D", "cn=Director +y Manager", "-w", $dir_mgr_pwd, "-f", $ldap_modify_file or die "cannot exec ldapmodify: $!"; local $/ = undef; defined($resultLdapSearch = <$P>) or die "error reading from pipe: $!"; close $P or die $! ? "error closing pipe: $!" : "ldap_server exitted with a non +zero exit status: $?"; }

        If you really need to quote strings for a bash-like shell, try one of these (untested). The last one only works in newer bash shells, but it has the advantage that it also quotes control and meta characters to characters readable on any terminal. The first three probably works in any shell.

        sub shell_quote1 { local($_) = @_; s/([^\w\n])/\\$1/g; s/\n/\"\n\"/g; +length or $_ = q/""/; $_ } sub shell_quote { local($_) = @_; s/([\\\$\"\`])/\\$1/g; qq/"$_"/ } sub shell_quote2 { local($_) = @_; s/'/'\\''/g; qq/'$_'/ } sub bash_quote3 { local($_) = @_; s/(\W)/sprintf("\\x%02x", ord($1))/g +e; qq/\$'$_'/ }

        The shell_quote1 function is similar to quotemeta except that it quotes newline characters properly, whereas quotemeta translates them to "\\\n" which the shell will unquote as nothing. I also quotes empty strings to an empty shell word.

        Finally, note that you can't quote nul characters for the shell in any way, because the shell cannot pass those as arguments to programs. Also, these functions except byte strings, so encode utf-8 strings before passing to these functions.