无法在perl中找到新的对象方法

时间:2016-11-03 07:46:06

标签: perl-module

<> 在unix中运行perl脚本时,它在windows草莓中完美运行,我收到以下错误:

找不到对象方法" new"通过包" Text :: CSV"

高度赞赏任何能够识别此信息的见解

脚本:

#!/usr/bin/perl
use strict;
use warnings;
use Net::LDAP;
use Text::CSV;
use Net::LDAP::E#ntry;
use File::Basename;
use File::chmod;
use Config::Tiny;
use File::Copy;
use Text::Trim;
use Data::Dumper qw(Dumper);
use Net::LDAP::Util qw(ldap_error_text);
use Net::LDAP::Constant;

my $config = Config::Tiny->read('config.ini');

#Variable Declaration section
my ($bindhost,$port,$bindpwd,$binddn,$base_search,$ldap,$customerCode,$logDir,$entry,$result,$csv,$file,$line,$data,$cn,$dn,$entry2,$start_timestamp,$new,$u,$ct,$old,$logfile,$max,$stop_timestamp);

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$start_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec);

foreach my $section (keys %{$config}) {
        #LDAP Binding Connectivity variables declaration
        $bindhost = $config->{$section}->{'ldap_host'};
        $port = $config->{$section}->{'ldap_port'};
        $bindpwd = $config->{$section}->{'ldap_password'};
        $binddn = $config->{$section}->{'ldap_user'};
        $base_search = $config->{$section}->{'ldap_customers_ou_dn'};
        $logDir = $config->{$section}->{'log_dir'};

        # connect to the ldap server
        my $ldap = Net::LDAP->new($bindhost,port=>$port,timeout=>240) or die "Could not bind to ldap server: $! - $@\n";

        $result = $ldap->bind
        (
                dn => trim($binddn), password=>trim($bindpwd)
        );

        #Open Script directory over here
        opendir(DIR, ".");
        my @files = grep(/\.csv$/,readdir(DIR));
        closedir(DIR);
        $csv = Text::CSV->new({ sep_char => ',' });
        #print "\n Script starts processing for the timings $start_timestamp";

        #Visit each .csv file by checking its naming convention over here
        my $fileCt = 0;

        if($file=$ARGV[0]){
                print "\n Script starts processing for the timings $start_timestamp";
                $ct = 1;
                open($data, '<', $file) or die "Could not open given file \n";
                open($logfile, '>>', 'logfile.txt');
                print $logfile "Script started running for file $file at ".$start_timestamp."\n";
                close $logfile;

                while ($line = <$data>){
                        if ($csv->parse($line)) {
                                my @fields = $csv->fields();
                                $customerCode = $fields[0];
                                $result = $ldap->search(
                                                base => "$base_search",
                                                filter => "(&(customerCode=$customerCode))",
                                                );
                                die ldap_error_text($result->code) if $result->code;

                                $max = $result->count; 
                                if($max == 0) {
                                        open($logfile, '>>', 'logfile.txt');
                                        print $logfile "This customerCode $customerCode was not found in LDAP and was not reset\n";
                                        close $logfile
                                }
                                else { 
                                        open($logfile, '>>', 'logfile.txt');
                                        print $logfile "This customerCode $customerCode was found in LDAP and is reset\n";
                                        close $logfile
                                }      
                                for (my $index = 0 ; $index < $max ; $index++) {
                                        my $entry = $result->entry($index);
                                        $u = ${$entry->get('uid')}[0];
                                        $dn = "uid=$u,$base_search";   
                                }

                                my @all = ();
                @all = trim($result->entries);
                foreach $entry (@all){}

                                $entry = Net::LDAP::Entry->new;
                                $entry->dn($dn);
                                $entry->replace(
                                'cn' => " ",
                                'userPassword'=> "",
                                'challengeQuestion'=> "",
                                'challengeAnswer'=> "",
                                'ctscPasswordCreationDate'=> "",
                                'ctscPasswordExpirationDate'=> "",
                                'ctscPasswordHistory'=> "",
                                'ctscPasswordResetAttempts'=> "",
                                'ctscPasswordLockoutEnable'=> "",
                                'ctscLastResetDate'=> "",
                                'ctscFailedLoginCount'=> "",  
                                );

                                $entry->update ($ldap);

                                $old = ${$entry->get('cn')}[0];
                                $old = ${$entry->get('userPassword')}[0];
                                $old = ${$entry->get('challengeQuestion')}[0];
                                $old = ${$entry->get('challengeAnswer')}[0];
                                $old = ${$entry->get('ctscPasswordCreationDate')}[0];
                                $old = ${$entry->get('ctscPasswordExpirationDate')}[0];
                                $old = ${$entry->get('ctscPasswordHistory')}[0];
                                $old = ${$entry->get('ctscPasswordResetAttempts')}[0];
                                $old = ${$entry->get('ctscPasswordLockoutEnable')}[0];
                                $old = ${$entry->get('ctscLastResetDate')}[0];
                                $old = ${$entry->get('ctscFailedLoginCount')}[0];
                                $entry2 = $entry->clone; # copies entry

                                $ldap->modify($dn, replace => {'cn' => "" });
                                $ldap->modify($dn, replace => {'userPassword' => "" });
                                $ldap->modify($dn, replace => {'challengeQuestion' => "" });
                                $ldap->modify($dn, replace => {'challengeAnswer' => "" });
                                $ldap->modify($dn, replace => {'ctscPasswordCreationDate' => "" });
                                $ldap->modify($dn, replace => {'ctscPasswordExpirationDate' => "" });
                                $ldap->modify($dn, replace => {'ctscPasswordHistory' => "" });
                                $ldap->modify($dn, replace => {'ctscPasswordResetAttempts' => "" });
                                $ldap->modify($dn, replace => {'ctscPasswordLockoutEnable' => "" });
                                $ldap->modify($dn, replace => {'ctscLastResetDate' => "" });
                                $ldap->modify($dn, replace => {'ctscFailedLoginCount' => "" });                        
                                }
                                else {
                                        warn "Line could not be parsed: $line\n";
                                }
                                $ct++;
                    } #End while loop  

                        my ($sec1,$min1,$hour1,$mday1,$mon1,$year1,$wday1,$yday1,$isdst1)=localtime(time);
                        $stop_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",$year1+1900,$mon1+1,$mday1,$hour1,$min1,$sec1);
                        print "\n Script ends Here for the timings - $stop_timestamp ";
                        open($logfile, '>>', 'logfile.txt');
                        print $logfile "Processing stopped at ".$stop_timestamp."\n";
                        close $logfile;
                        close $data;
                }       #if     file pattern checking loop ends        
                else {
                        print "\n Please provide a .csv file as an input";
                }
        }

