Perl - Part 5

[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]

Programming Component Continued

Create a file called adder.pl with this code in it:

#!/usr/bin/perl
  use Term::ReadKey;
  print "Enter two numbers and I'll add them:\n";
  print "First number: ";
  $num1 = ReadLine;
  print "Second number: ";
  $num2 = ReadLine;
  $sumof = $num1 + $num2;
  print "The answer is: $sumof.\n";

Before you run the program using the command perl adder.pl you should be aware that there is more than one way to run a perl program. Up until now you have been told to issue the perl command before the name of the file containing the program. This invoked the perl interpreter, fed it your program and all went well. If you issue the command ls -l in the perl_progs directory you should see a list of the current files in that directory/folder. It will look a little like this:

Note that your normal screen display may or may not include colour. If you are using dtelnet this may be configurable. In any case, in this example you should notice that some files are in green and some are in black. Also, on the left hand side the security permissions show varying patterns of rwx for each of owner, group, world. The file adder.pl has no x's beside it; this means it cannot be run directly. You must use the perl interpreter prefix to run this file in its current form. The first line of the files you've made so far has been #!/usr/bin/perl which is the location of the Perl interpreter. So instead of using the perl prefix, we should be able to run the perl files we create directly - without doubly specifying the location of the perl interpreter. To do this, change the mode of the file you've just created using the command
chmod +x adder.pl
to allow the file to be executed directly. This is the preferred method of operation. You can now run the program by using the command
./adder.pl
to run the file directly. The ./ means "in this directory". Try issuing the command without the ./ and see the error message caused by its absence! When you run the program you should get output like this:
Note how the \n generated by pressing return doesn't need to be removed from the numbers you type. Perl is understaning/forgiving/clever like that and knows how to cope with the input.

Variables

When working with data in a program it is normally necessary to store that data temporarily as the program runs. The data is stored in what are called variables. They are called variables because the contents of the variable can be changed as the program runs - the contents can vary. (There are also what are called constants; these as the name implies remain unchanged throughout the program.) In all programming languages there are several types of data variables, and very often you need to know about the data type before the program starts; this is because you have to pre-declare the variables before you can use them. Perl is more forgiving than this. There is no need to pre-declare variables: when you use them the computer makes storage space for them automatically. You also don't need to make an explicit statement of what type of data (numeric, text, etc) to be stored. Perl works that out and proceeds accordingly. This also is unusual - and easier - compared to other programming languages. In Perl the most significant aspect of data storage is the choice of: So far you have only used scalar variables - which are identified by the $ that proceeds them. If you can identify the variables in a program, you should be able to identify the number of unique variables used in the adder.pl program above. Meantime move onwards to try some sample challenge programs......

[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]

Last updated: 20131015-16:45