Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Exiting given via next

Reply
Thread Tools

Exiting given via next

 
 
skendric@fhcrc.org
Guest
Posts: n/a
 
      04-22-2009
i'm fond of constructs like the following:

use Switch;
LINE:
while (my $line = <$fh>) {
switch ($line) {
case /Shrill and clear he crowed/ {
say 'shrill';
$count++;
next LINE;
}
case /Recking nothing of wizardry/ {
say 'recking';
$count++;
next LINE;
}
else {
next LINE;
}
}
}

but, switch has a memory leak (http://rt.cpan.org/Public/Bug/
Display.html?id=45232), which makes it unsuitable for sitting inside
loops which iterate many times

the new switch statement in perl-5.10.0 does not have this memory
leak:

use feature 'switch';
LINE:
given ($line) {
when (/Shrill and clear he crowed/) {
say 'shrill';
$count++;
next LINE;
}
when (/Recking nothing of wizardry/) {
say 'recking';
$count++;
next LINE;
}
default {
next LINE;
}
}

however, the 'switch' feature doesn't like seeing 'next':

gnat> ./daily-syslog-extracts
Parsing logfile...
Exiting when via next at ./daily-syslog-extracts line 179, <GEN0> line
7.
Exiting given via next at ./daily-syslog-extracts line 179, <GEN0>
line 7.
Exiting when via next at ./daily-syslog-extracts line 179, <GEN0> line
12.
Exiting given via next at ./daily-syslog-extracts line 179, <GEN0>
line 12.
[...]


is there some way to persuade the new 'switch' feature to tolerate
'next' statements? or at least to be quiet about its displeasure?

[i can fall back to a cascaded if/elsif structure ... but i find
'switch' cleaner to read, so much so that i'm having trouble letting
go of it]

--sk

stuart kendrick
fhcrc
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-22-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> i'm fond of constructs like the following:
>
> use Switch;
> LINE:
> while (my $line = <$fh>) {
> switch ($line) {
> case /Shrill and clear he crowed/ {
> say 'shrill';
> $count++;
> next LINE;
> }
> case /Recking nothing of wizardry/ {
> say 'recking';
> $count++;
> next LINE;
> }
> else {
> next LINE;
> }
> }
> }
>
> but, switch has a memory leak (http://rt.cpan.org/Public/Bug/
> Display.html?id=45232), which makes it unsuitable for sitting inside
> loops which iterate many times
>
> the new switch statement in perl-5.10.0 does not have this memory
> leak:
>
> use feature 'switch';
> LINE:
> given ($line) {
> when (/Shrill and clear he crowed/) {
> say 'shrill';
> $count++;
> next LINE;
> }
> when (/Recking nothing of wizardry/) {
> say 'recking';
> $count++;
> next LINE;
> }
> default {
> next LINE;
> }
> }
>
> however, the 'switch' feature doesn't like seeing 'next':
>
> gnat> ./daily-syslog-extracts
> Parsing logfile...
> Exiting when via next at ./daily-syslog-extracts line 179, <GEN0> line
> 7.
> Exiting given via next at ./daily-syslog-extracts line 179, <GEN0>
> line 7.
> Exiting when via next at ./daily-syslog-extracts line 179, <GEN0> line
> 12.
> Exiting given via next at ./daily-syslog-extracts line 179, <GEN0>
> line 12.
> [...]
>
>
> is there some way to persuade the new 'switch' feature to tolerate
> 'next' statements? or at least to be quiet about its displeasure?
>
> [i can fall back to a cascaded if/elsif structure ... but i find
> 'switch' cleaner to read, so much so that i'm having trouble letting
> go of it]


given/when/default is not a loop construct, just as if/unless/elsif/else
is not. next/last/redo will only work properly from inside a loop.
Perhaps you want to use *goto* instead?



John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      04-22-2009
On Wed, 22 Apr 2009 15:49:43 -0700 (PDT), (E-Mail Removed) wrote:

