Modifying variables
Previous  Top  Next

1: $firstname = "Heathcliff";
2: $lastname = "Hudginston";
3: # PROMPT FOR INPUT
4: print "Your age please $firstname $lastname: ";
5: $myage = <STDIN>;
6: 
7: # REMOVE NEW LINE
8: chomp($myage);
9:
10:# INCREASE BY ONE
11:$myage++;
12:
13:# OUTPUT
14:print "\n\nHello $firstname,\n";
15:print "You will turn $myage on your next birthday.";

This should look like this:

Your age please Heathcliff Hudginston: 65
Hello Heathcliff,
You will turn 66 on your next birthday.

Allright, now what is <STDIN> and why do we use chomp ?

*) <STDIN> (line 8) prompts for input and stores the value in the according variable
*) chomp (line 11) removes the new line character that was appended to the variable $myage when we hit the ENTER button on the keyboard
*) $myage++ is the same as $myage = $myage + 1. You use this with other operators like - and so on as well.