visibility of a variable in Perl script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting visibility of a variable in Perl script.
# 1  
Old 12-18-2008
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->$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..

Last edited by Niroj; 12-18-2008 at 05:51 AM..
# 2  
Old 12-18-2008
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"

Quote:
The "foreach" loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn.
If the variable is preceded with the keyword "my", then it is lexically scoped, and is therefore visible only within the loop.
Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop.
If the variable was previously declared with "my", it uses that variable instead of the global one, but it's still localized to the loop.
This implicit localisation occurs only in a "foreach" loop.
Why do you need to see the variable in the foreach loop after the loop completes?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Variable visibility

Dear All, Saying, I have two distinct functions with the same goal (counting lines containing a specific pattern in a file MyFile). To perform that operation, I used a "while loop" with two different syntax ("grep" command would be much more better in that case but this is not the concern in... (8 Replies)
Discussion started by: dae
8 Replies

2. Shell Programming and Scripting

Passing variable from bash to perl script

Hi All, I need to pass a variable from bash script to perl script and in the perl script i am using those variables in the sql query but its giving error : Use of uninitialized value $ENV{"COUNTRYCD"} in concatenation (.) or string at /GIS_ROOT/custom/tables/DBread_vendor.pl line 50. Can ... (6 Replies)
Discussion started by: NileshJ
6 Replies

3. Shell Programming and Scripting

Error while reading variable from a file in perl script

I have a file abc.ini and declared many variables in that file, one of the variable(DBname) value I am trying to read in my perl script but getting error. File abc.ini content # database name DBname =UATBOX my $ex_stat; my $cmd_output; $ex_stat = "\Qawk '/^DBname/{print... (2 Replies)
Discussion started by: Devesh5683
2 Replies

4. Programming

Visibility of X11 windows

Does anyone know if it is possible to check whether a window in x11 (using xlib) is visible or not? I tried XGetWindowProperty, but that doesn't work, because the windows host (dtwm on Solaris 10) does not support EWMH. (5 Replies)
Discussion started by: JenniferKuiper
5 Replies

5. Shell Programming and Scripting

Using variable from shell script in perl file

Hey, So I have a shell script that outputs some variables, call them $a and $b. I know in shell scripting if I wanted to use the variables in another shell script I'd do sh code.sh "$a" "$b" How can I do something similar with perl? (2 Replies)
Discussion started by: viored
2 Replies

6. Shell Programming and Scripting

Script to convert csv file to html with good visibility

Hi, I have Below script which converts csv file to html succesfully.but the visiblity is simple in black n white. I want to have better visibilty of each columns in different colours(like green).As it is a Database report suppose some tablespace available space is less than 20% then it should... (7 Replies)
Discussion started by: sv0081493
7 Replies

7. Shell Programming and Scripting

Perl Variable Check Script

I am working on a perl script that is used to update a list of hosts to a certain file but I am having an issue when I try to perform a check to make sure the user enters valid information. The following is what I have currently written for the script: IPINPUT: print "Enter IP Address: ";... (2 Replies)
Discussion started by: Takau
2 Replies

8. Shell Programming and Scripting

Replacing variable value in perl script

hi I have this line in my perl script if (($cookie =~ /^cookie(.*)\/(.*)/) && ($ !~ /^cookie(10)\/(.*)|/)) { $cookienumber = "$1.$2"; } now if the result is cookie1/0, my $cookienumber would be 1.0 but if the result is cookie1/0/0 ... (5 Replies)
Discussion started by: tententen
5 Replies

9. Shell Programming and Scripting

Increment a date variable in perl script

Hi, I have a perl script which prints epoch value of date in milliseconds as the output. My reuirement is that once the output is printed,the day variable shld increment by 1 and when i execute the script for the second time the output shld be for the new day value. My script looks as... (11 Replies)
Discussion started by: jyothi_wipro
11 Replies

10. Shell Programming and Scripting

How to return a value of a variable from shell script to perl script

HI , Is there any way to return a value of variable from shell to perl script. Code: === Perl file my $diff1=system("sh diff.sh"); my $diff2=system("sh diff1.sh"); I need exit status of below commands i.e 0 and 1 respectively. Since in both the cases diff is working so system... (3 Replies)
Discussion started by: srkelect
3 Replies
Login or Register to Ask a Question