Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

can't locate object method new in perl

by Bhabesh347 (Initiate)
on Nov 03, 2016 at 08:06 UTC ( [id://1175199]=perlquestion: print w/replies, xml ) Need Help??

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

while running a perl script in unix , which worksS perfectly in windows strawberry, i am getting following error: cant locate object method "new" via package "Text::CSV" Any insights to identify this is highly appreciated MyScript.pl
#!/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(tim +e); $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_tim +estamp"; #Visit each .csv file by checking its naming convention over h +ere my $fileCt = 0; if($file=$ARGV[0]){ print "\n Script starts processing for the timings $st +art_timestamp"; $ct = 1; open($data, '<', $file) or die "Could not open given f +ile \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 => "(&(customer +Code=$customerCode))", ); die ldap_error_text($result->code) if +$result->code; $max = $result->count; if($max == 0) { open($logfile, '>>', 'logfile. +txt'); print $logfile "This customerC +ode $customerCode was not found in LDAP and was not reset\n"; close $logfile } else { open($logfile, '>>', 'logfile. +txt'); print $logfile "This customerC +ode $customerCode was found in LDAP and is reset\n"; close $logfile } for (my $index = 0 ; $index < $max ; $ +index++) { my $entry = $result->entry($in +dex); $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('challengeQuestio +n')}[0]; $old = ${$entry->get('challengeAnswer' +)}[0]; $old = ${$entry->get('ctscPasswordCrea +tionDate')}[0]; $old = ${$entry->get('ctscPasswordExpi +rationDate')}[0]; $old = ${$entry->get('ctscPasswordHist +ory')}[0]; $old = ${$entry->get('ctscPasswordRese +tAttempts')}[0]; $old = ${$entry->get('ctscPasswordLock +outEnable')}[0]; $old = ${$entry->get('ctscLastResetDat +e')}[0]; $old = ${$entry->get('ctscFailedLoginC +ount')}[0]; $entry2 = $entry->clone; # copies entr +y $ldap->modify($dn, replace => {'cn' => + "" }); $ldap->modify($dn, replace => {'userPa +ssword' => "" }); $ldap->modify($dn, replace => {'challe +ngeQuestion' => "" }); $ldap->modify($dn, replace => {'challe +ngeAnswer' => "" }); $ldap->modify($dn, replace => {'ctscPa +sswordCreationDate' => "" }); $ldap->modify($dn, replace => {'ctscPa +sswordExpirationDate' => "" }); $ldap->modify($dn, replace => {'ctscPa +sswordHistory' => "" }); $ldap->modify($dn, replace => {'ctscPa +sswordResetAttempts' => "" }); $ldap->modify($dn, replace => {'ctscPa +sswordLockoutEnable' => "" }); $ldap->modify($dn, replace => {'ctscLa +stResetDate' => "" }); $ldap->modify($dn, replace => {'ctscFa +iledLoginCount' => "" }); } else { warn "Line could not be parsed +: $line\n"; } $ct++; } #End while loop my ($sec1,$min1,$hour1,$mday1,$mon1,$year1,$wd +ay1,$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 inp +ut"; } }
CSV.pm:
use Text::CSV; my @rows; my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attr +ibute. 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 argume +nt $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_h +r () $ref = $csv->getline_hr ($io); # getline (), but returns a hash +ref $eof = $csv->eof (); # Indicate if last parse or # getline () hit End Of File $csv->types(\@t_array); # Set column types

Replies are listed 'Best First'.
Re: can't locate object method new in perl
by GrandFather (Saint) on Nov 03, 2016 at 08:09 UTC

    Reduce the code to about 10 lines we can run and see the problem.

    Premature optimization is the root of all job security
Re: can't locate object method new in perl
by haukex (Archbishop) on Nov 03, 2016 at 08:15 UTC

    Hi Bhabesh347,

    This "CSV.pm" that you are showing us appears to be a copy-and-paste of the module's synopsis. That's unnecessary, and if you placed that file into a directory named "Text", that would explain why your MyScript.pl isn't working, since Perl might be loading that instead of the actual module. You may need to install the Text::CSV module properly, using either your system's package manager, cpan, or cpanm. This is all just a guess though, as GrandFather already said, it'd be best if you give us an SSCCE.

    Hope this helps,
    -- Hauke D

Re: can't locate object method new in perl
by Erez (Priest) on Nov 03, 2016 at 08:28 UTC

    This code isn't even supposed to run due to the use Net::LDAP::E#ntry; on line 7, and you should get an error way before getting to the Text::CSV instantiation. Other than that, I don't see any issue with it, and on my Linux machine it compiles and run without even a warning.

    Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1175199]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-20 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found