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

#!/usr/bin/perl -w use strict; use Net::LDAP; my $ldap = Net::LDAP->new ( "ldap.hostname.com") or die "$@"; sub LDAPattrsearch { my ($ldap,$searchString,$Attrs,$base) = @_; if (!$base) { $base = "o=hostname.com"; } if (!$Attrs) { $Attrs = [ 'val1','val2','val3','val4']; } } my $result = $ldap->search ( base => "$base", filter => "$searchString", attrs => "$Attrs" ); } print "$result\n";
I am running this code. Getting the below error.
perl -c ldap_data.pl Global symbol "$base" requires explicit package name at ldap_data.pl l +ine 15, <DATA> line 225. Global symbol "$searchString" requires explicit package name at ldap_d +ata.pl line 16, <DATA> line 225. Global symbol "$Attrs" requires explicit package name at ldap_data.pl +line 17, <DATA> line 225. Unmatched right curly bracket at ldap_data.pl line 19, at end of line syntax error at ldap_data.pl line 19, near "}" ldap_data.pl had compilation errors.

Replies are listed 'Best First'.
Re: Error while using strict.
by moritz (Cardinal) on Apr 03, 2012 at 10:20 UTC

    Sometimes Perl's error messages aren't the best. In particular, the fourth error message:

    Unmatched right curly bracket at ldap_data.pl line 19, at end of line syntax error at ldap_data.pl line 19, near "}"

    is the really interesting one. If you have syntax errors, ignore the erorrs from strict. Fix the syntax error first.

    The syntax might also be related to the fact that you close the subroutine before you use $base etc, ie you define these variables inside the subroutine, but try to use them on the outside. That doesn't work.

Re: Error while using stirct.
by tangent (Parson) on Apr 03, 2012 at 10:52 UTC
    Error while using stirct
    Your error isn't to do with using strict (or warnings) - if you turn off strict you will still get an error caused by the two problems highlighted below:
    sub LDAPattrsearch { my ($ldap,$searchString,$Attrs,$base) = @_; if (!$base) { $base = "o=hostname.com"; } if (!$Attrs) { $Attrs = [ 'val1','val2','val3','val4']; } } # <---- THIS SHOULDN'T BE HERE AT ALL my $result = $ldap->search ( base => "$base", filter => "$searchString", attrs => "$Attrs" ); } # <---- AND THIS SHOULD GO AT THE END AFTER PRINT print "$result\n";
Re: Error while using stirct.
by Khen1950fx (Canon) on Apr 03, 2012 at 12:45 UTC
    This works for me. It returns timeout because the host isn't an ldap server.
    #!/usr/bin/perl -l use strict 'refs'; use warnings; use Net::LDAP; my $mesg = (); die "$@" unless my $ldap = Net::LDAP->new( 'www.example.com', 'timeout' => 2, 'debug' => 1, ); sub LDAPattrsearch { use strict; use warnings; $mesg->bind; my ($searchString, $Attrs, $base) = @_; unless ($base) { $base = "o=hostname.com"; } unless ($Attrs) { $Attrs = [ 'val1', 'val2', 'val3', 'val4']; } $mesg = $ldap->search( base => $base, filter => $searchString, attrs => $Attrs, ); $mesg = $ldap->code && $mesg->error; foreach my $entry ($mesg->entries) { $entry->dump; } $mesg->unbind; } undef $ldap; exit;
      It does work, because it is not giving any errors, but am not getting any output too :(.