Programming Wiki: Perl Variable Basics

Programming Wiki : PerlVariableBasics

WikiHomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register

Perl Variable Basics

Perl


http://www.pageresource.com/cgirec/ptut5.htm

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:
$adrevenue


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:
$adrevenue=100;

You can now use the variable inside your print statement to output the value of the $adrevenue variable:
$adrevenue=100;
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:
(perl
$my_stomach='full';
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:

$my_stomach='full';
$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.";

There are no comments on this page. [Add comment]

Powered by Wikka Wakka Wiki 1.1.6.0
Page was generated in 0.0173 seconds