I can't stop the scoping problem (perl)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I can't stop the scoping problem (perl)
# 1  
Old 11-16-2009
I can't stop the scoping problem (perl)

I hate to post something so n00bish, but I'm pretty n00bish when it comes to perl so here it goes:

Code:
$var=1;

while(1){ 

   if ($var == 2){
      last;
   }

   $var=2;
}

works the way I intend it to, breaking the infinite loop after the first time through, but:

Code:
while(1){

   $var=1;

   if ($var == 2){
      last;
   }

   $var=2;

}

results in what I can only assume is an infinite loop. My question is what changes scope-wise to the if statement when $var is instantiated outside of the while loop?

update:
Title is supposed to be "spot" not "stop" but apparently I'm dyslexic.

Last edited by thmnetwork; 11-16-2009 at 12:15 AM..
# 2  
Old 11-16-2009
Code:
$var=1;

while(1){ 

   if ($var == 2){
      last;
   }

   $var=2;
}

In the above
1) step 1 var =1 then goes to while loop
2) In the while loop in the first check var != 2 so doesnot get into the if loop then sets var = 2
3) In the next attempt var is 2 goes into if loop and the while loop ends

Now lets check the second block of code
Code:
while(1){

   $var=1;

   if ($var == 2){
      last;
   }

   $var=2;

}

1) goes into the while loop then sets var = 1 and checks if var == 2 so doesnot enter into the if loop
2) sets var =2 and then again goes to the while loop begining

3) Now here what is happening is it sets the variable var to 1 again . the condition of var=2 is never met. Hence the infinite loop.

Regarding the scope in the first instance if you print var outside the while loop it would be 1
in the second instance depending on where you declared the variable var like my $var will determine the value.

HTH,
PL
# 3  
Old 11-16-2009
thanks, I don't know how I missed that, I was running on fumes at that point of the night. Thanks for indulging me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - concatenate string - strange variable scoping

Hello, I am trying to concatenate a string in a bash script like this: runCmd="docker run -e \"IMAGE_NAME=$IMAGE_NAME\" " env | grep "$ENV_SUFFIX" | while read line; do envCmd="-e \"${line}\" " runCmd=$runCmd$envCmd echo $runCmd # here concatenation works fine done echo... (3 Replies)
Discussion started by: czabak
3 Replies

2. Solaris

Stop/start script problem on Solaris-10

I have Big brother script, which start/stop Big Brother processes. Something got change on server and now I am not able to start/stop it. There is no change in script, as I compared it from other server. This service is being managed by bb user (group is also bb). root@tsazdq04:/#... (6 Replies)
Discussion started by: solaris_1977
6 Replies

3. Shell Programming and Scripting

Stop Window Services with Perl Script

Hello All I am getting an issue which I want to share will all you guys There is one windiows sercice running on my machine names Network Provisioning Service. I developed the perl script which do $service='Network Provisioning Service'; system('net stop $service'); When I manually... (1 Reply)
Discussion started by: adisky123
1 Replies

4. Web Development

problem with exporting vairable from one perl cgi to another perl cgi script while redirecting.

Can anyone tell me how to export a variable from one perl CGI script to another perl cgi script when using a redirect. Upon running the login.pl the user is prompted to enter user name and password. Upon entering the correct credentials (admin/admin) the user is redirected to welcome page. My... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

5. Shell Programming and Scripting

Shell variable scoping

This may be a stupid question, but was wondering if it is possible to make a variable local to a particular script and invisible to an external script that may source the script where it is defined? Thanks as always (2 Replies)
Discussion started by: stevensw
2 Replies

6. Red Hat

Problem with Stop Script during shutdown of o/s

Friends , I create two script for oracle database startup and shutdown . $ cat start.sh ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1 ORACLE_OWNER=oracle su - oracle << ! sqlplus / as sysdba << ! startup $ cat stop.sh ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1... (1 Reply)
Discussion started by: shipon_97
1 Replies

7. Shell Programming and Scripting

problem with perl

hi, i have a script that coverts the file time in epoch time.but the problem is perl is not working inside the k-shell ---------------------------------------------------------------- #!/bin/ksh echo "Enter the file" read n perl -e 'print ((stat("n")))'... (6 Replies)
Discussion started by: ali560045
6 Replies

8. AIX

How to stop perl service

how to stop perl service on AIX? how to find if per service runing? (4 Replies)
Discussion started by: rainbow_bean
4 Replies

9. UNIX for Advanced & Expert Users

how to stop others users to stop viewing what i am doing ?

Hi , I have one question, suppose i am a normal user and when i use 'w' command , it shows who is logged on and what they are doing . Now i want to stop others users to know what i am doing accept the root ? can i do this ? thanks (5 Replies)
Discussion started by: mobile01
5 Replies

10. Shell Programming and Scripting

ksh/Linux: Variable scoping issue? Pl. help!

user_account() { set -x nodename=$1 # set userid to user0 userid="user0" echo outside:pid $$ cat $MY_DIR/user_accounts | while read line do # line="node1 user1" echo inside do: pid $$ line:$line userid:$userid poss_node=`echo $line |awk '{print $1}'`... (2 Replies)
Discussion started by: jasmeet100
2 Replies
Login or Register to Ask a Question