Programming Wiki: Perl Variable Basics

Programming Wiki : PerlVariableBasics

WikiHomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
Most recent edit on 2006-04-16 20:18:32 by ScottRippee

No differences.

Oldest known version of this page was edited on 2006-04-16 19:36:27 by ScottRippee []
Page view:


Perl Variable Basics

First, a regular variable in Perl is always preceeded by the $ sign. So, if you want a variable named adrevenue, you would have to write it as:

Number Values

Now, we need to give this variable a value. This is often done when the variable is defined. To start, lets give it a numerical value. To do this, we need to set the variable equal to a number using the = sign, and end the statement with a semicolon:

You can now use the variable inside your print statement to output the value of the $adrevenue variable:
print "The ad revenue for my site today is $adrevenue dollars.";

This just prints the sentence:

The ad revenue for my site today is 100 dollars.

String Values

To give a variable a string value (text, text with numbers), we need to surround the value using single quotes or double quotes. There is a difference in using the single or double quotes though. If you use single quotes, your string is taken as-is:
print "My stomach feels $my_stomach.";

This gives $my_stomach a value of full, and that is what is printed:

My stomach feels full.";

The use of double quotes allows you to use other variables as part of the definition of a variable. It would now recognize the $ sign as setting off another variable and not as part of the string. So, now you could use something like this:

$full_sentence="My stomach feels $my_stomach.";
print "$full_sentence";

Now, the value of $my_stomach is used as part of the $full_sentence variable. This can be very handy at times. Now, it would print the following sentence (again):

My stomach feels full.";
Powered by Wikka Wakka Wiki
Page was generated in 0.0172 seconds