Perl - the First Day !

Getting Set up to work with Perl the First time on a Machine: Install Perl, DOS Setup

Starting a Perl Sessions - After the first time: DOS directory & path, Windows Editor

A sequence of Perl programs for beginners:

1 one liner
print "Hello World!";

 
2 New Line
print "\nHello World\n";

 
3 More lines

print "\n";
Hello World\n";
print "How is everyone?\n";

 
4 variable
$var1 = 16;
print $var1;

 
5 more variables


$var1 = 16;
print "\n var1 = $var1\n";
$anotherVar = 7;
$sum = $var1 + $anotherVar;
print "The sum of $var1 plus $var2 is $sum";

 
6 text variables


$varT = "This is a line of text";
print $varT;

 
7 errors
$varT ="This line does not end with a semicolon"

 
8 more text variables


$var1 = "First line";
$var2 = "second line";
print "Notice that these are not separate lines : $var1, $var2";

 
9 still more text variables
$var1 = "First line";
$var2 = "second line";
print "This is better:\n $var1 \n$var2\n";

 
10 User input (a)


print "Enter your name:";
$name = <STDIN>;

 
11 User input (b)


print "Enter your name:";
$name = <STDIN>;
print "\nHello $name, how are you?\n";

 
12 User input (c)
print "Enter your name:";
$name = <STDIN>;
chop ($name);
print "\nHello $name, how are you?\n";

 
  User input (c)

print "Enter your name:";
$name = <STDIN>;
chop ($name);
print "How old are you? ";
$age = <STDIN>;
$age2 = $age * 2;
print "\nHello $name, so you are $age2.\n";

 
  Loops (a)
$count = 0;
while ($count < 10)
    {
      print "This is loop number $count\n";
      $count = $count + 1;
     }
 
  loops (b)


print "Enter number of times to loop : ";
$loopNum = <STDIN>;
$count = 1;
while ($count < $loopNum)
    {
      print "This is loop number $count of $loopNum loops\n";
      $count = $count + 1;
     }
print "\nDo you see anything wrong with the above?\n";
print "\nHow would you fix it?\n"; #click here for answer

 
  Comments
print "Anything following a \# on a line is ignored\n"; #this is ignored
# and so is this
# print "fudge"; #and this line wont work till the first # is removed

 
  "If" statements (a)

$uMean = 42;
print "Enter a number for the meaning of the universe: ";
$uGuess = <STDIN>;
if ($uGuess  ==  $uMean)
         {
           print "\nNope! $uGuess is too high!\n";
          }
if ($uGuess  <  $uMean) {print "\nNope! $uGuess is too low!\n";}
if ($uGuess  ==  $uMean) {print "YEP! You got it! $uGuess is right!\n";

   # that funny looking == is two of these "=" in a row. In comparing numbers in Perl
   #      you can use ">", "<", "==", and "!=".   That last one, "!=" means "not equal to"

 
  "If" (b)
See if you can figure out how to modify the above program (If (a) ) so that the user keeps guessing until the right answer is guessed.   Hint: Use a loop! Click here to see the answer.

 
  "if  (c)
In program #   above you learned how to program a loop using a "while" statement.   Now that you know how to use "if" statements, see if you can build a loop without using a "while" statement.   Hint: Use an "if" statement!

 
   


If you have gotten through, and have some understanding of, all of the above, Congratualtions! You have covered three of the most important concepts in programming:

variables ( like $var1, $uGuess etc.)
loops (  while ($count < 10) ...)       Note there are a few other ways to make loops
"if" statements ( if ($uMean < $uGuess) { ... }

       Note: We only looked at comparing numbers with an "if" statement.  It turns out
                 that "if" statements can test for other conditions, such as comparing text,
                 or whether a file already exists,  or whether the computer has some particular
                 feature you would like to use.

 
       
   
The next lesson in this format will cover how to read text from a file and how to put text into a file.