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: Beginner:
Labelled Break Statement Query

 



PerlMonkey
New User

Aug 22, 2014, 10:50 PM

Post #1 of 4 (3122 views)
Labelled Break Statement Query Can't Post

Hi guys,
I just started learning a bit of Perl this week and came across this code. So I guess the READ_NEW_FILE is just a label which is used in the code for breaking out of the while loop. What I'm not sure about is that this is the first time I have seen a label surrounding a while loop with braces?


Code
  READ_NEW_FILE:{ 
while(my $line = <NEW_FILE>) {
$line =~ s/(\r)|(\n)//g;
if($line =~ /import ([\w\.]+);/) {
my $library = $1;
$new_imported_libraries{$library} = $line;
} elsif($line =~ /\/\*\*/) {
$start_method = 1;
push @new_method_statements, $line;
$line_count++;
} else {
if($start_method && !$end_method) {
if($line =~ /^\}$/) {
# println "THIS IS THE END OF THE CLASS";
$end_method = 1;
last READ_NEW_FILE;
}
push @new_method_statements, $line;
$line_count++;
}
}
}
}


Is there any difference between the below two statements and in what situation would you want to use braces with your label?



Code
MYLABEL:{ 
while(){

}
}


and


Code
MYLABEL: 
while(){

}


thanks,
PerlMonkey


FishMonger
Veteran / Moderator

Aug 23, 2014, 7:11 AM

Post #2 of 4 (3111 views)
Re: [PerlMonkey] Labelled Break Statement Query [In reply to] Can't Post

The braces create a new block/scope. Any vars declared within that block/scope are only visible while you're inside that block.

In this case (and without addition code for context) there is no reason to use the braces, but using them doesn't do any harm.

If there was a var declared in the block prior to the while loop and that last conditional test never evaluated to true, then that last READ_NEW_FILE; statement which exits the block would never be executed. Given that case, the var declared at the beginning of the block would be accessible after the while loop.


Code
  READ_NEW_FILE:{ 

# this is only visible inside this block
my $var = '';

while(my $line = <NEW_FILE>) {

$var = int rand $.; # useless var assignment

$line =~ s/(\r)|(\n)//g;
if($line =~ /import ([\w\.]+);/) {
my $library = $1;
$new_imported_libraries{$library} = $line;
} elsif($line =~ /\/\*\*/) {
$start_method = 1;
push @new_method_statements, $line;
$line_count++;
} else {
if($start_method && !$end_method) {
if($line =~ /^\}$/) { # assume this is never true
# println "THIS IS THE END OF THE CLASS";
$end_method = 1;
last READ_NEW_FILE;
}
push @new_method_statements, $line;
$line_count++;
}
}
}
# this will print only if the earlier
# 'last READ_NEW_FILE' statement was never executed
print "$var\n";
}



Laurent_R
Veteran / Moderator

Aug 24, 2014, 4:33 AM

Post #3 of 4 (3079 views)
Re: [PerlMonkey] Labelled Break Statement Query [In reply to] Can't Post

In the specific case you've shown, not only are the braces useless (but, as pointed out by Fishmonger, they do no harm), but even the "READ_NEW_FILE:" label is useless, as a simple "last;" statement would suffice. Labels are really useful when you have nested loops and want to exit from several loop nesting levels in one go.

This is an brief (and perhaps somewhat clumsy) example in which using a label really makes sense (although the code below could probably be written differently, such as removing comments before splitting the line):


Code
READ_NEXT_LINE: 
while (my $line = <$file>) {
my @words = split /\s+/, $line;
for my $item (@words)
next READ_NEXT_LINE if $item =~ /^#/ ; # skip the rest of @words if it is comments
# ... further processing of $item
}
}


Here, if one of the $items start with '#', the rest of the line is a comment and we just want to skip the rest of the items and pick up the next input line.


PerlMonkey
New User

Aug 24, 2014, 4:06 PM

Post #4 of 4 (3013 views)
Re: [PerlMonkey] Labelled Break Statement Query [In reply to] Can't Post

Hi All,
Thanks for explaining that :)

 
 


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

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