perl magic: Use if/elsif/else instead of given/when (FS #12188)

Unbreaks the maemo build.

Patch by Nick Peskett with a small comment added
as suggested by Dominik Riebeling.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30323 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Thomas Jarosch 2011-08-16 19:26:24 +00:00
parent 2e154df92f
commit 121b26e62f
2 changed files with 90 additions and 97 deletions

View file

@ -1,5 +1,4 @@
#!/usr/bin/perl #!/usr/bin/perl
use feature "switch";
use List::Util 'shuffle'; # standard from Perl 5.8 and later use List::Util 'shuffle'; # standard from Perl 5.8 and later
my $tempfile = "multigcc.out"; my $tempfile = "multigcc.out";
@ -26,23 +25,22 @@ my $command = join " ", @params;
# count number of cores # count number of cores
my $cores; my $cores;
given ($^O) { # Don't use given/when here - it's not compatible with old perl versions
when ("darwin") { if ($^O eq 'darwin') {
chomp($cores = `sysctl -n hw.ncpu`); chomp($cores = `sysctl -n hw.ncpu`);
$cores = 1 if ($?); $cores = 1 if ($?);
}
elsif ($^O eq 'solaris') {
$cores = scalar grep /on-line/i, `psrinfo`;
$cores = 1 if ($?);
}
else {
if (open CPUINFO, "</proc/cpuinfo") {
$cores = scalar grep /^processor/i, <CPUINFO>;
close CPUINFO;
} }
when ("solaris") { else {
$cores = scalar grep /on-line/i, `psrinfo`; $cores = 1;
$cores = 1 if ($?);
}
default {
if (open CPUINFO, "</proc/cpuinfo") {
$cores = scalar grep /^processor/i, <CPUINFO>;
close CPUINFO;
}
else {
$cores = 1;
}
} }
} }

View file

@ -17,7 +17,6 @@
use strict; use strict;
use warnings; use warnings;
use feature 'switch';
use File::Basename; use File::Basename;
use File::Copy; use File::Copy;
use vars qw($V $C $t $l $e $E $s $S $i $v); use vars qw($V $C $t $l $e $E $s $S $i $v);
@ -74,36 +73,35 @@ sub init_tts {
our $verbose; our $verbose;
my ($tts_engine, $tts_engine_opts, $language) = @_; my ($tts_engine, $tts_engine_opts, $language) = @_;
my %ret = ("name" => $tts_engine); my %ret = ("name" => $tts_engine);
given ($tts_engine) { # Don't use given/when here - it's not compatible with old perl versions
when ("festival") { if ($tts_engine eq 'festival') {
print("> festival $tts_engine_opts --server\n") if $verbose; print("> festival $tts_engine_opts --server\n") if $verbose;
my $pid = open(FESTIVAL_SERVER, "| festival $tts_engine_opts --server > /dev/null 2>&1"); my $pid = open(FESTIVAL_SERVER, "| festival $tts_engine_opts --server > /dev/null 2>&1");
my $dummy = *FESTIVAL_SERVER; #suppress warning my $dummy = *FESTIVAL_SERVER; #suppress warning
$SIG{INT} = sub { kill TERM => $pid; print("foo"); panic_cleanup(); }; $SIG{INT} = sub { kill TERM => $pid; print("foo"); panic_cleanup(); };
$SIG{KILL} = sub { kill TERM => $pid; print("boo"); panic_cleanup(); }; $SIG{KILL} = sub { kill TERM => $pid; print("boo"); panic_cleanup(); };
$ret{"pid"} = $pid; $ret{"pid"} = $pid;
} }
when ("sapi") { elsif ($tts_engine eq 'sapi') {
my $toolsdir = dirname($0); my $toolsdir = dirname($0);
my $path = `cygpath $toolsdir -a -w`; my $path = `cygpath $toolsdir -a -w`;
chomp($path); chomp($path);
$path = $path . '\\'; $path = $path . '\\';
my $cmd = $path . "sapi_voice.vbs /language:$language $tts_engine_opts"; my $cmd = $path . "sapi_voice.vbs /language:$language $tts_engine_opts";
$cmd =~ s/\\/\\\\/g; $cmd =~ s/\\/\\\\/g;
print("> cscript //nologo $cmd\n") if $verbose; print("> cscript //nologo $cmd\n") if $verbose;
my $pid = open2(*CMD_OUT, *CMD_IN, "cscript //nologo $cmd"); my $pid = open2(*CMD_OUT, *CMD_IN, "cscript //nologo $cmd");
binmode(*CMD_IN, ':encoding(utf16le)'); binmode(*CMD_IN, ':encoding(utf16le)');
binmode(*CMD_OUT, ':encoding(utf16le)'); binmode(*CMD_OUT, ':encoding(utf16le)');
$SIG{INT} = sub { print(CMD_IN "QUIT\r\n"); panic_cleanup(); }; $SIG{INT} = sub { print(CMD_IN "QUIT\r\n"); panic_cleanup(); };
$SIG{KILL} = sub { print(CMD_IN "QUIT\r\n"); panic_cleanup(); }; $SIG{KILL} = sub { print(CMD_IN "QUIT\r\n"); panic_cleanup(); };
print(CMD_IN "QUERY\tVENDOR\r\n"); print(CMD_IN "QUERY\tVENDOR\r\n");
my $vendor = readline(*CMD_OUT); my $vendor = readline(*CMD_OUT);
$vendor =~ s/\r\n//; $vendor =~ s/\r\n//;
%ret = (%ret, %ret = (%ret,
"stdin" => *CMD_IN, "stdin" => *CMD_IN,
"stdout" => *CMD_OUT, "stdout" => *CMD_OUT,
"vendor" => $vendor); "vendor" => $vendor);
}
} }
return \%ret; return \%ret;
} }
@ -111,15 +109,13 @@ sub init_tts {
# Shutdown TTS engine if necessary. # Shutdown TTS engine if necessary.
sub shutdown_tts { sub shutdown_tts {
my ($tts_object) = @_; my ($tts_object) = @_;
given ($$tts_object{"name"}) { if ($$tts_object{'name'} eq 'festival') {
when ("festival") { # Send SIGTERM to festival server
# Send SIGTERM to festival server kill TERM => $$tts_object{"pid"};
kill TERM => $$tts_object{"pid"}; }
} elsif ($$tts_object{'name'} eq 'sapi') {
when ("sapi") { print({$$tts_object{"stdin"}} "QUIT\r\n");
print({$$tts_object{"stdin"}} "QUIT\r\n"); close($$tts_object{"stdin"});
close($$tts_object{"stdin"});
}
} }
} }
@ -146,48 +142,47 @@ sub voicestring {
our $verbose; our $verbose;
my ($string, $output, $tts_engine_opts, $tts_object) = @_; my ($string, $output, $tts_engine_opts, $tts_object) = @_;
my $cmd; my $cmd;
printf("Generate \"%s\" with %s in file %s\n", $string, $$tts_object{"name"}, $output) if $verbose; my $name = $$tts_object{'name'};
given ($$tts_object{"name"}) { printf("Generate \"%s\" with %s in file %s\n", $string, $name, $output) if $verbose;
when ("festival") { if ($name eq 'festival') {
# festival_client lies to us, so we have to do awful soul-eating # festival_client lies to us, so we have to do awful soul-eating
# work with IPC::open3() # work with IPC::open3()
$cmd = "festival_client --server localhost --otype riff --ttw --output \"$output\""; $cmd = "festival_client --server localhost --otype riff --ttw --output \"$output\"";
# Use festival-prolog.scm if it's there (created by user of tools/configure) # Use festival-prolog.scm if it's there (created by user of tools/configure)
if (-f "festival-prolog.scm") { if (-f "festival-prolog.scm") {
$cmd .= " --prolog festival-prolog.scm"; $cmd .= " --prolog festival-prolog.scm";
}
print("> $cmd\n") if $verbose;
# Open command, and filehandles for STDIN, STDOUT, STDERR
my $pid = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, $cmd);
# Put the string to speak into STDIN and close it
print(CMD_IN $string);
close(CMD_IN);
# Read all output from festival_client (because it LIES TO US)
while (<CMD_ERR>) {
}
close(CMD_OUT);
close(CMD_ERR);
} }
when ("flite") { print("> $cmd\n") if $verbose;
$cmd = "flite $tts_engine_opts -t \"$string\" \"$output\""; # Open command, and filehandles for STDIN, STDOUT, STDERR
print("> $cmd\n") if $verbose; my $pid = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, $cmd);
`$cmd`; # Put the string to speak into STDIN and close it
} print(CMD_IN $string);
when ("espeak") { close(CMD_IN);
$cmd = "espeak $tts_engine_opts -w \"$output\""; # Read all output from festival_client (because it LIES TO US)
print("> $cmd\n") if $verbose; while (<CMD_ERR>) {
open(ESPEAK, "| $cmd");
print ESPEAK $string . "\n";
close(ESPEAK);
}
when ("sapi") {
print({$$tts_object{"stdin"}} "SPEAK\t$output\t$string\r\n");
}
when ("swift") {
$cmd = "swift $tts_engine_opts -o \"$output\" \"$string\"";
print("> $cmd\n") if $verbose;
system($cmd);
} }
close(CMD_OUT);
close(CMD_ERR);
}
elsif ($name eq 'flite') {
$cmd = "flite $tts_engine_opts -t \"$string\" \"$output\"";
print("> $cmd\n") if $verbose;
`$cmd`;
}
elsif ($name eq 'espeak') {
$cmd = "espeak $tts_engine_opts -w \"$output\"";
print("> $cmd\n") if $verbose;
open(ESPEAK, "| $cmd");
print ESPEAK $string . "\n";
close(ESPEAK);
}
elsif ($name eq 'sapi') {
print({$$tts_object{"stdin"}} "SPEAK\t$output\t$string\r\n");
}
elsif ($name eq 'swift') {
$cmd = "swift $tts_engine_opts -o \"$output\" \"$string\"";
print("> $cmd\n") if $verbose;
system($cmd);
} }
} }