S
skendric
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
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