CSV.pm:

use Text::CSV;

my @rows;
my $csv = Text::CSV->new ( { binary => 1 } )  # should set binary attribute.
                or die "Cannot use CSV: ".Text::CSV->error_diag ();

open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!";
while ( my $row = $csv->getline( $fh ) ) {
    $row->[2] =~ m/pattern/ or next; # 3rd field should match
    push @rows, $row;
}
$csv->eof or $csv->error_diag();
close $fh;

$csv->eol ("\r\n");

open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!";
$csv->print ($fh, $_) for @rows;
close $fh or die "new.csv: $!";

#
# parse and combine style
#

$status = $csv->combine(@columns);    # combine columns into a string
$line   = $csv->string();             # get the combined string

$status  = $csv->parse($line);        # parse a CSV string into fields
@columns = $csv->fields();            # get the parsed fields

$status       = $csv->status ();      # get the most recent status
$bad_argument = $csv->error_input (); # get the most recent bad argument
$diag         = $csv->error_diag ();  # if an error occured, explains WHY

$status = $csv->print ($io, $colref); # Write an array of fields
                                      # immediately to a file $io
$colref = $csv->getline ($io);        # Read a line from file $io,
                                      # parse it and return an array
                                      # ref of fields
$csv->column_names (@names);          # Set column names for getline_hr ()
$ref = $csv->getline_hr ($io);        # getline (), but returns a hashref
$eof = $csv->eof ();                  # Indicate if last parse or
                                      # getline () hit End Of File

$csv->types(\@t_array);               # Set column types

1 个答案:

答案 0 :(得分:0)

我不知道你的第二个代码块是什么。看起来您复制了SYNOPSIS from the CPAN page of Text::CSV

但是,在您的程序中,您有一个use TEXT::CSV,然后您收到以下错误消息:

  

找不到对象方法&#34; new&#34;通过包&#34; Text :: CSV&#34;

该错误信息是一个死路。

你的Unix机器上没有安装Text :: CSV。 Install it from CPAN