私は単位をインチに変換するはずのこのperlプログラムを持っています。しかし、ループ中に変数を出力することはありません。それはちょうど、標準は「に等しく、」印刷し、$ユニット2は、ここに私のコードここPerlのテキスト解析変数が印刷に失敗する
#!/usr/bin/perl
#usage: perl convert.pl
use Path::Class;
use warnings;
use autodie; # die if problem reading or writing a file
my $dir = dir("./");
my $file = $dir->file("distances.txt");
# Read in the entire contents of a file
my $content = $file->slurp();
# openr() returns an IO::File object to read from
my $file_handle = $file->openr();
# Read in line at a time
while(my $line = $file_handle->getline()) {
# parses string into value and unit
my ($val, $unit) = split /[\n]/, $line;
# initialize values
my ($val2, $unit2) = "";
# check unit and apply corresponding math
if ($unit eq "in") {
$val2 = $val * 2.54;
$unit2 = "cm";
}
else {
if ($unit eq "ft") {
$val2 = $val * 0.3048;
$unit2 = "m";
}
else {
if ($unit eq "yd") {
$val2 = $val * 0.9144;
$unit2 = "m";
}
else {
if ($unit eq "mi") {
$val2 = $val * 1.609347;
$unit2 = "km";
}#close mile if statement
}#close else statement
}#close 2nd else statement
}#close 1st else statement
print $val, " ", $unit, " is equal to ", $val2, " ", $unit2, "\n";
}
は、私が使用しているテキストファイルです
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 1.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 2.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 3.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 4.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 5.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 6.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 7.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 8.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 9.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 10.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 11.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 12.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 13.
is equal to
Use of uninitialized value $unit2 in print at convert.pl line 47, <GEN1> line 14.
is equal to
初期化されていないと述べています。
74 in
10 in
10 ft
10 yd
10 mi
72 in
6 ft
2 yd
300 ft
100 yd
158400 in
13200 ft
4400 yd
2.5 mi
'警告を使用;' – toolic
https://metacpan.org/pod/Math::Units – toolic
* **必ず*** 'all'' – Borodin