in reply to Re^2: How to capture error messages...
in thread How to capture error messages...

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?

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

    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.

      If you really need to quote strings for a bash-like shell, try one of these (untested)
      Or you could use a module, like String::ShellQuote, that most likely has been tested.

      In this case, this can be done by using the multi-argument pipe-open

      No, you can't. You took out the 2>&1, reintroducing the problem the OP was trying to solve. I'd use IPC::Open3 to redirect STDERR without involving the shell.

        Oh, sorry. I didn't notice that.

        In that case, let me modify the above code like this (untested):

        my $resultLdapSearch; { defined(my $pid = open my $P, "-|") or die "error forking ldapmodify: +$!"; if (!$pid) { open STDERR, ">&STDOUT" or die "error dup2ing stdout to stderr"; exec "ldapmodify", "-h", $ldap_server, "-D", "cn=Directory Manager +", "-w", $dir_mgr_pwd, "-f", $ldap_modify_file or die "cannot exec ldapmodify: $!"; die; # just to be sure } local $/ = undef; defined($resultLdapSearch = <$P>) or die "error reading from pipe: $!"; close $P or die $! ? "error closing pipe: $!" : "ldapmodify exitted with a nonz +ero exit status: $?"; }