英語の文章を豚ラテン語に変換するプログラムを (退屈なので) 作成しているところです。コードは非常にうまく見えますが、文を入力するとエラーがスローされます。なぜエラーが発生するのかわかりません。
コード:
use strict;
use warnings;
sub to_pig_latin
{
my $sentence = shift;
my @words = split(" ", $sentence);
my $translated = " ";
my $cw;
for(my $i = 0; $i < scalar(@words); $i++)
{
my $word = $words[$i];
if($word eq "." or $word eq "!" or $word eq "?")
{
}
else
{
$cw = substr(1, length($word)-1, $word);
$translated = $translated.$cw.substr(0,1,$word)."ay"." ";
}
}
return $translated;
}
print "Welcome to English -> Pig Latin Converter.\n";
print "Enter an English sentence to translate:\n";
my $to_translate = <STDIN>;
chomp($to_translate);
print " ";
my $translated = to_pig_latin($to_translate);
print "Translated sentence:$translated\n";
エラー:
Welcome to English -> Pig Latin Converter.
Enter an English sentence to translate:
Hello World!
Argument "Hello" isn't numeric in substr at pig_latin.pl line 18, <STDIN> line 1.
substr outside of string at pig_latin.pl line 18, <STDIN> line 1.
Use of uninitialized value $cw in concatenation (.) or string at pig_latin.pl line 19, <STDIN> line 1.
Argument "World!" isn't numeric in substr at pig_latin.pl line 18, <STDIN> line 1.
substr outside of string at pig_latin.pl line 18, <STDIN> line 1.
Use of uninitialized value $cw in concatenation (.) or string at pig_latin.pl line 19, <STDIN> line 1.
Translated sentence: ay ay