visibility of a variable in Perl script.

I am writing a script to cross check the dbscript. For that I am searching the SQL manipulators in the dbscript as shown below. But my problem is the variable $pattern is coming as null when comes out of the foreach loop.

File content:

vi /home2/niroj_p/dbscript.sql

.......................
......................
UPDATE ...............
.......................;

INSERT.........
..............;
UPADTE.................;

====================
My perl script:
--------
my @sql_manipulator_Q= ('UPDATE','INSERT','DELETE');
& get_sql_Query(\@sql_manipulator_Q);

sub get_sql_Query()
{
my $ref_pattern_Q=shift;
my $count=0;
my $pattern;
open FH0, "/home2/niroj_p/dbscript.sql" or die "Sorry, file doesn't exist !\n";

while ( my $line = <FH0>)
{
foreach $pattern (@{$ref_pattern_Q})
{

      if \($line =~ m/^$pattern/ \) 
 \{
  $count\+\+;
  print "Inside foreach-&gt;$count: $pattern\\n"; \#coming correct
   last;
 \}

}

 print "Inside while loop-&gt; $pattern\\n"; \#Coming as NULL 

}

}

output:

Inside while loop->
Inside while loop->
Inside while loop->
Inside while loop->
Inside while loop->
Inside while loop->
Inside foreach->1: UPDATE
Inside while loop->
Inside while loop->
Inside while loop->
Inside while loop->
Inside while loop->
Inside while loop->
Inside foreach->2: INSERT
Inside while loop->
Inside while loop->
Inside while loop->
Inside while loop->
Inside foreach->3: UPDATE
Inside while loop->
Inside while loop->

I want the variable $pattern should retain its value as in foreach loop.
please help..

First off. Please use code tags.

Always code 'use strict' and 'use warnings'.
If you did you would have caught the error in the definition of the function sub get_sql_Query.
You don't put parenthesis after it. Perl things you are trying to invoke it.

You didn't exactly cut and paste the exact code and output.
You misspelled one of the 'UPDATE' words in your sql script
and it should not have shown in the ouput.

Now that's out of the way.

From "perldoc perlsyn"

Why do you need to see the variable in the foreach loop after the loop completes?