Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to understand the how and why this script is working. 2

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
US
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(&quot;Please enter your age: &quot;);
$age = <STDIN>;
chomp($age);

print(greeting($name, $age));

sub greeting
{
$msg = &quot;Hello $_[0], &quot;;
determine_age($_[1],$msg);
}

sub determine_age
{
$num = ($_[0] <=> 18);
if ($num == -1)
{
return &quot;$_[1]you are under 18.($_[0])\n&quot;;
}
elsif ($num == 0)
{
return &quot;$_[1]you will be a 19 on your next birthday!\n&quot;;
}
else
{
return &quot;$_[1]you are over 18!($_[0])\n&quot;;
}
}


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 &quot;$_[1]you are under 18.($_[0])\n&quot;;
}

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
 
When a subroutine is called, the parameters are converted to a list and automatically assigned to the array $_. Thus within a subroutine $_[0] is the FIRST parameter passed to the subroutine, $_[1] is the SECOND, etc. Thus, within greeting, $_[0] has the value of $name, and $_[1] has the value of $age, since it was called as greeting($name, $age).

Then greeting set $msg to &quot;Hello $_[0], &quot; (i.e. &quot;Hello $name, &quot;) and calls determine_age as determine_age($_[1], $msg). So the value of $age is passed as the first parameter.

Within determine_age, $_[0] now contains the value of $age, and $_[1] contains the value of $msg. The compare sets $num to -1, 0 or 1 depending on whether $_[0] (aka $age) is less than, equal to, or greater than 18. Then it checks $num and returns a string depending on the value of $num. The statement
Code:
return &quot;$_[1]you are under 18.($_[0])\n&quot;;
will return the string &quot;Hello $name, you are under 18.($age)\n&quot;. The other return statements act similarly.

Once back in greeting, since there is no return statement, the subroutine will return the value of the last statement evaluated, which is this case is the call to determine_age, which returned the message above, so that is the value of the statement, so that is what will be returned from greeting.

The program then prints out the value returned by greeting, which is the message returned.

All that being explained, this is a HORRENDOUS example of perl code! Here are a few reasons why:

1. Since all greeting does is change $name to &quot;Hello $name, &quot; which is then passed into determine age, it can be completely done away with. The message returned by determine age could easily be modified to include the extra text and punctuation.

2. The implied return in greeting is sloppy. And confusing since the programmer used return in determine age.

3. By using $_[0], etc. the programmer may think s/he is being efficient, but the code would be much clearer if the parameter were assigned to named variables.

Here's a MUCH clearer example:
Code:
#!/usr/bin/perl

print(&quot;Please enter your name: &quot;);
$name = <STDIN>;
chomp($name);
print(&quot;Please enter your age: &quot;);
$age = <STDIN>;
chomp($age);

print(greeting($name, $age));

sub greeting
{
  my($name, $age) = @_; # assign parameters to named vars
  $num = ($age <=> 18);
  if ($num == -1)
  {
    return &quot;Hello $name, you are under 18. ($age)\n&quot;;
  }
  elsif ($num == 0)
  {
    return &quot;Hello $name, you will be a 19 on your next birthday!\n&quot;;
  }
  else
  {
    return &quot;Hello $name, you are over 18! ($age)\n&quot;;
  }
}
&quot;There's more than one way to do it&quot; usually means there's a better, or clearer, way. Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
i should point out that the first line of mr/s. dragon's response had a little typo. the values passed into a subroutine are assigned to the array '@_', and you then access them exactly how he said to. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Thanks stillflame, you are, of course, correct.

And for the record, it's Mr. Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I would personally like to thank the two of you, both tsdragon and stillflame.

You both have been absolutely WONDERFUL in you help. I can't even begin to express my thanks.

:))

It is due to considerate people like yourselves that others, like me, have the opportunity to obtain the answers to questions and challenges that otherwise would remain a blur 'in the world of programming' for eternity.

Thanks again,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
tsdragon,

I just had to tell you .. after I got a chance to take a good look at your reply to my question.

WOW!! Thanks for such an indepth explanation. It really, really, really helped!! Even the example re-write. I just had to let you know how much I appreciated the answer and help.

:)))))

Gary

THANKS AGAIN!
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
:~/ You're more than welcome. That's what we're here for. I get a great deal of satisfaction out of knowing I helped someone out, and sometimes I learn something new in the process. I glad I could be of assistance.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top