Perl debugger problem

Hi gurus, during debugging script I noticed some very strange behavior. The 1st task is to trim last character of string stored in $temp1 variable, this can be done using either chop($temp1) or more complicated substr($temp1, 0, length($temp1) - 1). Those both codes works as oneliners but in my script the substr solution did not works. Bellow is example from debugger:

1st TASK:

1st SOLUTION - DID NOT WORKING:

main::(delmid_n.pl:24):	    if (($state == 0) && ($_ !~ $musr) && ($_ !~ $lusr)) {
>> n
main::(delmid_n.pl:54):	        $strprn=substr($temp1, 0, length($temp1) - 1);
>> p $temp1
                (-,user2,t-mobile.co.uk)\

>> p substr($temp1, 0, length($temp1) - 1) . "\n";
                (-,user2,t-mobile.co.uk)


  DB<4> 
main::(delmid_n.pl:55):		print $strprn . "\n";
  DB<4> 


main::(delmid_n.pl:56):	        $temp1 = "";
  DB<4>

As you can see in the $strprn variable nothing is stored but if the same piece of code (which is stored into $strprn variable) is printed via 'p' command the output is OK. This "bug" can be overcomed using mentioned chop() function, see code bellow:

2nd SOLUTION - WORKING:

main::(delmid_w.pl:24):	    if (($state == 0) && ($_ !~ $musr) && ($_ !~ $lusr)) {
>> p $temp1
                (-,user2,t-mobile.co.uk)\

  DB<4> n
main::(delmid_w.pl:56):		chop ($temp1);
  DB<4> n
main::(delmid_w.pl:57):		print $temp1;
  DB<4> n
                (-,user2,t-mobile.co.uk)
main::(delmid_w.pl:58):	        $temp1 = "";
  DB<4>

The above code is exactly the same as first example but following two lines from 1st example:

$strprn=substr($temp1, 0, length($temp1) - 1);
print $strprn . "\n";

are replaced with following two lines in 2nd example:

chop ($temp1);
print $temp1;

What is wrong with 2nd solution ?

2nd TASK:

Second task is quiet simple matching the string against regexp, but it is not working for me:

  DB<1> 
main::(delbeg_n.pl:15):	$state = 0;
  DB<1> 
main::(delbeg_n.pl:16):	$muser = qr/\(-,user1,[^,]+\.co\.uk\)\\$/;
  DB<1> 
main::(delbeg_n.pl:19):	line: while (<>) {
  DB<1> 
main::(delbeg_n.pl:20):	    chomp;      # strip record separator
  DB<1> 
main::(delbeg_n.pl:21):	    @Fld = split(/\s+/, $_,);
  DB<1> 
main::(delbeg_n.pl:23):	    if (($state == 0) && ($Fld[1] =~ $muser)) {
  DB<1> p $Fld[1]        
(-,user1,one2one.co.uk)\

  DB<2> n
main::(delbeg_n.pl:43):	        print $_;
  DB<2> p $_
netgroup1       (-,user1,one2one.co.uk)\

  DB<3> if ($Fld[1] =~ $muser) {print "TRUE"}
TRUE

This is problem which I did not workaroud so far.

As you can see after executing line 21 in code the next execution line is 43 (the else statement). Why following condition is not evaluated as a true and code did not continues with line 23, 24, 25 ...

if (($state == 0) && ($Fld[1] =~ $muser))

Following line was inserted as a demonstration that condition should be evaluated as a true.

if ($Fld[1] =~ $muser) {print "TRUE"}

Thanks a lot.

Use

chomp

not

chop

It has been already done but you seen results in above post.

#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
                        # this emulates #! processing on NIH machines.
                        # (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
                        # process any FOO=bar switches

$[ = 1;                 # set array base to 1
$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

$state = 0;
$temp1 = "";
$temp2 = "";
$musr = qr/\(-,user3,[^,]+\.co\.uk\)\\$/;
$lusr = qr/\(-,user3,[^,]+\.co\.uk\)$/;
$strprn="";

while (<>) {
    chomp;      # strip record separator

    if (($state == 0) && ($_ !~ $musr) && ($_ !~ $lusr)) {
        if (length($temp1) != 0) {
            print $temp1;
        }

        $temp0 = $_;
        $state = 1;
    }
    elsif (($state == 1) && ($_ !~ $musr) && ($_ !~ $lusr)) {
        if (length($temp0) != 0) {
            print $temp0;
        }

        $temp1 = $_;
        $state = 0;
    }
    elsif (($state == 0) && ($_ =~ $musr)) {
        $state = 2;
    }
    elsif (($state == 2) && ($_ =~ $musr)) {
        $state = 3;
    }
    elsif (($state == 3) && ($_ !~ $musr) && ($_ !~ $lusr)) {
        print $temp1;
        print $_;
        $temp1 = "";
        $temp0 = "";
        $state = 1;
    }
    elsif (($state == 2) && ($_ =~ $lusr)) {
        $strprn=substr($temp1, 0, length($temp1) - 1);
	print $strprn . "\n";
        $temp1 = "";
        $state = 0;
    }
    else {
	print "Error at line " . $.; exit $.;
    }
}

if ($state == 0) {
    print $temp1;
}
elsif ($state == 1) {
    print $temp0;
}
else {
    exit $.;
}

#exit $ExitValue;

Input file:

netgroup1       (-,user1,one2one.co.uk)\
                (-,user1,t-mobile.co.uk)\
                (-,user2,one2one.co.uk)\
                (-,user2,t-mobile.co.uk)\
                (-,user3,one2one.co.uk)\
                (-,user3,t-mobile.co.uk)
netgroup2       (-,user4,one2one.co.uk)\
                (-,user4,t-mobile.co.uk)\
                (-,user5,one2one.co.uk)\
                (-,user5,t-mobile.co.uk)\
                (-,user6,one2one.co.uk)\
                (-,user6,t-mobile.co.uk)
netgroup3       (-,user7,one2one.co.uk)\
                (-,user7,t-mobile.co.uk)\
                (-,user8,one2one.co.uk)\
                (-,user8,t-mobile.co.uk)\
                (-,user9,one2one.co.uk)\
                (-,user9,t-mobile.co.uk)