CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
goto - effect on the scope of variables

 



aravindprasad
Novice

Jul 8, 2009, 1:08 PM

Post #1 of 2 (1311 views)
goto - effect on the scope of variables Can't Post

Hi all,

I am in this situation where using goto-LABEL seems to be the best way to go forward. But I am completely lost on the scope of the variables in this method. Please look at the script below. Assume strict mode.


Code
01  @array = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 
02 for (my $var=0; $var<@array; $var++) {
03 # blah blah blah
04 if ($array[$var] == 2) {
05 goto PRINT;
06 INLOOP:
07 print "$array[$var]\n";
08 } else {
09 print "$array[$var]\n";
10 }
11 }
12 PRINT:
13 print "I got to PRINT\n";
14 unless ($var > @array) {
15 goto INLOOP;
16 }
17 print "I have reached the end. Exitting...\n";


I would like to know whether $var is valid in the "unless" statement in line 14 since it lies out side the for loop. Also I am curious whether goto can be used to jump between subroutines. Would the answers change if I were not in the strict mode?

Thanks and regards,
-Aravind


(This post was edited by aravindprasad on Jul 8, 2009, 1:12 PM)


FishMonger
Veteran / Moderator

Jul 8, 2009, 1:24 PM

Post #2 of 2 (1306 views)
Re: [aravindprasad] goto - effect on the scope of variables [In reply to] Can't Post

If you run the script in strict mode, then your answer to the 1st question would be obvious when you run the script.

The answer to your second question is in the second sentence of the perldoc for the goto function.

perldoc -f goto

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives