Perl: Problem in retaining values after each loop

use strict;
use warnings;
open (my $fhConditions, "<input1.txt"); #open input file1
open (my $fhConditions1, "<input2.txt");#open input file2
open (my $w1, ">output1");
open (my $w2, ">output2");
our $l = 10;#set a length to be searched for match 
our $site="AAGCTT";#pattern to be matched
our $p1=0;
our $p2=0;
our $line1;
our $line2;
open(my $read, "<2.txt");#file where pattern would be present
my @e = <$read>;
my $d = join('', @e );
$d =~ s/\s+//g;
while ($line1 = <$fhConditions>) #while each line in input file 1
{
chomp $line1;
my @info = split("\t" , $line1);
my $match1 = substr($d,$info[3],$l);#in $d from the position specified by $info[3] till $l characters get all characters stored as $match1
print "$match1\n";
$p1=index($match1, $site);#get the index where it is actually matching
print  "$p1 \n";
print $line1;
}
while ($line2 = <$fhConditions1>)#while each line in input file 2
 {
chomp $line2;
print $line2;
my @info = split("\t" , $line2);
my $a = $info[3]-$l;
my $match2 = substr($d,$a,$l);#in $d from the position specified by $info[3] -$l characters get all characters stored as $match2
print "$match2\n";
$p2=index($match2, $site);
print  "$p2 \n";
print $line2;  
}
if($p1+$p2<=30)#if the sum of the indexes is less than 30 then print those lines from input files to output 
{
print $w1 $line1;
print $w2 $line2;
}

i am unable to get the values for $p1 and $p2 . how do i make it proper?

Without seeing your input data it's impossible to guess what you're trying to do here.

What input do you have, and, what output do you want?