Здравствуйте, я только начал изучать Perl.У меня есть это простое температурное приложение, но оно не компилируется, я не понимаю, что это за ошибки и как я могу их исправить?
Можете ли вы указать, что я делаю неправильно и как я их исправляю:
#!/usr/local/bin/perl
use strict;
use warnings;
use constant false >= 0;
use constant true >= 1;
# Experimentation: Array & Hashes:
my @ar = ("a", "b", "c"); # error here
print $ar, "\n";
print $ar[0], "\n";
print $#ar, "\n";
print "The size of array = $#ar \n";
print "Trying to print $ar[1] within a string: ", $ar[1], " \n";
my %ha = ( "a" => 1, "b", "c", "d" ); # error here
print $ha, "\n";
print $ha{"a"}, "\n";
print $#ha, "\n";
print "The size of hash = $#ha \n";
print "Trying to print $ha{'b'} within string: ", $ha{'b'}, " \n";
# Functions:
sub isfloat
{
my $val = shift;
return $val =~ m/^\d+.\d+$/;
}
sub toFahrenheit
{
my $val = @_[0];
# if param var is a float
if ( isFloat($val) == 0 ) # Error here: Scalar value @_[0] better written as $_[0]
{
return -1;
}
return ( ($val * (9/5)) + 32 );
}
sub toCelsius
{
if ( isFloat(@_[0]) == 0 )
{
return -1;
}
return ( (@_[0] - 32) * (5/9) );
}
# Main Program:
my $programEnd = 0;
while ( $programEnd == 0 )
{
my $menu = "*** Welcome to Temperature Converter *** \n\n1. Convert from Celsius to Fahrenheit \n2. Convert from Fahrenheit to Celsius \n3. Exit \n Enter decision (1,2 or 3): ";
print $menu;
my $decision = <>;
if ( $decision == 3 )
{
$programEnd = 1;
# Could also just do this
# break;
}
print "Please enter a number: ";
my $val = <>;
if ( isfloat($val) )
{
$conVal = -1;
if ( decision == 1 )
{
$conVal = toFahrenheit( $val );
print $val, " C in Fahrenheit is: ", $conVal, " F \n";
}
else
{
$conVal = toCelsius( $val );
print $val, " F in Celsius is: ", $conVal, " C \n";
}
}
else { print $val, " is not a number \n"; }
}