garymgordon
Programmer
Here is the script. I will include my questions at the end.
#!/usr/bin/perl
print("Please enter your name: "
;
$name = <STDIN>;
chomp($name);
print("Please enter your age: "
;
$age = <STDIN>;
chomp($age);
print(greeting($name, $age));
sub greeting
{
$msg = "Hello $_[0], ";
determine_age($_[1],$msg);
}
sub determine_age
{
$num = ($_[0] <=> 18);
if ($num == -1)
{
return "$_[1]you are under 18.($_[0])\n";
}
elsif ($num == 0)
{
return "$_[1]you will be a 19 on your next birthday!\n";
}
else
{
return "$_[1]you are over 18!($_[0])\n";
}
}
QUESTIONS:
1) What is this line saying? I am unclear on this.
$num = ($_[0] <=> 18);
2) In this line, I understand (if I am correct) that if the user enters a value less than 18, than the answer is evaluted as true. My reason for saying true, is that the <=> compares three possibilities. And, -1 is given if $a is less than $b. So the statement is executed. But I don't understand the statement. Here is the line(s) I don't understand.
if ($num == -1)
{
return "$_[1]you are under 18.($_[0])\n";
}
What is happening with $_[1]? What value is being given to $_[1], ... and where is it being returned too?
Next ... what is happening with $_[0]?? What value is being given to $_[0] and WHY?
I have run the script and can see what is happening, but I don't understand the how or why each line is being executed.
Please help me understand this script from about the line of print(greeting($name, $age)); on down.
Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
#!/usr/bin/perl
print("Please enter your name: "
$name = <STDIN>;
chomp($name);
print("Please enter your age: "
$age = <STDIN>;
chomp($age);
print(greeting($name, $age));
sub greeting
{
$msg = "Hello $_[0], ";
determine_age($_[1],$msg);
}
sub determine_age
{
$num = ($_[0] <=> 18);
if ($num == -1)
{
return "$_[1]you are under 18.($_[0])\n";
}
elsif ($num == 0)
{
return "$_[1]you will be a 19 on your next birthday!\n";
}
else
{
return "$_[1]you are over 18!($_[0])\n";
}
}
QUESTIONS:
1) What is this line saying? I am unclear on this.
$num = ($_[0] <=> 18);
2) In this line, I understand (if I am correct) that if the user enters a value less than 18, than the answer is evaluted as true. My reason for saying true, is that the <=> compares three possibilities. And, -1 is given if $a is less than $b. So the statement is executed. But I don't understand the statement. Here is the line(s) I don't understand.
if ($num == -1)
{
return "$_[1]you are under 18.($_[0])\n";
}
What is happening with $_[1]? What value is being given to $_[1], ... and where is it being returned too?
Next ... what is happening with $_[0]?? What value is being given to $_[0] and WHY?
I have run the script and can see what is happening, but I don't understand the how or why each line is being executed.
Please help me understand this script from about the line of print(greeting($name, $age)); on down.
Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer