PERL COMMAND PROMPT
PERL - Programming
PERL was
designed to be simple and direct. We outlined earlier that file manipulation
and grep style functions are the bread and butter of PERL. Often times
these types of tasks will require some form of user input. PERL can do exactly
that (imagine that) and these next few pages of tutorial will walk you through
the process step by step.
PERL - User Input
PERL is capable of quickly taking input from a user then manipulating it in some fashion and spitting out some sort of result. Whether the task at hand is complex file manipulation or just a simple conversion script, your script may require some sort of user-input. With PERL this input comes from the command prompt or MS-DOS on a windows machine.In a minute we will be taking a look at how to retrieve some user input via the command prompt but first, you may need to install a PERL compiler for your machine.
ActivePERL - PERL for Windows
PERL comes pre-installed on many operating systems, like Linux for example. For those of us running a version of Windows however, we must download and install some sort of program that allows us to access PERL scripts from DOS. There are many PERL compilations available for your machine. A simple Google search yields several million results.ActivePERL is the simplest of installers available and it is also free to download. Download the program, follow the onscreen installation guide, and you should have an active installation of PERL ready to go.
To test the installation, make sure the program has fully completed installation and then run through the following:
- Click on Start, go to Run.
- Enter Command or cmd into the display box.
- At the command prompt type perl -v.
PERL - Testing...Testing Script
Just to be sure everything is running correctly. Open up notepad or your preferred simple text editor and let's make a beginning script. We'll have our script print to the command prompt with the print function just as we would if we were printing text to our web browser.testingtesting.pl:
#! usr/bin/perl
print "Hello PERL!";
Copy the two lines above and save them in a directory that
you are capable of accessing through DOS. Locate that directory in your DOS
prompt and simply type the name of your PERL script into the command prompt to
execute your script.testingtesting.pl:
C:\>testingtesting.pl
Hello PERL!:
You should now have some basic concept of how to manage and run PERL scripts from the command prompt of your machine. Next we will be taking a look at how to manipulate user input via the command prompt.
PERL - <STDIN>
<STDIN> stands for standard input. It can be abbreviated by using simple <>. By declaring a scalar variable and setting it equal to <STDIN> we set the variable equal to whatever will be typed by our user at the command prompt. Observe:whatismyage.pl:
#! usr/bin/perl
print "How old are you?";
$age = <>;
print "WOW! You are $age years old!";
How old are you?:
Let's take this example one step further and also request the favorite color of our user.
agencolor.pl:
#! usr/bin/perl
print "How old are you?";
$age = <>;
print "What is your favorite color?";
$color = <>;
print "You are $age, and your favorite color is $color.";
Age 'N' Color:
PERL - Beginning Scripting
You now have the knowledge required to code some mathematical scripts like the following.circle.pl:
#! usr/bin/perl
print "What is the radius of the circle?";
chomp ($r = <>);
$diameter = (2 * $r);
$area = (3.14 * ($r ** 2));
$cir = ($diameter * 3.14);
print "Radius: $r\n Diameter: $diameter\n Circumference: $cir\n Area: $area";
Circles!:
PERL - Chomp
Let's take another look at our agencolor.pl script. This script asked for two user inputs and returned an unformatted string and our variables as a result on the last line.agencolor.pl:
#! usr/bin/perl
print "How old are you?";
$age = <>;
print "What is your favorite color?";
$color = <>;
print "You are $age, and your favorite color is $color.";
Here we have asked two questions, stored the user's input
into two separate variables and printed out the results all in just a few lines
of code. Be sure to take note of the formatting here. The user is required to
strike the "enter" key resulting in a line break. We can remove the
line break rather easily with the chomp() function. This function simply
removes any erroneous line breaks and spacing from the end of our string.agencolor2.pl:
#! usr/bin/perl
print "How old are you?";
chomp ($age = <>);
print "What is your favorite color?";
chomp ($color = <>);
print "You are $age, and your favorite color is $color.";
No comments:
Post a Comment