How to Write Perl Script
Perl scripts are very common in the programming world, especially for programs that must manage large amounts of text, such as indexers. Perl scripts can be very easy or very difficult depending on what your goals are. For example, they can be developed to carry out specific tasks such as renaming or deleting files. Scripts that use Perl modules are very convenient as they can be programmed to do anything from checking local gas prices to updating a database right from the Windows command prompt. This article demonstrates how to create the essential elements of Perl scripting. These are scalar, array and hash variables and the "print" command. Once you have the basics, developing more sophisticated programs will come much easier.
- Difficulty:
- Moderately Easy
Instructions
-
-
1
Create a scalar variable. To create a scalar variable you must name the variable and assign it a value and then type a ";" character. If you want a text value it must be written between quotation marks. Numerical values do not need quotation marks. For example: $name = "John Anderson"; $age = 35;
-
2
Create an array variable. The difference between an array and a scalar variable is that an array can hold multiple bits of information. To create an array, type the "@" sign, the name of the array and then the values of the array within "'()" characters. Each value must be separated by commas. Text values must be written between quotation marks. For example: @Anderson_family = ("John","Mary", "Julie", "Mark"); @ages = (35,30,7,5);
-
3
Create a hash variable. Hash variables are similar to array variables except that they include a "key" and a "value." They begin with a "%" sign and are followed by the name of the variable. The values for the hash are placed within "()" and are separated by commas. However, each value in a hash consists of a key and a value. For example: %Famly_age = ("John" => 35, "Mary" => 30, "Julie" => 7, "Mark" => 5); This means that John is 35, Mary is 30 and so on. When the information from a hash is needed it is called through a scalar variable + the key, for example: "$Family_age{John}" will equal the value of 35.
-
4
Use the print command. The print command is responsible for output in Perl. To utilize it you must type "print" and then the desired output. For example: "print $name" will output "John Anderson" because you assigned that value to "$name" in Step 1. Likewise, Print @Anderson_family will output all of the names that were assigned to this array.
-
5
Create a program. This program will output some of the variables you created. To do this type the following;
#!/usr/bin/perl
$name = "John Anderson";
$age = 35;
@Anderson_family = ("John","Mary", "Julie", "Mark");
%Family_age = ("John" => 35, "Mary" => 30, "Julie" => 7, "Mark" => 5);
print "$name is $age years old\n";
print "@Anderson_family\n";
print "Mary is $Family_age{Mary} years old and Mark is $Family_age{Mark} years old";
The '\n' character makes sure that a new line follows what is printed out. Save the program as "family.pl" in a known directory. - 6
- 7
-
8
Design your own program. Change the variables around the way you would like them. Add different values and perform different functions. For example, you could add the ages of some family members by typing $Age = $Family_age{Mary} + $Family_age{Mark }; Have fun and get creative.
-
1
Tips & Warnings
Most errors in Perl are made because of mistyping. Make sure to check your syntax meticulously. One missing ";" and the program will fail. Once you are familiar with the basics you can start to design more interesting and complicated programs.
Related Searches
References
- Photo Credit http://images.amazon.com/images/P/0596000278.01._SCLZZZZZZZ_.jpg