Developing Webs logo
Google
Web DW-Main DW-Photoshop
Visit the message board for solutions or post a question Check out the Classes offered for free at Developing Webs Voice Chat submit a tutorial, resources, article, font for publication consideration and earn a one way link back to your site Get more specifics about the Developing Webs Voice chat room and its room members View the current news at Developing Webs alternative sites which are resources for your d3evelopment needs Join Developing Webs Chat Link to us on your web page to help support this resource! Check out Developing Webs At yahoo groups! Developing Webs Home Adobe Photoshop Tutorials Macromedia Flash Tutorials Jasc Paint Shop Pro Tutorials Adobe Illustrator Animation basics Some Basic PHP tutorials Perl Tutorials HTML Tutorials Cascading Style Sheets javascript Front Page Tutorials Macromedia Dream Weaver Tutorials dreamweaver Publishing your site, FTP, ChMod, Promotions Tools to help you create your site Free and shareware fonts to download Photographs to use in your projects Shop for your site needs Free Online classes REcommend this site to others Log in or edit your information when logged in Developingwebs Log Out Change My Account details and preferences
RSS Newsfeed
for your site
DW News
Calendar
DW Forum
Right click on button and copy shortcut

Add to My Yahoo!

Reminder
Remember to book mark this site!



Request a Tutorial

The three data types

by pixelatedcyberdust, 1/13/04
 


There are three data types we use in Perl:

1) Scalar:

The first one, and most commonly used, is a scalar. A scalar holds a single string of information or a single perl command. In this example: $message = "HELLO WORLD!"; The scalar is anything that has a $ before it's name. In this case $message is a scalar and it's holding the information it's assigned to after the =.

2) Array:

The next type of variable is called an array. It's called by using @ instead of $. An array holds a LIST of information as opposed to a scalar which can hold a single string. An example of an array is: @names = ("Alberta", "Sky", "Photobug"); In this example we can assume that Perl will do something with those nicks after more variables are written. Arrays are used inside ()'s and are "" and comma separated. Each separate name, or each set of " .. " stands by itself and can be used separately later. Write: @names = ("Alberta", "Sky") then: print "@names"; in your test.pl file. Printing @array will print every element that's in the list, one after another and in NO specific order. Though later on you can tell the array in which order to print. By default it just prints it with this thought in mind "I'll print it no matter how I want, just you try and stop me" and it does it's own thing.

3) Hash:

The last type of variable is called a hash. A hash borrows the $ symbol from the scalar. $names{test} = "test"; A hash is a list of elements, much like the @array, but to the point where each element can be called upon and edited/deleted at any point in your program. Hashes are a little too advanced to get into this early. Go to this link to read up on the hash data type: http://perlmonks.com/index.pl?node_id=861.

Review:

There are three types of variables. The SCALAR holds one string of information. The ARRAY holds a list and so does the HASH.

Examples:

Let's jump into a few examples. We'll learn how to assign and use scalars to hold text and hopefully have a little fun doing it :) Open a new .pl and remove everything but the Shebang from your test.pl file so we can start with a new example.

print "What is your name?\n";
$name = <STDIN>;

We are now creating our first scalar variable called $name.We are assigning $name to <STDIN> which is Perl's Standard Input. While we are programming for the command line, STDIN is our way to taking information from the user so we can use it inside our script. We can do anything we want with this information; print it, alter it, write it to a textfile or database or virtually anything.

In order for us to use input from the user, we assign it to a variable. Assigning it to $name makes the input available throughout the rest of the program. Because this is a new system command, STDIN is Perl's Standard Input device. It's called by a filehandle. In Perl, there are exceptions to every rule which makes it hard to get into. Since this is a built-in command, we don't use quotes around the <STDIN>.

print "$name";

Now that we have the user input in $name, let's print it back out to the screen so they can see what they wrote. When you use a scalar or any other variable in a print command, it prints its value. Save the program and go back into the command prompt and run the program. It should ask you to type in your name, enter your name and click enter. What ever you just typed in will be displayed on the line underneath and will be stored into $name.

Complete example:

#!/usr/bin/perl
# How to store data into a variable example

print "What is your name?\n";
$name = <STDIN>;

print "$name";

We just printed $name back to the screen, but like we said earlier, there are many more things you can do with variables. Variables will contain their information until one of two things happen. 1) When the script ends, all the information gets deleted. 2) The information gets deleted or changed by assinging a new value to it.

To delete any of the three variables, we assign it a blank value. A blank or a null value in Perl looks like "". So to set $name to a value of nothing, we'd write $name = "";. The same goes with @arrays and $hashes as well. Try the next example and see what it does.

Complete example:

#!/usr/bin/perl

print "What is your name?\n";
$name = <STDIN>;

$name = "";

print "You typed in $name , or did you?";

As you may have expected, it will print out "You typed in , or did you?". We deleted $name the line before we printed it out. This example is useless in real life, there wouldn't be any use in asking for user input and deleting it before we get the chance to use it, but this serves as a good example on how it's done.
 



"Building The Web Into a Nicer Place -- One Site At A Time"
Developing Webs Group Copyright © 2001-2008 All Rights Reserved
Privacy and Legal