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->$count: $pattern\\n"; \#coming correct
last;
\}
}
print "Inside while loop-> $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..