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

Hi I am a newbie. I have a very long code which I know can be simplified but that is my not question but my question is why when running the perl program, it skips some of the codes (stated below) and subs being called (in the example below it skipps sub printSummary). Is there a problem here? I am running it in a Solaris 10 environment.
------------------------------------ This is perl, v5.8.4 built for sun4-solaris-64int (with 27 registered patches, see perl -V for more detail) Copyright 1987-2004, Larry Wall ------------------------------------
My sub info is as below:
#-----Extract Moved Users-----# sub ExtractUsers { my $foundUser = "n"; my $foundDN = "n"; my $matchUser = -1; my $foundOrg = -1; my $lenLine = 0; my $readLine = 0; my $foundFirst = "n"; my $totalFound = 0; my $statusLine = 0; my $myArray = 0; print "ExtractUsers\n"; open(FILE, "< $backupPathDir") or die "Can't Find or Open +$backupPathDir: $!"; # File output - writing to print "backupPathDir: $backupPathDir\n";; open(NEW, "> $ldap_move_file") or die "can't open $ldap_mo +ve_file: $!"; print "ldap_move_file: $ldap_move_file\n"; while (<FILE>) { # Good practice to store $_ value because # subsequent operations may change it. my($line) = $_; # Good practice to always strip the trailing # newline from the line. chomp($line); #print "line = $line\n"; ++$readLine; ++$statusLine; if ($statusLine eq 20000) { print "Processing Lines: $readLine\n"; $statusLine = 0; } $lenLine = length($line); #print "lenline = $lenLine\n"; # Skip if found blank line and also statement with line id "en +try-id" if (($lenLine > 1) && (index($line,'entry-id') eq -1)){ # checking whether the line contains o=mail,dc=company,dc= +com $foundOrg = index($line, $org_suffix); if ($foundOrg > 0) { $foundUser = "n"; $foundDN = "y"; if ($totalFound eq scalar(@userArray)) { exit (0); } } #print "FoundOrg = $foundOrg\n"; #print "foundDN = $foundDN\n"; if ($foundDN eq "y") { $line =~ s/People/$move_suffix/g; #print "***Line = $line\n"; #$userArray = @userArray; foreach $myArray (@userArray) { #$userArray =$_; #print "userArray = $userArray\n"; $matchUser = index($line, $myArray); #print "matchuser = $matchUser\n"; if ($matchUser > 0) { if ($foundFirst eq "y") { # Insert a blank line after each users but + not for first found user (print NEW "\n") or die "can't write to + $ldap_move_file: $!"; } (print NEW $line . "\n") or die "can't writ +e to $ldap_move_file: $!"; $foundFirst = "y"; $foundUser = "y"; ++$totalFound; } else { $foundDN = "n"; } } } else { if ($foundUser eq "y") { if (index($line, "nsUniqueId") eq -1) { (print NEW $line . "\n") or die "can't writ +e to $ldap_move_file: $!"; } } } } } close(FILE) or die "can't close $backupPathDir: $ +!"; close(NEW) or die "can't close $ldap_move_file: $ +!"; my $test = `rm $backupPathDir`; my $logfile = $step1_logfile . `date '+%m%d%Y'`; chomp ($logfile); $logfile = "$logfile" . "." . $step1_log_ext; print "OutputLog: $logfile\n"; open(READ, "< $ldap_move_file") or die "Can't Find or Open + $ldap_move_file: $!"; # Appending TO the Log files open(WRITE, ">> $logfile") or die "Can't Find or Open $log +file: $!"; print WRITE "**Step #5 Creating a Move User LDIF File\n"; print WRITE "========================================\n"; while (<READ>) { # Good practice to store $_ value because # subsequent operations may change it. my($line) = $_; # Good practice to always strip the trailing # newline from the line. chomp($line); print WRITE "$line\n"; } print WRITE "\n"; close(READ) or die "can't close $ldap_move_file: +$!"; close(WRITE) or die "can't close $logfile: $!"; }
My main calling code of execution is below
#--Loading of parameters from deprovision.cfg--# LoadConfigParam; ExtractParameterStep1; PrintBanner; # Reading input file - reading from if (-e $verified_working_file) { ContinueYesNo; if ($answer eq "y") { #--Run DB2LDIF Before Changes--# $b4_after_status = $db2ldif_after_ext; Db2Ldif; ValidUserArray; ExtractUsers; PrintSummary; } }
Please note that from the line beginning with the command below onwards the lines have been skipped. I am still very confused why this is happening? How do I track it?
my $test = `rm $backupPathDir`;

Edit: g0n - readmore tags

Replies are listed 'Best First'.
Re: Why my perl script skip my some of the executed lines even without using conditions??
by jesuashok (Curate) on Apr 10, 2006 at 09:18 UTC
    Hi

    This is not the case. If you have a condition check before calling "PrintSummary" function as follows :-

    if ($answer eq "y") { #--Run DB2LDIF Before Changes--# $b4_after_statu +s = $db2ldif_after_ext Db2Ldif ValidUserArray ExtractUsers PrintSummary }
    If $answer eq 'Y' then definitely the function would have been called.
    The reasons when "PrintSummary" would not have beed called are as follows :-
    1) If there is any exit statment in the previous function
    2) If there is any condition check and return statement in the begining of "PrintSummary" function then you may not come to know whethere the function is called or not

    "Keep pouring your ideas"
Re: Why my perl script skip my some of the executed lines even without using conditions??
by graff (Chancellor) on Apr 11, 2006 at 02:58 UTC
    I assume you are not posting all the code (maybe this is a good thing), but one important thing that is missing here is "use strict" -- you do have a lot of "my" declarations, so if you aren't using stricture, it shouldn't be hard to add.

    Anyway, given what you have posted, I would be surprised if the execution went beyond this block:

    if ($totalFound eq scalar(@userArray)) { exit (0); }
    Near as I can tell, you don't have any code that adds an element to @userArray -- so scalar() on that would always be zero. Then, the part that increments $totalFound is inside a loop over the elements of @userArray -- and since that is always zero, $totalFound never gets incremented, so when the above if statement is ever reached, that is where the script will exit, every time.

    As for how to track what's going on, the perl debugger is pretty easy to learn; read the perldebug man page, and run the script with "perl -d". But before you do that, make sure you have "use strict;", and try "-w" (or "use warnings;") as well. Maybe try a little refactoring while you're at it...

      I have not tried using "use strict" but the strangest thing is IF i run a "perl -d" over my script I am actually able to run the codes line by line and it completes successfully but when I were to run it using "perl -w" or "perl" it will stop pass the line 333
      : : Line 331: close(FILE) or die "can't close $backupP +athDir: $!"; Line 332: close(NEW) or die "can't close $ldap_mov +e_file: $!"; Line 333: Line 334: my $test = `rm $backupPathDir`; Line 335: my $logfile = $step1_logfile . `date '+%m%d%Y'`; : :
      Sigh! Very very strange.... help!
      Hi Graff,

      You are correct! That was the problem.... as I was ripping my code all over again.... I then realized... thanks a lot.

      It is working again.... Thanks!