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

OS: Solaris 10 ( X86 ) Hi I am a new bie and have been coding a relatively easy structured programming method. I have a snippet of my program which will:
#-----Running LDAPMODIFY Against The Ldif File Created-----# sub RunUpdate { print "--Updating Directory server. Please wait for a moment....\n +\n"; my $resultLdapSearch = `ldapmodify -h $ldap_server -D "cn=Director +y Manager" -w $dir_mgr_pwd -f $ldap_modify_file`; #print "resultLdapSearch: $resultLdapSearch\n"; }
When running the program I get a output error due to the ldapmodify command is trying to update and existing field, as shown below:
bash-2.05# perl 4-run_update_attributes.pl ********************************************************************** +******* Step #4 - The Script WILL NOW Update The Users "Company" and "Co" Attr +ibutes ********************************************************************** +******* Do you want to continue [y]: --Updating Directory server. Please wait for a moment.... ldap_modify_s: Type or value exists ********************************************************************** +* See Result: ----------- Log Output file (verified): 01152008.log ********************************************************************** +*
As you notice, the error "ldap_modify_s: Type or value exists" appears from the "ldapmodify" that I executed. I need to know how do I capture this value and once I find out, I will write some codes to handle this error. Please advice.

Replies are listed 'Best First'.
Re: How to capture error messages...
by ikegami (Patriarch) on Jan 16, 2008 at 01:56 UTC
    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`;

      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?

Re: How to capture error messages...
by hipowls (Curate) on Jan 16, 2008 at 03:53 UTC

    IPC::Open3 or IPC::Run provide facilities for running external programs and capturing standard error separately from standard output, but be warned, it's easy to deadlock.

    However you may find that using Net::LDAP better suits your needs. Since the attempted LDAP updates occur within your program checking the result is fairly easy.

    use Net::LDAP; my $ldap = Net::LDAP->new( ... ); my $mesg = $ldap->modify( ... ); if ( $mesg->is_error() ) { ... }

Re: How to capture error messages...
by Anonymous Monk on Jan 16, 2008 at 04:03 UTC
Re: How to capture error messages...
by poolpi (Hermit) on Jan 16, 2008 at 11:56 UTC
    Here you can find a pure Perl clone of the ldapmodify command:

    ldapmodify.pl

    HTH,

    PooLpi