>i'm fond of constructs like the following:
>
>use Switch;
>LINE:
>while (my $line = <$fh>) {
> switch ($line) {
> case /Shrill and clear he crowed/ {
> say 'shrill';
> $count++;
> next LINE;
> }
> case /Recking nothing of wizardry/ {
> say 'recking';
> $count++;
> next LINE;
> }
> else {
> next LINE;
> }
> }
>}
>
>but, switch has a memory leak (http://rt.cpan.org/Public/Bug/
>Display.html?id=45232), which makes it unsuitable for sitting inside
>loops which iterate many times
>
>the new switch statement in perl-5.10.0 does not have this memory
>leak:
>
>use feature 'switch';
>LINE:


while (my $line = <$fh>) {

>given ($line) {
> when (/Shrill and clear he crowed/) {
> say 'shrill';
> $count++;
> next LINE;
> }


I don't have 5.10 , does given() replace the
while ($line = <$fh>) {
?
Otherwise, it might be infinite loop thing.

-sln
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-23-2009
>>>>> "M" == Mark <(E-Mail Removed)> writes:

M> His:

>>> while (my $line = <$fh>) {


M> IS a loop; a simple 'next;' (not 'next LINE;' per se) should be enough
M> to get the outer ''while' loop going (if the condition is still true, of
M> course).

but that was using the Switch module (which is evil since it does source
filtering). his second case using the 5.10 switch feature (built in and
not a source filter) didn't have a loop. he was wondering why next
didn't work inside the given. the answer (which is correct) is that
given is not a loop construct. if it was wrapped in a while as in the
first example, next would work.

> use feature 'switch';
> LINE:
> given ($line) {
> when (/Shrill and clear he crowed/) {


uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
skendric@fhcrc.org
Guest
Posts: n/a
 
      04-23-2009
hi uri,

i was being too terse ... in fact, the second case is the same as the
first (wrapped in a while loop) ... is there some way to suppress the
'Exiting via given/when' complaints?

use feature 'switch';
while (my $line = <$fh>) {
LINE:
given ($line) {
when (/Shrill and clear he crowed/) {
say 'shrill';
$count++;
next LINE;
}
when (/Recking nothing of wizardry/) {
say 'recking';
$count++;
next LINE;
}
default {
next LINE;
}
}
}

--sk

> but that was using the Switch module (which is evil since it does source
> filtering). his second case using the 5.10 switch feature (built in and
> not a source filter) didn't have a loop. he was wondering why next
> didn't work inside the given. the answer (which is correct) is that
> given is not a loop construct. if it was wrapped in a while as in the
> first example, next would work.

 
Reply With Quote
 
skendric@fhcrc.org
Guest
Posts: n/a
 
      04-23-2009
or am i doing something evil by trying to exit given and when clauses
using 'next'?

--sk
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-23-2009
>>>>> "s" == skendric <(E-Mail Removed)> writes:

s> i was being too terse ... in fact, the second case is the same as the
s> first (wrapped in a while loop) ... is there some way to suppress the
s> 'Exiting via given/when' complaints?

you have several mistakes and easy ways to fix this.

s> use feature 'switch';
s> while (my $line = <$fh>) {
s> LINE:

that label should be before the while. but that isn't the main bug.

s> given ($line) {
s> when (/Shrill and clear he crowed/) {
s> say 'shrill';
s> $count++;
s> next LINE;


from perldoc perlsyn:

Breaking out

You can use the "break" keyword to break out of the enclosing "given"
block. Every "when" block is implicitly ended with a "break".


but since you will exit the given block anyway in all cases, just fall
through and you will do the loop anyway. there is no need for the next
calls. i tested that. about the only time you will need to break from a
given is when you have to deeper inside a when clause.

s> }
s> when (/Recking nothing of wizardry/) {
s> say 'recking';
s> $count++;
s> next LINE;

delete that (and the previous) next and it works fine and quietly.

s> }
s> default {
s> next LINE;
s> }

that whole default clause isn't needed. you will exit the given block if
nothing is matched by a when and then you will loop as expected.

you need to read up more about given/when and see how it works by
default. you were trying too hard to exit when it exits for you!

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      04-23-2009
On Thu, 23 Apr 2009 06:22:25 -0700 (PDT), (E-Mail Removed) wrote:

>hi uri,
>
>i was being too terse ... in fact, the second case is the same as the
>first (wrapped in a while loop) ... is there some way to suppress the
>'Exiting via given/when' complaints?
>
>use feature 'switch';
>while (my $line = <$fh>) {
> LINE:
> given ($line) {
> when (/Shrill and clear he crowed/) {
> say 'shrill';
> $count++;
> next LINE;
> }


With 5.8.6 I get "Label not found..." with your block structure.
while (my $line = <DATA>)
{
LABEL:
@ar = map {
next LABEL if (++$i > 10); $_
} split (//, $line);
print "$i";
getc(STDIN);
next;
}
print "end\n";

But with label up here, I don't get that error.
LABEL:
while (my $line = <DATA>)
{
LABEL:
@ar = map {
...

-sln

 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      04-23-2009
On Thu, 23 Apr 2009 11:14:17 -0400, Uri Guttman <(E-Mail Removed)> wrote:

>>>>>> "s" == skendric <(E-Mail Removed)> writes:

>

[snip]
>from perldoc perlsyn:
>
> Breaking out
>
> You can use the "break" keyword to break out of the enclosing "given"
> block. Every "when" block is implicitly ended with a "break".
>
>
>but since you will exit the given block anyway in all cases, just fall
>through and you will do the loop anyway. there is no need for the next
>calls.


Unfortunately, you have to specify "continue" inside a when() to "fall through" to the next
when block, otherwise a "break" is implicit from a given(), like what you said, or a "next"
is implicit if the given is called from a loop block.

This is opposite of how the C switch/case works. There is no implicit "break" within a case block,
only fall through behavior, to the next case. A break is explicit from the switch.
In Perl now, to fall through, you have to explicitly continue, otherwise the break is implicit.

More confusion.

-sln

 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      04-23-2009
On Thu, 23 Apr 2009 09:18:29 -0700, (E-Mail Removed) wrote:

>On Thu, 23 Apr 2009 11:14:17 -0400, Uri Guttman <(E-Mail Removed)> wrote:
>
>>>>>>> "s" == skendric <(E-Mail Removed)> writes:

>>

>[snip]
>>from perldoc perlsyn:
>>
>> Breaking out
>>
>> You can use the "break" keyword to break out of the enclosing "given"
>> block. Every "when" block is implicitly ended with a "break".
>>
>>
>>but since you will exit the given block anyway in all cases, just fall
>>through and you will do the loop anyway. there is no need for the next
>>calls.

>
>Unfortunately, you have to specify "continue" inside a when() to "fall through" to the next
>when block, otherwise a "break" is implicit from a given(), like what you said, or a "next"
>is implicit if the given is called from a loop block.

^^^^^
when
>
>This is opposite of how the C switch/case works. There is no implicit "break" within a case block,
>only fall through behavior, to the next case. A break is explicit from the switch.
>In Perl now, to fall through, you have to explicitly continue, otherwise the break is implicit.
>
>More confusion.
>
>-sln


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exiting threads via signal Prince Al Perl Misc 6 11-12-2009 11:05 PM
Problem in exiting from a file and continue with next file Karthi kn Ruby 2 04-10-2008 01:44 PM
Days in a given date range for a given month......... Lord0 Java 1 04-19-2006 04:54 PM
CurrentElement->next = CurrentElement->next->next (UNDEFINED?) Deniz Bahar C Programming 2 03-09-2005 12:45 AM
How to make the program connect to next IP address without exiting program, if the current connection fail?? Abby C Programming 1 08-29-2003 03:08 AM



Advertisments